问题
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