PDA

View Full Version : How to get value from QCombobox which is configured as QWidget in QTableWidget ??



San_4368
21st December 2017, 08:14
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.


QWidget *pWidget1 = new QWidget();
QComboBox *ipcombo = new QComboBox;
QHBoxLayout * pLayout1 = new QHBoxLayout(pWidget1);
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.


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

high_flyer
21st December 2017, 10:27
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:


void MainWindow::on_submitbtn_2_clicked()
{
QStringlist ComboData;
QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget_2->cellWidget(row,3));
if(myCB){
Combodata<< myCB->currentText();
}
}

d_stranz
21st December 2017, 16:42
Well, there is a small difference between this:

ui->tableWidget->setCellWidget(1,3,pWidget1);
and this:

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.