PDA

View Full Version : Scroll checkboxes in tabWidget



Amindo
18th July 2007, 13:48
I have created with "QT Designer" simple form with tabWidget
I need to dynamically add checkboxes to tab area:

in my constructor:

======
ui.setupUi(this);

ui.tabWidget->QTabWidget::setTabText(0, QString::fromLocal8Bit("Params"));
ui.tabWidget->QTabWidget::setTabText(1, QString::fromLocal8Bit("SysParams"));



QCheckBox *mycheckBox[100];

mycheckBox[0] = new QCheckBox(ui.tab);
mycheckBox[0]->setObjectName(QString::fromUtf8("mycheckBox[0]"));
mycheckBox[0]->setGeometry(QRect(30, 30, 331, 18));
mycheckBox[0]->setText(QApplication::translate("BarkasikForm", "myCheckBox0", 0, QApplication::UnicodeUTF8));
=======

I can't understand how to scroll tab area when number of checkboxes > 10 and out of viewable area.

I found some code:

=====
QScrollArea *scrollArea;
scrollArea = new QScrollArea(ui.tab);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(ui.<?????????>);
=====


Help please!

Michiel
18th July 2007, 14:35
Something like:


QScrollArea* scrollArea = new QScrollArea(ui.tab);
QWidget* checkboxes = new QWidget(scrollArea);
scrollArea->setWidget(checkboxes);

// ...

mycheckBox[0] = new QCheckBox(checkboxes);

// ...

Beware of bugs. This code has not been tested. :)

Amindo
19th July 2007, 07:05
:(
Checkboxes is not visible.

jpn
19th July 2007, 09:25
Use layouts (http://doc.trolltech.com/4.3/layout). And construct the content first, then set it to the scroll area.


QScrollArea* scrollArea = new QScrollArea(ui.tab);
QWidget* checkboxes = new QWidget(scrollArea);
QVBoxLayout* layout = new QVBoxLayout(checkboxes);
for (int i = 0; i < 100; ++i)
{
mycheckBox[i] = new QCheckBox(checkboxes);
...
layout->addWidget(mycheckBox[i]);
}
scrollArea->setWidget(checkboxes); // must be last, "QWidget checkboxes" is resized according to its size hint

Amindo
19th July 2007, 11:11
Thanks. Works fine. But how to set size of scrollable layout to fit tab frame geometry?

jpn
19th July 2007, 11:14
You must apply a layout to the tab page as well. Where do you add the tabs, in code or in designer?

Edit: Oh, what's "ui.tab"? Presumably "ui.tabWidget" is the tab widget, so is "ui.tab" the first tab page?


QScrollArea* scrollArea = new QScrollArea(ui.tab);
QVBoxLayout* pageLayout = new QVBoxLayout(ui.tab);
pageLayout->addWidget(scrollArea);
...

Or simply add the scroll area and apply a layout in designer.. :)

Amindo
19th July 2007, 12:12
Thanks fine code! :cool:


For hystory: my working code......



ScrollArea* scrollArea = new QScrollArea(ui.tab);
QWidget* checkboxes = new QWidget(scrollArea);
QVBoxLayout* layout = new QVBoxLayout(checkboxes);

QVBoxLayout* pageLayout = new QVBoxLayout(ui.tab);
pageLayout->addWidget(scrollArea);


QCheckBox *mycheckBox[100];

for (int i = 0; i < 100; ++i)
{

mycheckBox[i] = new QCheckBox(checkboxes);
mycheckBox[i]->setGeometry(QRect(30, 30, 331, 18));
mycheckBox[i]->setText(QApplication::translate("BarkasikForm", "my0", 0, QApplication::UnicodeUTF8));

layout->addWidget(mycheckBox[i]);

}

scrollArea->setWidget(checkboxes);

Michiel
19th July 2007, 18:32
Now that the checkboxes are in a layout, you can leave out the setGeometry call.