Important: Please read the Qt Code of Conduct -

How to associate QListWidget with QPushButton ?

  • Hi ,

    My requirement is to have a DropDown menu like this :
    Screenshot 2020-07-11 at 11.49.23 AM.png

    For this i am thinking of having a QListWidget , but i want the QListWidget to appear only when i hover on the QPushButton/QToolButton . How can i connect the QListWidget with the PushButton/ToolButton ?

    Like for QMenu we have the method QPushButton::setMenu(QMenu *menu) to associate a menu with the push button . How to achieve this association for QListWidget ?


  • @Sougata1996

    • Why can't you use a QMenu for this, (I believe) menu items can have an icon at the left?

    • Every widget (e.g. including QPushButton) has, you can use that to detect when the mouse enters/hovers and display your menu widget then.

  • Lifetime Qt Champion

    you can use QMenu +
    for this

    alt text

      QWidgetAction *pWidgetAction = new QWidgetAction(0);
        auto Widget = new QListWidget();
        Widget->addItem("no 1");
        Widget->addItem("no 2");

    @JonB is right. If you don't need anything else from the item that icon + text then a normal menu should just do it.
    alt text

  • @JonB , @mrjj :
    Thanks for the quick response .
    Right , the only reason that i did not use QMenu for this is because i wanted a Vertical Scrollbar with the List . As far as i know Vertical Scrollbar is not supported for QMenu . Or is there a way to have a scrollbar for QMenu Items as well ?

    My requirement is that if there are more than 4 items in the DropDownList a Vertical Scrollbar should appear , which could facilitate the user to scroll down and view the rest of the items .

    Is there a way to do this by QMenu ?

  • Lifetime Qt Champion

    As far as i know, QMenu will not show scroll bars.
    Once upon a time you could do
    submenu->setStyleSheet("QMenu { menu-scrollable: 1; }");
    to have sub menus have scrollbars but not sure it still works :)
    It still there but has issues.
    Also draws funny on Win 10..

  • @mrjj : To display the scrollbar what i am thinking of doing is using QListWidget and customising the QListWidgetItems to be of QMenu type by using the method QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget) . The second parameter that i will pass will be of QMenu type .

    Is there any other solution to this , would like to know your opinion about this solution or any other suggestion that you have to overcome this limitation of QMenu .

  • Lifetime Qt Champion

    Do you really need QMenu on top of the items in QListWidget?
    The item can already have icon + text and you can get signal when you click it and then close
    the Menu so it acts like normal.

    Other solutions would require much more code. If you look at QCombobox, it displays a scroll area for its
    dropdown viewport so it's possible to hand create but then you must handle cases like click outside should close it and

  • @mrjj : Screenshot 2020-07-11 at 11.50.09 AM.png
    I agree the QListWidgetItem will have Icon and text both but to display a flyout as shown in this image i will require submenu right ? Or is there is way to have create a flyout like this even by nesting QListWidgetItems ?

  • Lifetime Qt Champion

    Ah you want to have sub menus..
    No, QListWidget cannot do that at all.

    I never tried using QMenu as item widget but try and see if it works.
    That would be the easiest since you require sub-menus.

  • Hi @mrjj , @SGaist : I was able to use QMenu as ItemWidget and it worked for me , but i am seeing a strange problem which is bugging me :

    Screenshot 2020-07-17 at 1.56.43 PM.png

    As you can see in the above image there is a gap between the highlight and the left border , that it causing a strange issue .

    On clicking in between the highlight and the border my row suddenly disappears :

    Screenshot 2020-07-17 at 1.56.24 PM.png

    One way i was thinking to overcome this might be if my selection covers the entire width of the ListItem instead of leaving any gap . But was not able to achieve it .

  • Lifetime Qt Champion

    Its hard to guess at.
    Maybe its the menu you hit.

  • @mrjj : On some debugging i understood the reason , what is happening is that on clicking outside the menu the list widget item tries to get focus and hides the static content of the QMenu which is inside list widget item . I have used the function setItemWidget() passing the listwidgetitem as first parameter and menu(with icon and text) as the second parameter . Though i could not find a solution to this .

  • Lifetime Qt Champion

    I would guess its the QMenu that hides it self as it normally does when you "click outside"
    It might be possible to catch that using an event filter
    but i didnt look into that.

  • This post is deleted!

Log in to reply