PDA

View Full Version : Is it possible to put a QGroupBox into a cell of QTableWidget ??



jiapei100
12th February 2013, 14:54
Hi, all:

I followed the code on page http://doc.qt.digia.com/4.7-snapshot/widgets-groupbox.html . The code patch is attached:



QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));

QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));

radio1->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addStretch(1);
groupBox->setLayout(vbox);

Since groupBox is a QWidget, I would like add groupBox into a widget cell as :

ui->tableWidget->setCellWidget(0, 4, groupBox);

But it seems this doesn't work for me at all. Those radios can't show up at all.

Can anybody help to let me know if it's possible to put a groupBox into a tableWidget cell? If so, how?


Thanks
Pei

Santosh Reddy
12th February 2013, 15:08
Yes it is possible, just make sure the table has the specified row, and column.


ui->tableWidget->setRowCount(1);
ui->tableWidget->setColumnCount(5);

jiapei100
12th February 2013, 21:47
Thanks Santosh Reddy.

This doesn't work for me at all... I'm pretty sure my tableWidget does have 6 columns and 6 rows.
Please refer to http://qt-project.org/forums/viewthread/24742/#114245, somebody answered my question, but that's not the solution I want.


Any other suggestions please?


Thank you
Pei



Yes it is possible, just make sure the table has the specified row, and column.


ui->tableWidget->setRowCount(1);
ui->tableWidget->setColumnCount(5);

Santosh Reddy
12th February 2013, 21:58
Then I will say that you are missing somthing to mention.
What does the output look like ?
Are you sure setCellWidget is being called ?

ChrisW67
12th February 2013, 22:43
Make sure the cell is large enough to show the widget otherwise you will just get the top left corner of the group box.


#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QTableWidget w(6, 6);
QGroupBox *groupBox = new QGroupBox("Radio Buttons", &w);
QRadioButton *radio1 = new QRadioButton("&Radio button 1", &w);
QRadioButton *radio2 = new QRadioButton("R&adio button 2", &w);
QRadioButton *radio3 = new QRadioButton("Ra&dio button 3", &w);
radio1->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout(groupBox);
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addStretch(1);
groupBox->setLayout(vbox);

w.setCellWidget(0, 0, groupBox);

// This:
w.resizeRowToContents(0);
w.resizeColumnToContents(0);
// or this:
// w.verticalHeader() ->resizeSection(0, groupBox->sizeHint().height());
// w.horizontalHeader()->resizeSection(0, groupBox->sizeHint().width());
// might be good places to start

w.show();

return app.exec();
}


8709