[solved] How to remove the sizeHint() previously applied?



  • Hello there :) In my listwidget items I have set a custom size hint. After some user actions I want to remove this sizeHint and go back to the default one.

    More specifically, I want to be able to convert my list view mode from IconMode to ListMode. My listwidget items, when IconMode is enabled have a custom size hint. When the user switches back to the ListMode I want to revert back to the default sizeHint. As I saw, the default sizeHint is QSize(-1, -1), but, if I set every item to this sizeHint, then nothing shows up (the items seem to disappear).

    In case this is helpful, when I am at IconMode, my items have icon and when I switch to the ListMode I make sure to remove this icon via:
    @ui->listWidget->item(i)->setIcon(QIcon(""));@

    Can someone point me to the right direction?



  • Wouldn't it make more sense to use different delegates instead? Changing your actual data to change the display mode doesn't sound like the best approach...



  • [quote author="Andre" date="1347788270"]Wouldn't it make more sense to use different delegates instead? Changing your actual data to change the display mode doesn't sound like the best approach...[/quote]

    It is a quick one and it works just fine :)

    Soooo, the answer is: You cannot do it?



  • I did not say that. What you could try, is this:
    @
    ui->listWidget->item(i)->setData(Qt::SizeHintRole, QVariant());
    @

    Note that I did not try this myself, so it might not work. Also note that you should check if item(i) returns a valid pointer before dereferencing it.



  • [quote author="Andre" date="1347806462"]I did not say that. What you could try, is this:
    @
    ui->listWidget->item(i)->setData(Qt::SizeHintRole, QVariant());
    @

    Note that I did not try this myself, so it might not work. Also note that you should check if item(i) returns a valid pointer before dereferencing it.[/quote]

    It works perfectly. How did you find this solution? I couldn't locate it at the documentation.

    Thanks a lot!



  • It comes from understanding the model/view structure. QVariant() is the default non-valid value that is used in the model/view structure of Qt. You might read about that in the documentation for [[doc:QAbstractItemModel]]. What that call does is reset the SizeHint role to the default 'invalid' value, allowing the default delegate to take over that work again. Recognize that QListWidget is just a thin layer around a QStandardItemModel (or something a lot like it) and QListView.

    Anyway: you're welcome!


Log in to reply
 

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