How to associate QListWidget with QPushButton ?
-
wrote on 12 Jul 2020, 06:19 last edited by
Hi ,
My requirement is to have a DropDown menu like this :
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
-
Hi ,
My requirement is to have a DropDown menu like this :
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
wrote on 12 Jul 2020, 06:32 last edited by-
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.
-
-
Lifetime Qt Championwrote on 12 Jul 2020, 06:35 last edited by mrjj 7 Dec 2020, 06:40
Hi
you can use QMenu +
https://doc.qt.io/qt-5/qwidgetaction.html
for thisQWidgetAction *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.
-
wrote on 12 Jul 2020, 06:49 last edited by
@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 ?
-
@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 Championwrote on 12 Jul 2020, 06:54 last edited by mrjj 7 Dec 2020, 06:57@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.. -
wrote on 12 Jul 2020, 07:03 last edited by
@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 .
-
@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 .
@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. -
wrote on 12 Jul 2020, 07:20 last edited by
@mrjj :
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 ? -
@mrjj :
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 ?@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. -
wrote on 17 Jul 2020, 08:33 last edited by
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 :
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 :
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 .
-
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 :
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 :
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 .
@Sougata1996
Hi
Its hard to guess at.
Maybe its the menu you hit. -
wrote on 20 Jul 2020, 07:46 last edited by 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 .
-
@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 .
@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. -
wrote on 14 Aug 2020, 09:50 last edited byThis post is deleted!