Я создал ComboBoxItemDelegate, который я добавил в первый столбец QTableView. Я попытался использовать метод
currentIndexChanged(int index)
в соединении, чтобы получить индекс выбранного элемента в ComboBox. Это то, что я написал
connect(cbd, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboboxActivated(int)));
Но, похоже, не работает. Отладка говорит:
QObject :: connect: Нет такого сигнала MyComboBoxDelegate :: currentIndexChanged (int)
Я искал на inte rnet, но не на одном форуме topi c помог мне решить мою проблему.
Это мой mycomboboxitemdelegate.cpp
#include "mycomboboxitemdelegate.h"
MyComboBoxDelegate::MyComboBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent){
}
MyComboBoxDelegate::~MyComboBoxDelegate()
{
}
QWidget *MyComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *cb = new QComboBox(parent);
cb->addItem(QString("Debut Match"));
cb->addItem(QString("Ligne Droite"));
cb->addItem(QString("Courbe"));
cb->addItem(QString("Action"));
cb->addItem(QString("Recalage"));
cb->addItem(QString("Fin du match"));
cb->addItem(QString("Consigne XYT"));
return cb;
}
void MyComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *cb = qobject_cast<QComboBox *>(editor);
Q_ASSERT(cb);
model->setData(index, cb->currentText(), Qt::EditRole);
}
void MyComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QComboBox *cb = qobject_cast<QComboBox *>(editor);
Q_ASSERT(cb);
// get the index of the text in the combobox that matches the current value of the item
const QString currentText = index.data(Qt::EditRole).toString();
const int cbIndex = cb->findText(currentText);
// if it is valid, adjust the combobox
if (cbIndex >= 0)
cb->setCurrentIndex(cbIndex);
}
И мой mycomboboxitemdelegate.h
#ifndef MYCOMBOBOXITEMDELEGATE_H
#define MYCOMBOBOXITEMDELEGATE_H
#include <QStyledItemDelegate>
#include <QString>
#include <QComboBox>
#include <QtWidgets>
class MyComboBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MyComboBoxDelegate(QObject *parent = nullptr);
~MyComboBoxDelegate() override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
};
#endif // MYCOMBOBOXITEMDELEGATE_H
Спасибо за вашу помощь!