Results 1 to 4 of 4

Thread: I have problems with QListWidget and QGridLayout

  1. #1
    Join Date
    Nov 2007
    Qt products
    Unix/X11 Windows

    Default Re: I have problems with QListWidget and QGridLayout

    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:desired.jpg.while while what I got is this other one:
    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:

    Qt Code:
    1. boxCategory_ = new QListWidget;
    2. boxCategory_->setObjectName("boxCategory_");
    3. boxCategory_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
    4. boxCategory_->setLineWidth(1);
    5. boxCategory_->setFlow(QListView::TopToBottom);
    7. boxCategory_->clear();
    9. boxCategory_->setViewMode(QListView::IconMode);
    10. boxCategory_->addItem(new QListWidgetItem(QIcon(image0), tr("Projection (defaultable)")));
    11. boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("Radar Bias (from sensor)")));
    12. boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("Radar Noise (from sensor)")));
    13. boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("ADS Bias (defaultable)")));
    14. boxCategory_->addItem(new QListWidgetItem(QIcon(image1), tr("WAM Bias (from sensor)")));
    15. boxCategory_->addItem(new QListWidgetItem(QIcon(image2), tr("Roles (from sensor)")));
    16. boxCategory_->addItem(new QListWidgetItem(QIcon(image3), tr("External calls (defaultable)")));
    17. boxCategory_->addItem(new QListWidgetItem(QIcon(image4), tr("Other (defaultable)")));
    18. boxCategory_->addItem(new QListWidgetItem(QIcon(image4), tr("CMP DA, MxA, GA (defaultable)")));
    19. boxCategory_->addItem(new QListWidgetItem(QIcon(image4), tr("RRT classification (defaultable)")));
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. stackConfigPages_ = new QStackedWidget;
    2. stackConfigPages_->setObjectName("stackConfigPages_");
    3. stackConfigPages_->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
    4. stackConfigPages_->insertWidget(0, stackPageRoles_);
    5. stackConfigPages_->insertWidget(1, stackPageExternalCalls_);
    6. stackConfigPages_->insertWidget(2, stackPageProjections_);
    7. stackConfigPages_->insertWidget(3, stackPageRadarBias_);
    8. stackConfigPages_->insertWidget(4, stackPageOther_);
    9. stackConfigPages_->insertWidget(5, stackPageWAMBias_);
    10. stackConfigPages_->insertWidget(6, stackPageADSBias_);
    11. stackConfigPages_->insertWidget(7, stackPageCMP1_);
    12. stackConfigPages_->insertWidget(8, stackPageRadarNoise_);
    13. stackConfigPages_->insertWidget(9, stackPageCMP2_);
    15. btnResetToDefault_ = new QPushButton(tr("Reset to def&ault"));
    16. btnResetToDefault_->setObjectName("btnResetToDefault_");
    18. btnSetAsDefault_ = new QPushButton(tr( "Set as default" ));
    19. btnSetAsDefault_->setObjectName("btnSetAsDefault_");
    21. btnHelp_ = new QPushButton;
    22. btnHelp_->setObjectName("btnHelp_");
    24. btnOk_ = new QPushButton;
    25. btnOk_->setObjectName("btnOk_");
    26. btnOk_->setDefault(true);
    28. btnCancel_ = new QPushButton(tr( "Ca&ncel" ));
    29. btnCancel_->setObjectName("btnCancel_");
    31. OtrParametersDialogLayout->addWidget(boxCategory_, 0, 0, 1, 2); <-- The list widget (takes 1 row and 2 columns)
    32. OtrParametersDialogLayout->addWidget(stackConfigPages_, 0, 2, 1, 4); <-- The stacked widget (takes 1 row and 4 columns)
    33. OtrParametersDialogLayout->addWidget(btnResetToDefault_, 1, 0); <-- The "Reset to dafault" button
    34. OtrParametersDialogLayout->addWidget(btnSetAsDefault_, 1, 1 ); <-- The "Set as default" button
    35. OtrParametersDialogLayout->addWidget(btnHelp_, 1, 2, 1, 1); <-- The "Help" button
    36. OtrParametersDialogLayout->addItem(new QSpacerItem(50, 20, QSizePolicy::Expanding, QSizePolicy::Minimum), 1, 3); <-- a spacer to push on the right side the last 2 buttons
    37. OtrParametersDialogLayout->addWidget(btnOk_, 1, 5); <-- The Ok button
    38. OtrParametersDialogLayout->addWidget(btnCancel_, 1, 6); <-- The Cancel button
    To copy to clipboard, switch view to plain text mode 

    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,

    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
    Last edited by franco.amato; 28th August 2021 at 04:35. Reason: updated contents
    Franco Amato

  2. #2
    Join Date
    Jun 2012
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default Re: I have problems with QListWidget and QGridLayout


    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)

  3. The following user says thank you to stryga42 for this useful post:

    franco.amato (30th August 2021)

  4. #3
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 831 Times in 818 Posts
    Qt products
    Windows Android

    Default Re: I have problems with QListWidget and QGridLayout

    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.

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    franco.amato (30th August 2021)

  6. #4
    Join Date
    Nov 2007
    Qt products
    Unix/X11 Windows

    Default Re: I have problems with QListWidget and QGridLayout

    Hi stryga42,
    I set no parent to the boxCategory because I added it to a layout so the layout takes the ownership of it.
    Franco Amato

Similar Threads

  1. Replies: 2
    Last Post: 1st April 2011, 10:32
  2. Problems with scroll in QListWidget
    By VilmarCeller in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2010, 11:54
  3. Having resize problems with QGridLayout
    By di_zou in forum Newbie
    Replies: 0
    Last Post: 10th December 2009, 16:49
  4. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 14:01
  5. Qt 3.3 QGridLayout
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2007, 18:40


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.