How to work with signals from QTableWidget cell with cellWidget set

帅比萌擦擦* 提交于 2019-12-11 12:42:50

问题


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 ...
}

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


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?


回答1:


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);

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();
    }
}


来源:https://stackoverflow.com/questions/30484784/how-to-work-with-signals-from-qtablewidget-cell-with-cellwidget-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!