简体   繁体   中英

Get index of a ComboBoxItemDelegate in a QTableView

I created a ComboBoxItemDelegate which I added in the first column of a QTableView. I tried to use the

currentIndexChanged(int index)

method within a connect to get the index of the selected item in the ComboBox. This is what I wrote

connect(cbd, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboboxActivated(int)));

But it appears to not work. Debug says

QObject::connect: No such signal MyComboBoxDelegate::currentIndexChanged(int)

I searched on the internet but not a single forum topic helped me to solve my problem.

This is my 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);
}

And my 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

Thanks for your help !

Since there is only one delegate and not one for every cell - why do you need this information at all? You're creating an instance in MyComboBoxDelegate::createEditor() - there you can also connect the signal if you really want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM