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. Forcing size update on first view of QListWidget
Qt 6.11 is out! See what's new in the release blog

Forcing size update on first view of QListWidget

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 8.0k 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.
  • S Offline
    S Offline
    sbmiller
    wrote on last edited by
    #1

    Hello,
    On the project I'm working on, I'm running into an issue with the geometry updating on a QListWidget. Basically the current flow is this:

    • User right-clicks to display a context menu
    • User hovers over a QMenu in the context menu, which causes a QListWidget to be populated with QListWidgetItem objects, which is then shown as a child of the QMenu.
    • On the first creation of the QListWidget, the default size is used, which frequently cuts off the names of the QListWidgetItems populating the QListWidget. However, if the user then moves the mouse away from the menu and then back again, the list has been resized to fit the contents.

    Ideally, the QListWidget would be resized appropriately after population, but prior to being displayed, but I have not been able to figure out how to accomplish this. I've tried various means of adjusting/updating the geometry, and have tried calling qApp->processEvents() just to make sure that any events being issued aren't just being queued until after the first display, but with no success. Please let me know if there is something I am missing or if there is a better strategy. Also, I can provide more information as necessary. Thanks.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Peppy
      wrote on last edited by
      #2

      I am missing your problem...It's cutting, but it shouldn't ?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sbmiller
        wrote on last edited by
        #3

        That is correct. The QListWidget is not resizing in order to fit the width of the QListWidgetItem objects that are added to it. For my program, the list is being populated with names of files loaded, so if a file has a long name it is cut off when the list is first shown.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Peppy
          wrote on last edited by
          #4

          Mhm...I think about some kind of hack/cheat...

          There is many ways how to fix it

          • Sort from longest item to the shortest (not so good, I think)
          • Check out, which is the longest and set up the width as you need
            ...
          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #5

            Been there before!! My approach was...

            @void TargetsWindow::adjustTableColumnsToContents()
            {
            int horizontalHeaderHeight = 37;

            if((ui->tableWidget->rowCount() >= 0) && (ui->tableWidget->rowCount() <= 6)){
                //no scrollbar (0..n targets)
                ui->tableWidget->horizontalHeaderItem(0)->setSizeHint(QSize(287,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(1)->setSizeHint(QSize(90,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(2)->setSizeHint(QSize(150,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(3)->setSizeHint(QSize(150,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(4)->setSizeHint(QSize(100,horizontalHeaderHeight));
            }else{
                //vertical scrollbar needed (n+1..m targets)
                ui->tableWidget->horizontalHeaderItem(0)->setSizeHint(QSize(271,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(1)->setSizeHint(QSize(90,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(2)->setSizeHint(QSize(150,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(3)->setSizeHint(QSize(150,horizontalHeaderHeight));
                ui->tableWidget->horizontalHeaderItem(4)->setSizeHint(QSize(100,horizontalHeaderHeight));
            }
            ui->tableWidget->resizeColumnsToContents();
            

            }@

            Some relevant tableWidget's properties (Qt Designer):

            • sizePolicy = fixed, fixed
            • selectionMode = SingleSelection
            • selectionBehavior = SelectRows
            • wordWrap = True
            • horizontalHeaderVisible = True (any other option about headers, default or false)

            Hope it helps!!

            --

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Peppy
              wrote on last edited by
              #6

              This is solution too ;) :-) :-D

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sbmiller
                wrote on last edited by
                #7

                I've tried something similar to no avail - I can't do iytenorio's solution directly, since I'm not using a table in this case, but basically I've used font metrics to determine the longest string and am trying to resize the parent widget appropriately.
                @
                QString str = QString::fromStdString(displayName);
                QSize textSize = mpElementList->fontMetrics().size(Qt::TextSingleLine, str);
                if (textSize.width() > mpElementList->width())
                {
                mpElementList->resize(textSize);
                }@
                Still no luck. Again I'm making sure to run qApp->processEvents() to ensure that the event queue is flushed, so I'm not sure where the hiccup is.

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  Maybe posting some actual code, or something essentially similar, could help. Or maybe a screen capture. I still think the problem involves height, width and sizeHint, but without my development system at hand...

                  best of luck!

                  --

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sbmiller
                    wrote on last edited by
                    #9

                    "Here":http://i.imgur.com/Daexy.png is a screenshot of the issue. The top is what happens the first time the list widget is shown, the bottom is what it looks like every time afterwards. Ideally I would like to have the bottom part of the screenshot occur everytime.

                    As far as code, there's not much more than what I showed. The code iterates over all of the items to be put into the QListWidget, creating a QListWidgetItem from it, then it gets pushed into the list. Based on the screenshot, it seems like the size policy and hints are being set correctly, but just aren't being enforced until after the first time it's shown. I'm not sure what need to go in to make it work correctly.

                    1 Reply Last reply
                    0

                    • Login

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