c++ - QMenu: How to customize the menu items of QMenu -


i want build dropdown list control qpushbutton , qmenu below:

qpushbutton* menubt = new qpushbutton("please select"); menubt->setflat(true); qmenu* menu = new qmenu(); menubt->setmenu(menu); qwidgetaction* wa1 = new qwidgetaction(menu); qlabel* l1 = new qlabel("option1"); wa1->setdefaultwidget(l1); menu->addaction(wa1); qwidgetaction* wa2 = new qwidgetaction(menu); qlabel* l2 = new qlabel("option2"); wa2->setdefaultwidget(l2); menu->addaction(wa2); menu->setstylesheet("qmenu::item {font-family: \"arial\"; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"     "qmenu::item:hover {background-color: rgb(0, 0, 255);}"); menubt->setstylesheet("qpushbutton {font-family: \"arial\"; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}"); 

i set font , hover background color menu items setstylesheet, seems doesn't work. how make font , hover background color work on menu items?

answer:

class qtdropdownbutton : public qpushbutton {     q_object public:     qtdropdownbutton(qstring text, qwidget *parent = nullptr);      void additem(qstring text);      protected slots:         void menuabouttoshow();  private:     qmenu* menu_; };      qtdropdownbutton::qtdropdownbutton(qstring text, qwidget *parent) :     qpushbutton(text, parent) {     setflat(true);     menu_ = new qmenu();     setmenu(menu_);      connect(menu_, signal(abouttoshow()), this, slot(menuabouttoshow()));      setstylesheet("font-family: arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);");     menu_->setstylesheet("qmenu::item {font-family: arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"         "qmenu::item:selected {background-color: rgb(0, 255, 255);}"         "qlabel {font-family: arial; font-size: 13pt;}"         "qlabel:hover {background-color: rgb(0, 0, 255);}"); }  void qtdropdownbutton::additem(qstring text) {     if(!menu_)         return;      qwidgetaction* wa1 = new qwidgetaction(menu_);     qlabel* l1 = new qlabel(text);     wa1->setdefaultwidget(l1);     menu_->addaction(wa1); }  void qtdropdownbutton::menuabouttoshow() {     if(menu_)         menu_->setfixedwidth(this->width()); } 

to set font-family don't need put quotes around arial. believe prevents style sheet parsing correctly.

a side note: @ moment menubt styled, other buttons default buttons. change button style buttons in menu, move style setstylesheet() call of menu this:

menu->setstylesheet("qmenu::item {font-family: arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}" + "qmenu::item:hover {background-color: rgb(0, 0, 255);}" + "qpushbutton {font-family: arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}"); 

but if want 1 button different, correct call setstylesheet() on it, can omit selector, this:

menubt->setstylesheet("font-family: arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);"); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -