【问题标题】:Get index of a ComboBoxItemDelegate in a QTableView获取 QTableView 中 ComboBoxItemDelegate 的索引
【发布时间】:2020-03-10 16:58:41
【问题描述】:

我创建了一个 ComboBoxItemDelegate,我将它添加到 QTableView 的第一列中。我尝试使用

currentIndexChanged(int index)

连接中的方法以获取组合框中所选项目的索引。这是我写的

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

但它似乎不起作用。调试说

QObject::connect: 没有这样的信号 MyComboBoxDelegate::currentIndexChanged(int)

我在互联网上进行了搜索,但没有一个论坛主题可以帮助我解决我的问题。

这是我的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

感谢您的帮助!

【问题讨论】:

    标签: c++ qt tableview qcombobox qstyleditemdelegate


    【解决方案1】:

    既然只有一个代表,而不是每个单元都有一个代表 - 为什么您需要这些信息?您正在 MyComboBoxDelegate::createEditor() 中创建一个实例 - 如果您真的想要,您也可以在那里连接信号。

    【讨论】:

    • 我需要这些信息来根据所选索引更新我的 tableView 的标题。 “MyComboBoxDelegate::createEditor()”中的“连接信号”你想说什么?
    • 我设法让我的工作通过这样做:connect(cb, SIGNAL(currentIndexChanged(int)), parent-&gt;parent()-&gt;parent(), SLOT(onComboboxActivated(int))); 正如你所说,我在“MyComboBoxDelegate::createEditor()”中写了这个
    猜你喜欢
    • 2018-07-14
    • 2015-12-29
    • 2019-09-08
    • 1970-01-01
    • 2018-09-08
    • 2015-10-08
    • 2015-09-30
    • 2017-09-02
    相关资源
    最近更新 更多