How to get value from QCombobox which is configured as QWidget in QTableWidget ??
Hello All,
I have created a Qcombobox as a Qwidget in a QtableWidget and it is working fine, The below code i have used for creating combobox in table widget.
Code:
pLayout1->addWidget(ipcombo);
pLayout1->setAlignment(Qt::AlignCenter);
pLayout1->setContentsMargins(0,0,0,0);
pWidget1->setLayout(pLayout1);
ipcombo->addItem("YES");
ipcombo->addItem("NO");
ui->tableWidget->setCellWidget(1,3,pWidget1);
connect(ipcombo,
SIGNAL(currentIndexChanged
(QString)),
this,
SLOT(on_currentIndexChanged
(QString)));
index++;
now the problem is i couldn't get the data out of the combobox which is assigned as a cellwidget. the below code only am using to get the data. if the button is clicked the SIGSEGV segmentation fault error is happening.
Code:
void MainWindow::on_submitbtn_2_clicked()
{
QStringlist ComboData;
QComboBox *myCB
= qobject_cast<QComboBox
*>
(ui
->tableWidget_2
->cellWidget
(row,
3));
Combodata<< myCB->currentText();
}
Kindly help me
thanks in advance..
Re: How to get value from QCombobox which is configured as QWidget in QTableWidget ??
Quote:
f the button is clicked the SIGSEGV segmentation fault error is happening.
That is becase probably the returned QComboBox pointer is null:
This should not crash:
Code:
void MainWindow::on_submitbtn_2_clicked()
{
QStringlist ComboData;
QComboBox *myCB
= qobject_cast<QComboBox
*>
(ui
->tableWidget_2
->cellWidget
(row,
3));
if(myCB){
Combodata<< myCB->currentText();
}
}
Re: How to get value from QCombobox which is configured as QWidget in QTableWidget ??
Well, there is a small difference between this:
Quote:
ui->tableWidget->setCellWidget(1,3,pWidget1);
and this:
Quote:
QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget_2->cellWidget(row,3));
which could easily case a crash when you try to use the NULL pointer returned by the code above.