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