Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Layout of item in QListWidget
Qt 6.11 is out! See what's new in the release blog

Layout of item in QListWidget

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DragoonD Offline
    DragoonD Offline
    Dragoon
    wrote on last edited by Dragoon
    #1

    I'm a newbie in Qt and I need to create a list of combined widgets to manage a drone pre-flight checklist.
    I decided to use the QListWidget. I defined the widgets compound (testwidget.cpp) trough QT Designer in this way:

    bfb1071d-c2d9-47c2-a823-cb57b9288e54-image.png

    I needed to customize the constructor of the compound in this way:

    TestWidget::TestWidget(QTest *test, QWidget *parent)
        : TestWidget(parent)
    {
        mTest = test;
    
        ui->lblName->setText(test->label());
        ui->lblDescription->setText(test->description());
    }
    

    Then I placed a QListWidget item in my main form and tried to populate it with some items trough this code:

    void MainWindow::initUI() {
      setPageControls(ui->droneStack->currentIndex());
    
      // Initalize checklist
      initCheckList();
    }
    
    void MainWindow::initCheckList() {
        addTest("Test motor 1", "Execute motor1 test", ui->droneTestList);
        addTest("Test motor 2", "Execute motor2 test", ui->droneTestList);
        addTest("Test motor 3", "Execute motor3 test", ui->droneTestList);
        addTest("Test motor 4", "Execute motor4 test", ui->droneTestList);
    }
    
    void MainWindow::addTest(QString name, QString description, QListWidget* list) {
        QListWidgetItem *_item = new QListWidgetItem(list);
    
        list->addItem(_item);
    
        QTest* _test = new QTest(name, description);
    
        TestWidget *_widget= new TestWidget(_test);
    
        _item->setSizeHint(_widget->sizeHint());
    
        list->setItemWidget(_item, _widget);
    }
    

    When I run the app this's the result:

    a20e5357-2f44-4c06-be23-a3be4b31e35b-image.png

    It looks like the list item is not vertically adjusted to fit the content.
    How can I fix it? I thought this line of code should do the magic (as suggested in some sample code):

    _item->setSizeHint(_widget->sizeHint());
    

    But it doesn't seem to have any effect.

    bye by[t]e{s}... Mike

    jsulmJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #7

      Hi,

      Why are you using a QListWidget since you don't even make use of the model view part of it ?

      A QScrollArea would suffice for this task.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      DragoonD 2 Replies Last reply
      0
      • DragoonD Dragoon

        I'm a newbie in Qt and I need to create a list of combined widgets to manage a drone pre-flight checklist.
        I decided to use the QListWidget. I defined the widgets compound (testwidget.cpp) trough QT Designer in this way:

        bfb1071d-c2d9-47c2-a823-cb57b9288e54-image.png

        I needed to customize the constructor of the compound in this way:

        TestWidget::TestWidget(QTest *test, QWidget *parent)
            : TestWidget(parent)
        {
            mTest = test;
        
            ui->lblName->setText(test->label());
            ui->lblDescription->setText(test->description());
        }
        

        Then I placed a QListWidget item in my main form and tried to populate it with some items trough this code:

        void MainWindow::initUI() {
          setPageControls(ui->droneStack->currentIndex());
        
          // Initalize checklist
          initCheckList();
        }
        
        void MainWindow::initCheckList() {
            addTest("Test motor 1", "Execute motor1 test", ui->droneTestList);
            addTest("Test motor 2", "Execute motor2 test", ui->droneTestList);
            addTest("Test motor 3", "Execute motor3 test", ui->droneTestList);
            addTest("Test motor 4", "Execute motor4 test", ui->droneTestList);
        }
        
        void MainWindow::addTest(QString name, QString description, QListWidget* list) {
            QListWidgetItem *_item = new QListWidgetItem(list);
        
            list->addItem(_item);
        
            QTest* _test = new QTest(name, description);
        
            TestWidget *_widget= new TestWidget(_test);
        
            _item->setSizeHint(_widget->sizeHint());
        
            list->setItemWidget(_item, _widget);
        }
        

        When I run the app this's the result:

        a20e5357-2f44-4c06-be23-a3be4b31e35b-image.png

        It looks like the list item is not vertically adjusted to fit the content.
        How can I fix it? I thought this line of code should do the magic (as suggested in some sample code):

        _item->setSizeHint(_widget->sizeHint());
        

        But it doesn't seem to have any effect.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Dragoon said in Layout of item in QListWidget:

        Then I placed a QListWidget item in my main form

        How? In the designer? Is it in a layout?

        DragoonD 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Dragoon said in Layout of item in QListWidget:

          Then I placed a QListWidget item in my main form

          How? In the designer? Is it in a layout?

          DragoonD Offline
          DragoonD Offline
          Dragoon
          wrote on last edited by Dragoon
          #3

          @jsulm yes the QListWidget is placed in the tab of a QTabWidget and the layout of the page is set as a vertical layout.

          <widget class="QWidget" name="uav1checkList">
              <property name="font">
                  <font>
                         <pointsize>11</pointsize>
                         <bold>false</bold>
                    </font>
               </property>
               <attribute name="title">
                    <string>Check List</string>
                </attribute>
                <layout class="QVBoxLayout" name="verticalLayout_9">
                    <item>
                         <widget class="QListWidget" name="uav1TestList"/>
                    </item>
                 </layout>
          </widget>
          

          bye by[t]e{s}... Mike

          jsulmJ 1 Reply Last reply
          0
          • DragoonD Dragoon

            @jsulm yes the QListWidget is placed in the tab of a QTabWidget and the layout of the page is set as a vertical layout.

            <widget class="QWidget" name="uav1checkList">
                <property name="font">
                    <font>
                           <pointsize>11</pointsize>
                           <bold>false</bold>
                      </font>
                 </property>
                 <attribute name="title">
                      <string>Check List</string>
                  </attribute>
                  <layout class="QVBoxLayout" name="verticalLayout_9">
                      <item>
                           <widget class="QListWidget" name="uav1TestList"/>
                      </item>
                   </layout>
            </widget>
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Dragoon said in Layout of item in QListWidget:

            the QListWidget is placed in the tab of a QTabWidget

            and is uav1checkList also in a layout? Does it resize when you're resizing the tab?

            DragoonD 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Dragoon said in Layout of item in QListWidget:

              the QListWidget is placed in the tab of a QTabWidget

              and is uav1checkList also in a layout? Does it resize when you're resizing the tab?

              DragoonD Offline
              DragoonD Offline
              Dragoon
              wrote on last edited by Dragoon
              #5

              @jsulm uav1checkList is just one of the page of the container QTabWidget (that's part of a more complex hierarchy).
              At run time it seems that all the widgets are resized correctly.

              <item>
                  <widget class="QTabWidget" name="uav1Stack">
                      <property name="font"> ... </property>
                      <property name="styleSheet">... </property> 
                      <property name="tabPosition">... </property> 
                      <property name="currentIndex">... </property> 
                      <property name="iconSize">... </property> 
                      <widget class="QWidget" name="uav1home">
                      <widget class="QWidget" name="uav1checkList">
                      <widget class="QWidget" name="uav1manualControl">
                      <widget class="QWidget" name="uav1video">
                  </widget>
              </item>
              

              bye by[t]e{s}... Mike

              jsulmJ 1 Reply Last reply
              0
              • DragoonD Dragoon

                @jsulm uav1checkList is just one of the page of the container QTabWidget (that's part of a more complex hierarchy).
                At run time it seems that all the widgets are resized correctly.

                <item>
                    <widget class="QTabWidget" name="uav1Stack">
                        <property name="font"> ... </property>
                        <property name="styleSheet">... </property> 
                        <property name="tabPosition">... </property> 
                        <property name="currentIndex">... </property> 
                        <property name="iconSize">... </property> 
                        <widget class="QWidget" name="uav1home">
                        <widget class="QWidget" name="uav1checkList">
                        <widget class="QWidget" name="uav1manualControl">
                        <widget class="QWidget" name="uav1video">
                    </widget>
                </item>
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Hi,

                  Why are you using a QListWidget since you don't even make use of the model view part of it ?

                  A QScrollArea would suffice for this task.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  DragoonD 2 Replies Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    Why are you using a QListWidget since you don't even make use of the model view part of it ?

                    A QScrollArea would suffice for this task.

                    DragoonD Offline
                    DragoonD Offline
                    Dragoon
                    wrote on last edited by Dragoon
                    #8

                    @SGaist because I cannot find QScrollArea examples while I found many QListWidget ones.

                    26b84bb8-e96d-42eb-9fc4-31c2bc0907bb-image.png

                    uhm ... it doesn't seem to look the way I meant...:

                    void MainWindow::addTestItem(QString name, QString description,
                                                 QScrollArea *list) {
                      QTest *_test = new QTest(name, description);
                    
                      TestWidget *_widget = new TestWidget(_test);
                    
                      list->setWidget(_widget);
                    }
                    

                    bye by[t]e{s}... Mike

                    jsulmJ 1 Reply Last reply
                    0
                    • DragoonD Dragoon

                      @SGaist because I cannot find QScrollArea examples while I found many QListWidget ones.

                      26b84bb8-e96d-42eb-9fc4-31c2bc0907bb-image.png

                      uhm ... it doesn't seem to look the way I meant...:

                      void MainWindow::addTestItem(QString name, QString description,
                                                   QScrollArea *list) {
                        QTest *_test = new QTest(name, description);
                      
                        TestWidget *_widget = new TestWidget(_test);
                      
                        list->setWidget(_widget);
                      }
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @Dragoon said in Layout of item in QListWidget:

                      because I cannot find QScrollArea examples

                      https://doc.qt.io/qt-6/qtwidgets-widgets-imageviewer-example.html

                      DragoonD 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Dragoon said in Layout of item in QListWidget:

                        because I cannot find QScrollArea examples

                        https://doc.qt.io/qt-6/qtwidgets-widgets-imageviewer-example.html

                        DragoonD Offline
                        DragoonD Offline
                        Dragoon
                        wrote on last edited by
                        #10

                        @jsulm uhm... it doesn't seems to make what I need... this example is an appplication that loads one image inside the window and will manage scrolling bars if needed.
                        If you load a second image it just replace the first one, or at least that's what it seems to happen from the user point of view...
                        I haven't digged much on that code since it seems to lack the basic requirement I need.
                        I need to add several different istances of the same compound of widgets (TestWidget) to a TabWidget page.
                        I don't doubt that QScrollArea can do it but I'm guessing how much effort it needs in comparision of QListWidget?

                        bye by[t]e{s}... Mike

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Hi,

                          Why are you using a QListWidget since you don't even make use of the model view part of it ?

                          A QScrollArea would suffice for this task.

                          DragoonD Offline
                          DragoonD Offline
                          Dragoon
                          wrote on last edited by
                          #11

                          @SGaist I found a possibile solution using QScrollArea.. I needed to add a VerticalLayout into ScrollAreaContentWidget and then add the compound widget to such layout:

                          void MainWindow::addTestItem(QString name, QString description,
                                                      QVBoxLayout *layout) {
                           QTest *_test = new QTest(name, description);
                          
                           TestWidget *_widget = new TestWidget(_test);
                          
                           layout->addWidget(_widget);
                          }
                          

                          Now it seems to display correctly:
                          d96cdd5f-865b-4a99-b952-f26f940f82c1-image.png

                          bye by[t]e{s}... Mike

                          1 Reply Last reply
                          0
                          • DragoonD Dragoon has marked this topic as solved on

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved