QT开发中文网

如何在mainwindow捕捉delegate过的comboBox里数据变化的信息?

您可以在delegate类中使用信号和槽来捕获comboBox里的数据变化,并将这些信息发送给MainWindow。以下是一个示例:

  1. 在自定义delegate类中,添加一个自定义信号valueChanged,用于向MainWindow发送comboBox的值改变信息。

  2. 在createEditor函数中,连接comboBox的currentIndexChanged信号到自定义valueChanged信号。

  3. 在MainWindow中,连接delegate的valueChanged信号到槽函数。

  4. 在槽函数中获取comboBox的新值并进行相应操作。

下面是代码示例:

#include <QtWidgets>

class MyDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    explicit MyDelegate(QObject *parent = nullptr);

    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;

signals:
    void valueChanged(const QString& newValue);

private slots:
    void onCurrentIndexChanged(int index);

private:
    mutable QMap<int, QStringList> m_comboBoxItems; // 存储每行第一列ComboBox的items
};

MyDelegate::MyDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

QWidget* MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                   const QModelIndex &index) const
{
   if (index.column() == 0) { // 第一列用ComboBox编辑器
       QComboBox* comboBox = new QComboBox(parent);
       QStringList items = {"item1", "item2", "item3"};
       comboBox->addItems(items);
       connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
       connect(this, SIGNAL(valueChanged(QString)), parent, SLOT(onComboBoxValueChanged(QString)));

       m_comboBoxItems.insert(index.row(), items);

       return comboBox;
   } else if (index.column() == 1) { // 第二列用空白编辑器
       return new QWidget(parent);
   }

   return QStyledItemDelegate::createEditor(parent, option, index);
}

void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
        comboBox->setCurrentText(index.data(Qt::EditRole).toString());
    }
}

void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                               const QModelIndex &index) const
{
    if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
        model->setData(index, comboBox->currentText(), Qt::EditRole);

        // 在第一列选中项改变时更新第二列的ComboBox
        int row = index.row();
        int column = index.column();
        QStringList items = m_comboBoxItems.value(row);
        QString selectedItem = comboBox->currentText();

        if (!selectedItem.isEmpty()) {
            // 如果此行第二列为空,添加一个新的comboBox
            if (model->data(model->index(row, column+1)).toString().isEmpty()) {
                QComboBox* newComboBox = new QComboBox(qobject_cast<QWidget*>(editor));
                newComboBox->addItems(items);

                connect(newComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
                connect(this, SIGNAL(valueChanged(QString)), qobject_cast<QWidget*>(newComboBox)->parentWidget(),
                        SLOT(onComboBoxValueChanged(QString)));

                model->setData(model->index(row, column+1), QVariant::fromValue<QWidget*>(newComboBox));
            }
            // 更新已有的comboBox的items
            else {
                QComboBox* existingBox = qvariant_cast<QWidget*>(model->data(model->index(row, column+1), Qt::EditRole));
                if (existingBox != nullptr) {
                    existingBox->clear();
                    existingBox->addItems(items);
                }
            }
        } else {
            // 如果第一列选中项为空,将此行的第二列清空
            model->setData(model->index(row, column+1), QVariant());
        }
    }
}

void MyDelegate::onCurrentIndexChanged(int index)
{
    Q_UNUSED(index)

    emit valueChanged(qobject_cast<QComboBox*>(sender())->currentText());
    emit commitData(qobject_cast<QWidget*>(sender()));
    emit closeEditor(qobject_cast<QWidget*>(sender()));
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

private slots:
    void onComboBoxValueChanged(const QString& newValue);

private:
    QTreeView *m_treeView;
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    m_treeView = new QTreeView(this);
    QStandardItemModel *model = new QStandardItemModel(0, 2, this);
    m_treeView->setModel(model);

    MyDelegate *delegate = new MyDelegate(this);
    m_treeView->setItemDelegate(delegate);

    connect(delegate, SIGNAL(valueChanged(QString)), this, SLOT(onComboBoxValueChanged(QString)));

    setCentralWidget(m_treeView);
}

void MainWindow::onComboBoxValueChanged(const QString& newValue)
{
   qDebug() << "New value: " << newValue; // 在这里获取comboBox的新值并进行相应操作
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

   MainWindow w;
   w.show();

   return a.exec();
}

#include "main.moc"

在自定义delegate类中,添加了一个valueChanged信号,并将comboBox的currentIndexChanged信号连接到该信号上。在MainWindow中,连接delegate的valueChanged信号到槽函数onComboBoxValueChanged。在onComboBoxValueChanged函数中获取comboBox的新值并进行相应操作。需要注意的是,在createEditor函数中,要将delegate的valueChanged信号连接到每个新创建的comboBox控件上,以便在其值发生改变时向MainWindow发送信息。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=3017

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?