Forcing size update on first view of QListWidget



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



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



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



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


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

    --



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



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



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

    --



  • "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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.