Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QListWidget row size with a custom QListWidgetItem
Forum Updated to NodeBB v4.3 + New Features

QListWidget row size with a custom QListWidgetItem

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
17 Posts 2 Posters 19.7k Views 1 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.
  • C Offline
    C Offline
    canellas
    wrote on 5 Feb 2017, 17:11 last edited by canellas 2 May 2017, 17:12
    #1

    Hi!

    Using Qt Designer, I created an Item inheriting class from QWidget, and added a few widgtes.
    Then, again using Qt Designer, I created a MainWindow from QMainWindow, and added a QListWidget object called list, and a QPushButton called pushButton.

    On MainWindow::MainWindow(QWidget *parent), I added to the default code:

    ui->list->setSizeAdjustPolicy(QListWidget::AdjustToContents);
    

    On void MainWindow::on_pushButton_clicked(), I wrote:

    Item *my_item = new Item(this);

    
    QListWidgetItem * item = new QListWidgetItem();
    
    item->setSizeHint(QSize(0, 400));

    
    ui->list->addItem(item);
    
    ui->list->setItemWidget(item, my_item );
    

    Testing on the Linux Ubuntu running on my notebook, it all goes well, but when I run on my Android Moto G4 cell phone, the Item objects inserted in list are never displayed correctly. Using 400 in setSizeHint makes the widgets to be displayed, but cut at the bottom. Using, say, 100, makes the widgets to be squeezed, impossible to read.

    Does anyone have any idea on how to make it work?

    Thanks!!

    K 1 Reply Last reply 5 Feb 2017, 20:28
    0
    • C canellas
      5 Feb 2017, 17:11

      Hi!

      Using Qt Designer, I created an Item inheriting class from QWidget, and added a few widgtes.
      Then, again using Qt Designer, I created a MainWindow from QMainWindow, and added a QListWidget object called list, and a QPushButton called pushButton.

      On MainWindow::MainWindow(QWidget *parent), I added to the default code:

      ui->list->setSizeAdjustPolicy(QListWidget::AdjustToContents);
      

      On void MainWindow::on_pushButton_clicked(), I wrote:

      Item *my_item = new Item(this);

    
      QListWidgetItem * item = new QListWidgetItem();
    
      item->setSizeHint(QSize(0, 400));

    
      ui->list->addItem(item);
    
      ui->list->setItemWidget(item, my_item );
      

      Testing on the Linux Ubuntu running on my notebook, it all goes well, but when I run on my Android Moto G4 cell phone, the Item objects inserted in list are never displayed correctly. Using 400 in setSizeHint makes the widgets to be displayed, but cut at the bottom. Using, say, 100, makes the widgets to be squeezed, impossible to read.

      Does anyone have any idea on how to make it work?

      Thanks!!

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 5 Feb 2017, 20:28 last edited by
      #2

      Does your widget provide a correct values for width(), height() and sizeHint() (which by the way is only a hint)?

      Read and abide by the Qt Code of Conduct

      C 1 Reply Last reply 5 Feb 2017, 20:44
      0
      • K kshegunov
        5 Feb 2017, 20:28

        Does your widget provide a correct values for width(), height() and sizeHint() (which by the way is only a hint)?

        C Offline
        C Offline
        canellas
        wrote on 5 Feb 2017, 20:44 last edited by
        #3

        @kshegunov What would be correct values, if I do not know the size or resolution of the display?

        I mean, the app can run on any cell phone, iPhone models included. I expected the QListWidgetItem to be automatically resized to fit the list, and along with it the Item instance "inside" it. At least that's what I see when it runs on my Ubuntu Linux.

        Am I missing something?

        Thanks

        K 1 Reply Last reply 5 Feb 2017, 20:46
        0
        • C canellas
          5 Feb 2017, 20:44

          @kshegunov What would be correct values, if I do not know the size or resolution of the display?

          I mean, the app can run on any cell phone, iPhone models included. I expected the QListWidgetItem to be automatically resized to fit the list, and along with it the Item instance "inside" it. At least that's what I see when it runs on my Ubuntu Linux.

          Am I missing something?

          Thanks

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 5 Feb 2017, 20:46 last edited by
          #4

          @canellas said in QListWidget row size with a custom QListWidgetItem:

          What would be correct values, if I do not know the size or resolution of the display?

          How does the display figure here? You need to provide two numbers - width and height - to the layout engine so it know how big to draw the rows and columns of the list widget. You calculate the width and height based on the contents of the widget and/or the size policy that's set. I don't know what widget you're creating so it's hard to say how exactly this should be calculated, but I hope you understand what I meant.

          Read and abide by the Qt Code of Conduct

          C 1 Reply Last reply 5 Feb 2017, 21:05
          0
          • K kshegunov
            5 Feb 2017, 20:46

            @canellas said in QListWidget row size with a custom QListWidgetItem:

            What would be correct values, if I do not know the size or resolution of the display?

            How does the display figure here? You need to provide two numbers - width and height - to the layout engine so it know how big to draw the rows and columns of the list widget. You calculate the width and height based on the contents of the widget and/or the size policy that's set. I don't know what widget you're creating so it's hard to say how exactly this should be calculated, but I hope you understand what I meant.

            C Offline
            C Offline
            canellas
            wrote on 5 Feb 2017, 21:05 last edited by canellas 2 May 2017, 21:10
            #5

            @kshegunov The Item class is like this: alt text.

            The width is set to 278, and height to 138. sizePolicy.HorizontalPolicy and sizePolicy.HorizontalPolicy are set to Prefered.

            And this is how the screen looks like in my cell:

            alt text

            K 1 Reply Last reply 5 Feb 2017, 21:07
            0
            • C canellas
              5 Feb 2017, 21:05

              @kshegunov The Item class is like this: alt text.

              The width is set to 278, and height to 138. sizePolicy.HorizontalPolicy and sizePolicy.HorizontalPolicy are set to Prefered.

              And this is how the screen looks like in my cell:

              alt text

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 5 Feb 2017, 21:07 last edited by
              #6

              You have to upload the images to an external site (e.g postimage.org).
              However, with that size policy you'd get multiple inquiries about the sizes of your widget by Qt so it can calculate the correct size. If it's a fixed size, you need to set the correct policy.

              Read and abide by the Qt Code of Conduct

              C 1 Reply Last reply 5 Feb 2017, 21:22
              0
              • K kshegunov
                5 Feb 2017, 21:07

                You have to upload the images to an external site (e.g postimage.org).
                However, with that size policy you'd get multiple inquiries about the sizes of your widget by Qt so it can calculate the correct size. If it's a fixed size, you need to set the correct policy.

                C Offline
                C Offline
                canellas
                wrote on 5 Feb 2017, 21:22 last edited by canellas 2 May 2017, 21:23
                #7

                @kshegunov

                The Item class
                alt text

                The screen on my cel, with sizePolicy set to Fixed in both dimensions:
                alt text

                And with setSizeHint(QSize(0, 150));

                K 1 Reply Last reply 5 Feb 2017, 21:31
                0
                • C canellas
                  5 Feb 2017, 21:22

                  @kshegunov

                  The Item class
                  alt text

                  The screen on my cel, with sizePolicy set to Fixed in both dimensions:
                  alt text

                  And with setSizeHint(QSize(0, 150));

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 5 Feb 2017, 21:31 last edited by
                  #8

                  Okay, wait a bit. So you have a compound widget, does that widget have a layout? If it does you shouldn't need to do anything (even set the hint).

                  Read and abide by the Qt Code of Conduct

                  C 1 Reply Last reply 5 Feb 2017, 21:35
                  0
                  • K kshegunov
                    5 Feb 2017, 21:31

                    Okay, wait a bit. So you have a compound widget, does that widget have a layout? If it does you shouldn't need to do anything (even set the hint).

                    C Offline
                    C Offline
                    canellas
                    wrote on 5 Feb 2017, 21:35 last edited by canellas 2 May 2017, 21:36
                    #9

                    @kshegunov Yes, The first TextLabel and the text box by its side are grouped in a horizontal layout, as the Text Label and the text box below, and the whole Item class, sub class of QWidget, has a vertical layout.

                    I wonder if the QListWidgetItem, or the list object should, somehow, have a layout defined.

                    K 1 Reply Last reply 5 Feb 2017, 21:36
                    0
                    • C canellas
                      5 Feb 2017, 21:35

                      @kshegunov Yes, The first TextLabel and the text box by its side are grouped in a horizontal layout, as the Text Label and the text box below, and the whole Item class, sub class of QWidget, has a vertical layout.

                      I wonder if the QListWidgetItem, or the list object should, somehow, have a layout defined.

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 5 Feb 2017, 21:36 last edited by kshegunov 2 May 2017, 21:37
                      #10

                      Hm that's odd. If you have a vertical layout for the widget, I'd set the policy to expanding both directions. Aside from that it might be some peculiarity of the view ... but I haven't encountered it by now.

                      I wonder if the QListWidgetItem, or the list object should, somehow, have a layout defined.

                      It has a special way of laying out the items (implemented for the view itself).

                      Read and abide by the Qt Code of Conduct

                      C 1 Reply Last reply 5 Feb 2017, 21:42
                      0
                      • K kshegunov
                        5 Feb 2017, 21:36

                        Hm that's odd. If you have a vertical layout for the widget, I'd set the policy to expanding both directions. Aside from that it might be some peculiarity of the view ... but I haven't encountered it by now.

                        I wonder if the QListWidgetItem, or the list object should, somehow, have a layout defined.

                        It has a special way of laying out the items (implemented for the view itself).

                        C Offline
                        C Offline
                        canellas
                        wrote on 5 Feb 2017, 21:42 last edited by
                        #11

                        @kshegunov I set the sizePolicy to Expanding in horizontal and vertical dimensions, but the result was the same. I tried to remove the setSizeHint(QSize(0, 150));,and also the setSizeAdjustPolicy(QListWidget::AdjustToContents);, but with the same result.

                        K 1 Reply Last reply 5 Feb 2017, 21:43
                        0
                        • C canellas
                          5 Feb 2017, 21:42

                          @kshegunov I set the sizePolicy to Expanding in horizontal and vertical dimensions, but the result was the same. I tried to remove the setSizeHint(QSize(0, 150));,and also the setSizeAdjustPolicy(QListWidget::AdjustToContents);, but with the same result.

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 5 Feb 2017, 21:43 last edited by
                          #12

                          It's possible it may be a bug too. Can you prepare a minimal example that reproduces the behaviour? Btw, do you experience this only on the mobile device, when the screen is small?

                          Read and abide by the Qt Code of Conduct

                          C 1 Reply Last reply 5 Feb 2017, 21:46
                          0
                          • K kshegunov
                            5 Feb 2017, 21:43

                            It's possible it may be a bug too. Can you prepare a minimal example that reproduces the behaviour? Btw, do you experience this only on the mobile device, when the screen is small?

                            C Offline
                            C Offline
                            canellas
                            wrote on 5 Feb 2017, 21:46 last edited by
                            #13

                            @kshegunov Only in the mobile. When I run on a 14" notebook display, it works as expected, i.e., the row height and width expand and reduce according to the size of the window.

                            I have the example ready. Where should I send it to?

                            In time, thanks a lot for your time!

                            K 1 Reply Last reply 5 Feb 2017, 21:52
                            0
                            • C canellas
                              5 Feb 2017, 21:46

                              @kshegunov Only in the mobile. When I run on a 14" notebook display, it works as expected, i.e., the row height and width expand and reduce according to the size of the window.

                              I have the example ready. Where should I send it to?

                              In time, thanks a lot for your time!

                              K Offline
                              K Offline
                              kshegunov
                              Moderators
                              wrote on 5 Feb 2017, 21:52 last edited by
                              #14

                              Upload it where it's convenient. I'd try on my machine but I don't have any device on hand, so it might be a very limited test. For now my advice is to wait a bit so our other users can see and possibly offer insight, but you may need to post it (the example and description) as a bugreport. Don't hold your breath of it being fixed though, the widgets module isn't priority ...

                              Read and abide by the Qt Code of Conduct

                              C 1 Reply Last reply 5 Feb 2017, 21:58
                              0
                              • K kshegunov
                                5 Feb 2017, 21:52

                                Upload it where it's convenient. I'd try on my machine but I don't have any device on hand, so it might be a very limited test. For now my advice is to wait a bit so our other users can see and possibly offer insight, but you may need to post it (the example and description) as a bugreport. Don't hold your breath of it being fixed though, the widgets module isn't priority ...

                                C Offline
                                C Offline
                                canellas
                                wrote on 5 Feb 2017, 21:58 last edited by
                                #15

                                @kshegunov I uploaded the project to https://www.dropbox.com/s/tx88huu23d5cfcm/untitled6.tar.gz?dl=0. It is a tar.gz file, if you do not mind.

                                Well, if it is a bug, that means big trouble to me, as I am using something similar in a customer application.

                                Again, thanks for your help and time!

                                K 1 Reply Last reply 5 Feb 2017, 22:10
                                0
                                • C canellas
                                  5 Feb 2017, 21:58

                                  @kshegunov I uploaded the project to https://www.dropbox.com/s/tx88huu23d5cfcm/untitled6.tar.gz?dl=0. It is a tar.gz file, if you do not mind.

                                  Well, if it is a bug, that means big trouble to me, as I am using something similar in a customer application.

                                  Again, thanks for your help and time!

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 5 Feb 2017, 22:10 last edited by kshegunov 2 May 2017, 22:11
                                  #16

                                  I took a peek at your code and I have artifacts when resizing the widget. But it's my bad. You can't do this the way you're trying to. Here:

                                  This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.

                                  So what you need to do is to rework your code to use a list view with a delegate.

                                  Read and abide by the Qt Code of Conduct

                                  C 1 Reply Last reply 6 Feb 2017, 01:48
                                  0
                                  • K kshegunov
                                    5 Feb 2017, 22:10

                                    I took a peek at your code and I have artifacts when resizing the widget. But it's my bad. You can't do this the way you're trying to. Here:

                                    This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.

                                    So what you need to do is to rework your code to use a list view with a delegate.

                                    C Offline
                                    C Offline
                                    canellas
                                    wrote on 6 Feb 2017, 01:48 last edited by
                                    #17

                                    @kshegunov I understand... well, I'll try, once more, to understand views, models and delegates, and how Qt implement them. 8)

                                    The examples that come with QtCreator do not, as far as I could see, show how to insert in a Q*View a custom class object, and searching the web, I could not find an example either.

                                    Do you know any link that shows how to implement it? I would not mind to take a few days to do it from scratch on my own, but I do not have a lot of time to fix the app.

                                    Anyway, thanks a lot!!!

                                    1 Reply Last reply
                                    0

                                    1/17

                                    5 Feb 2017, 17:11

                                    • Login

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