PDA

View Full Version : I have problems with QListWidget and QGridLayout



franco.amato
28th August 2021, 03:38
Hi community,

I am porting to C++/Qt a dialog which was initially written in Java (I can not use QtDesigner for this project)

The problems I am having are mainly related to adding items to a QListWidget and to layout the dialog using QGridLayout.

The desired result I would like to achieve is the one in this image:13716.while while what I got is this other one:
13715
First problem that I am unable to solve: on the left side of the dialog there is a widget, of type QListWidget, named boxCategory_. Each QListWidgetItems item that I add contains an icon and a text. In the picture the text and the icon are not placed in the same line, the text is placed below the icon.

The code snipped I used to create the widget is the following:


boxCategory_ = new QListWidget;
boxCategory_->setObjectName("boxCategory_");
boxCategory_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
boxCategory_->setLineWidth(1);
boxCategory_->setFlow(QListView::TopToBottom);

boxCategory_->clear();

boxCategory_->setViewMode(QListView::IconMode);
boxCategory_->addItem(new QListWidgetItem(QIcon(image0), tr("Projection (defaultable)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("Radar Bias (from sensor)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("Radar Noise (from sensor)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("ADS Bias (defaultable)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("WAM Bias (from sensor)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image2), tr("Roles (from sensor)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image3), tr("External calls (defaultable)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image4), tr("Other (defaultable)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image4), tr("CMP DA, MxA, GA (defaultable)")));
boxCategory_->addItem(new QListWidgetItem(QIcon(image4), tr("RRT classification (defaultable)")));

Giving a parent to the QListWidgetItem does not solve the problem and calling the QListWidget::addItem() neither.
The QListWidgetItem inside the QListWidget are also movable, I can drag them with the mouse, very strange thing.

The second problem I am not able to solve consists in the arrangement of objects using the QGridLayout.
I don't understand why the 2 QPushButton (ok and cancel) are pushed more to the right of the QStackedWidget above.
Here the code:



stackConfigPages_ = new QStackedWidget;
stackConfigPages_->setObjectName("stackConfigPages_");
stackConfigPages_->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
stackConfigPages_->insertWidget(0, stackPageRoles_);
stackConfigPages_->insertWidget(1, stackPageExternalCalls_);
stackConfigPages_->insertWidget(2, stackPageProjections_);
stackConfigPages_->insertWidget(3, stackPageRadarBias_);
stackConfigPages_->insertWidget(4, stackPageOther_);
stackConfigPages_->insertWidget(5, stackPageWAMBias_);
stackConfigPages_->insertWidget(6, stackPageADSBias_);
stackConfigPages_->insertWidget(7, stackPageCMP1_);
stackConfigPages_->insertWidget(8, stackPageRadarNoise_);
stackConfigPages_->insertWidget(9, stackPageCMP2_);

btnResetToDefault_ = new QPushButton(tr("Reset to def&ault"));
btnResetToDefault_->setObjectName("btnResetToDefault_");

btnSetAsDefault_ = new QPushButton(tr( "Set as default" ));
btnSetAsDefault_->setObjectName("btnSetAsDefault_");

btnHelp_ = new QPushButton;
btnHelp_->setObjectName("btnHelp_");

btnOk_ = new QPushButton;
btnOk_->setObjectName("btnOk_");
btnOk_->setDefault(true);

btnCancel_ = new QPushButton(tr( "Ca&ncel" ));
btnCancel_->setObjectName("btnCancel_");

OtrParametersDialogLayout->addWidget(boxCategory_, 0, 0, 1, 2); <-- The list widget (takes 1 row and 2 columns)
OtrParametersDialogLayout->addWidget(stackConfigPages_, 0, 2, 1, 4); <-- The stacked widget (takes 1 row and 4 columns)
OtrParametersDialogLayout->addWidget(btnResetToDefault_, 1, 0); <-- The "Reset to dafault" button
OtrParametersDialogLayout->addWidget(btnSetAsDefault_, 1, 1 ); <-- The "Set as default" button
OtrParametersDialogLayout->addWidget(btnHelp_, 1, 2, 1, 1); <-- The "Help" button
OtrParametersDialogLayout->addItem(new QSpacerItem(50, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 1, 3); <-- a spacer to push on the right side the last 2 buttons
OtrParametersDialogLayout->addWidget(btnOk_, 1, 5); <-- The Ok button
OtrParametersDialogLayout->addWidget(btnCancel_, 1, 6); <-- The Cancel button

I tried everything. I tried to remove the QSpacerItem, I tried to give the big widget an additional column, but the 2 buttons (ok and cancel) are increasingly to the right .

I am using Qt 5.9.7.

I hope to get some help in understanding the problems.

Thanks in advance,
Franco

I solved the problem of the 2 buttons that goes too much on the right side, I still have the problem of the QListWidget's items, I can not have the icon and text on the same line

stryga42
28th August 2021, 14:56
Hi!

In your first code snippet boxCategory_ has no parent - are you sure that you add it correctly to the dialog? Remember that in the end all widgets of the dialog have to be in correct parent / child relation.
What about creating a small test project, use the Qt Designer there to set up the GUI and then copy-and-paste the code out of the setup method in ui_blabla.h?
Are you sure that you have to set the setSizePolicy of the list widget? What happens if you omit it?

General remark: Although a grid layout will work it is quite cumbersome to setup and tune. Maybe a nested HBox / VBox / HBox is easier. Especially the button row at the bottom calls for a HBox. (Aligning the button widths with the widgets above is unusual, I think)

d_stranz
28th August 2021, 16:01
I agree with stryga42 - using a grid for this is awkward. I would make the entire layout a VBox. Inside that, I would put two HBoxes, the top one for the list and stacked widget, the bottom for the buttons. For the button box, add the three left buttons, a spacer, and the two right buttons. For the top box, add the list widget and then the stacked widget. If you want the stack widget to stretch more than the list widget, either set a maximum size for the list or set a non-zero stretch for the stacked widget.

As for the icons, try calling setTextAlignment() on the QListWidgetItem instances with Qt::AlignLeft and Qt::AlignVCenter. It could also be that the combination of padding, border, and margin for the QListView is making it too small for the layout algorithm to fit both the icon and text on the same line. If that still doesn't work, you may have to edit the style sheet using QListView::item, QListView::icon, and/or QListView::text sub-control customization. (https://doc.qt.io/qt-5/stylesheet-reference.html#item-sub)

And if still nothing works to fix the alignment (it shouldn't be this hard...), you could switch to a QTreeWidget with the icon in column 0 and the text in column 1 of each row.

franco.amato
30th August 2021, 06:37
Hi stryga42,
I set no parent to the boxCategory because I added it to a layout so the layout takes the ownership of it.
Sincerely