How to create a customised QComboBox
-
I'm looking to create a widget which resembles a QComboBox in almost every respect except that the drop-down list should only appear when the user clicks in the immediate vicinity of the arrow symbol. If the user clicks anywhere else within the widget, I'd like signals similar to the 'pressed' and 'released' signals of QPushButton to be emitted.
My best idea so far is to overlay a QToolButton (with a down-arrow) onto the right-hand side of a QPushButton, using a style sheet to get rid of the QToolButton's border and make it look an integral part of the QPushButton. I can then have the QToolButton 'clicked' signal connected to a slot that displays a QListView under the whole width of the QPushButton. This looks viable - in fact, on-screen it looks exactly how I want it to - but I'm scratching my head as to how to get the QListView displayed as a popup in the proper location.
Is there a straightforward way to do this? Or is there an even simpler approach to achieve the overall objective, that I've failed to find in the documentation so far?
-
QComboBox::showPopup() would show the drop down list.
You might call this function from the place where you want to show the drop down list.
ui->comboBox->showPopup();
This is based on my understanding of the problem.
Please let me know if it helps.
--Kumara
-
Thank you.
In fact, though, I don't think I can use a QComboBox unless there is a way of preventing the list from appearing whenever I press the button; what I want is for the list to appear only when the arrow is pressed, and QComboBox doesn't seem (unless I've missed something) to be able to distinguish between the arrow being clicked and anywhere else being clicked.
Therefore, what I think I want to do is to make a QListView appear when I press a QPushButton.
-
I've come up with a better solution.
Rather than putting a QToolBox in front of a QPushButton, it's better to put a QPushButton in front of a QComboBox, with the QPushButton covering all of the QComboBox except for the arrow on the right. This way, I retain all of the functionality of the QComboBox without having to reimplement it manually using QListView.
1/4