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. How to associate QListWidget with QPushButton ?
QtWS25 Last Chance

How to associate QListWidget with QPushButton ?

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 1.6k Views
  • 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
    Sougata1996
    wrote on last edited by
    #1

    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 ?

    Thanks

    JonBJ 1 Reply Last reply
    0
    • S Sougata1996

      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 ?

      Thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 https://doc.qt.io/qt-5/qwidget.html#enterEvent, you can use that to detect when the mouse enters/hovers and display your menu widget then.

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        you can use QMenu +
        https://doc.qt.io/qt-5/qwidgetaction.html
        for this

        alt text

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

        update:
        @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

        1 Reply Last reply
        2
        • S Offline
          S Offline
          Sougata1996
          wrote on last edited by
          #4

          @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 ?

          mrjjM 1 Reply Last reply
          0
          • S Sougata1996

            @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 ?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @Sougata1996
            Hi
            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 :)
            update
            It still there but has issues.
            https://bugreports.qt.io/browse/QTBUG-48959
            Also draws funny on Win 10..

            1 Reply Last reply
            2
            • S Offline
              S Offline
              Sougata1996
              wrote on last edited by
              #6

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

              mrjjM 1 Reply Last reply
              0
              • S Sougata1996

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Sougata1996
                Hi
                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
                such.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sougata1996
                  wrote on last edited by
                  #8

                  @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 ?

                  mrjjM 1 Reply Last reply
                  0
                  • S Sougata1996

                    @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 ?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Sougata1996
                    Hi
                    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.

                    1 Reply Last reply
                    2
                    • S Offline
                      S Offline
                      Sougata1996
                      wrote on last edited by
                      #10

                      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 .

                      mrjjM 1 Reply Last reply
                      0
                      • S Sougata1996

                        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 .

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Sougata1996
                          wrote on last edited by Sougata1996
                          #12

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

                          mrjjM 1 Reply Last reply
                          0
                          • S Sougata1996

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

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Sougata1996
                            Hi
                            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
                            https://doc.qt.io/qt-5/eventsandfilters.html
                            but i didnt look into that.

                            1 Reply Last reply
                            1
                            • S Offline
                              S Offline
                              Sougata1996
                              wrote on last edited by
                              #14
                              This post is deleted!
                              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