Keyword: qt,qcombobox,qlineedit,qlistview
Os:windows 7
Problem Link: qcombobox:can We make the entire ComboBox clickable, not just the dropdown button (arrow) itself?
To make the entire ComboBox clickable, add a Qtcomboboxbutton class to inherit Qlineedit, ShowPopup in Mousepressevent.
classQtcomboboxbutton: Publicqlineedit{Q_object Public: Qtcomboboxbutton (Qwidget*parent =0); ~Qtcomboboxbutton ();protected: voidMousepressevent (Qmouseevent *);}; Qtcomboboxbutton::qtcomboboxbutton (Qwidget*parent/*= 0*/): Qlineedit (parent) {}qtcomboboxbutton::~Qtcomboboxbutton () {}voidQtcomboboxbutton::mousepressevent (Qmouseevent *e) {Qcombobox* Combo = dynamic_cast<qcombobox*>(parent ()); if(Combo) Combo-showPopup ();}
The use of Qcombobox+qlineedit+qlistview is as follows:
qcombobox* MyCombo =NewQcombobox; MyCombo->setview (NewQlistview ()); MyCombo->seteditable (true); MyCombo->setlineedit (NewQtcomboboxbutton (m_panswer)); MyCombo->lineedit ()->setreadonly (true); MyCombo->additem ("Option1"); MyCombo->additem ("Option2"); MyCombo->additem ("Option3"); MyCombo->setmaxvisibleitems (mycombo->count ()); MyCombo->lineedit ()->settext ("Please select"); QString Arrowimagepath="C:\arrow.png"; MyCombo->setstylesheet ("qcombobox {font-family: \ "arial\"; font-size:13px; padding:3px 0x 3px 5px;}" "Qcombobox::d rop-down {subcontrol-origin:padding; subcontrol-position:top right; width:30 px; border:0px;}" "Qcombobox::d own-arrow {image:url ("+ Arrowimagepath +");}"); MyCombo->view ()->setstylesheet ("Qlistview {font-family: \ "arial\"; font-size:13px; outline:0px;}" "Qlistview::item {padding:3px 0x 3px 5px; border-width:0px;}" "qlistview::item:selected {background-color:rgb (144, 226);}");
UI effects such as:
Customizing QT Header-formatted flat drop-down box controls-qcombobox+qlineedit+qlistview