如何使用设置了 cellWidget 的 QTableWidget 单元格处理信号 [英] How to work with signals from QTableWidget cell with cellWidget set

查看:494
本文介绍了如何使用设置了 cellWidget 的 QTableWidget 单元格处理信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTableWidget 里面有一些列.
由于我的需要,我在一些列中设置了 QComboBox 并用必要的数据填充它们.

I've got a QTableWidget with some columns inside.
Due to my needs I set QComboBox inside some columns and fill them with necessary data.

void settingsDialog::onAddFieldButtonClicked()
{
    fieldsTable->setRowCount(++rowCount);
    combo = new QComboBox();
    combo->addItem(QString("Choose from list..."));
    foreach( int height, heightsAvailable)
        combo->addItem(QString("%1").arg(height));
    fieldsTable->setCellWidget(rowCount-1, 3, combo);
    // etc for other columns ...
}

问题是如果这些组合框发生变化,如何从这些组合框中捕捉信号?
我想知道更改的小部件(组合框)的 rowcol 以及设置的值.

The question is how to catch signals from these combo boxes if they were changed?
I want to know row and col of changed widget (combo box) and the value which was set.


我已经尝试了 Qt 文档中为 QTableWidget 提到的所有可用信号,但它们仅在单元格内部没有小部件时才有效.
是否有一种简单的 Qt 方式来获得我需要的东西?


I've tried all available signals which are mentioned in Qt docs for QTableWidget, but they work only if cell doesn't have widget inside it.
Is there a simple and Qt-way to get what I need?

推荐答案

您可以处理来自组合框本身的 currentIndexChanged 信号,而不是处理来自表的信号.

Instead of handling a signal from the table, you can handle currentIndexChanged signal from combo box itself.

QComboBox* combo = new QComboBox();
combo->addItem(QString("Choose from list..."));
combo->addItem(QString("first item"));
combo->setProperty("row", ui->tableWidget->rowCount() - 1);
combo->setProperty("column", 0);
connect(combo, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
ui->tableWidget->setCellWidget(ui->tableWidget->rowCount() - 1, 0, combo);

在插槽中,您可以使用 sender() 来识别发出信号的组合框.

And in the slot, you can use sender() to identify the combo box which emitted the signal.

void MainWindow::OnComboIndexChanged(const QString& text)
{
    QComboBox* combo = qobject_cast<QComboBox*>(sender());
    if (combo)
    {
        qDebug() << "row: " << combo->property("row").toInt();
        qDebug() << "column: " << combo->property("column").toInt();
    }
}

这篇关于如何使用设置了 cellWidget 的 QTableWidget 单元格处理信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆