stylesheet - Applying a Qt style sheet to a QPushButton and a QToolButton -
i'm trying create widget have similar behaviour qcombobox, different behaviours depending on whether user clicks on arrow or elsewhere on widget. starters, i'm modelling widget wide qpushbutton qtoolbutton superimposed on it. once have looking want to, i'll add qlistview provide drop-down menu , make appear whenever unit presses qtoolbutton (but not qpushbutton). basic test code far looks this:
#include <qtwidgets/qapplication> #include <qdialog> #include <qpushbutton> #include <qtoolbutton> #include <qpushbutton> #include <qgridlayout> class stylesheettest : public qdialog { q_object public: stylesheettest(qwidget * parent = nullptr) : qdialog(parent) { // create widgets , layouts itslayout = new qgridlayout; itsmainbutton = new qpushbutton; itsdropdownbutton = new qtoolbutton; // place widgets in layout: itslayout -> addwidget(itsmainbutton, 0, 0, 1, 2); itslayout -> addwidget(itsdropdownbutton, 0, 1, 1, 1); itslayout -> setspacing(0); itslayout -> setcolumnstretch(0, 1); itsmainbutton -> setsizepolicy(qsizepolicy::expanding, qsizepolicy::fixed); itsdropdownbutton -> setsizepolicy(qsizepolicy::fixed, qsizepolicy::fixed); itsdropdownbutton -> setarrowtype(qt::downarrow); // formally appoint layout setlayout(itslayout); // stylesheet settings setstylesheet("qtoolbutton { border: none; } "); // give button label itsmainbutton -> settext("an extraordinarily long label button"); } private: qpushbutton *itsmainbutton; qtoolbutton *itsdropdownbutton; qgridlayout *itslayout; }; int main(int argc, char *argv[]) { qapplication a(argc, argv); stylesheettest t; t.show(); return a.exec(); } #include "main.moc" this gets me of way there; resulting button looks this:

it looks qcombobox, independent behaviours of qpushbutton , qtoolbutton, want. however, i'd make better:
- i'd set bit of margin on right-hand side of qpushbutton, text doesn't overlap arrow
- i'd arrow bit smaller
everything know style sheets (which admittedly isn't much) tells me achieve need resembling
// stylesheet settings setstylesheet("qpushbutton { padding-right: 20px; }"); setstylesheet("qtoolbutton { border: none; } " "qtoolbutton::down-arrow { width: 8px; height: 8px; }" ); but in fact result of mess:

further experimentation shows trying set 'padding-right' parameter of qpushbutton value @ leads vertical height getting squeezed in way, while attempts modify size of down-arrow fail. i'm doing wrong, can't see what. hints gratefully received.
update
i've decided better approach is, rather using qtoolbutton in front of qpushbutton , having manually implement qlistview behaviour, put qpushbutton in front of qcombobox leacing latter's arrow exposed. this:
#include <qtwidgets/qapplication> #include <qdialog> #include <qpushbutton> #include <qcombobox> #include <qgridlayout> class stylesheettest : public qdialog { q_object public: stylesheettest(qwidget * parent = nullptr) : qdialog(parent) { // create widgets , layouts itslayout = new qgridlayout; itsmainbutton = new qpushbutton; itshiddencombobox = new qcombobox; // place widgets in layout: itslayout -> addwidget(itshiddencombobox, 0, 0, 1, 3); itslayout -> addwidget(itsmainbutton, 0, 0, 1, 2); itslayout -> setspacing(0); itslayout -> setcolumnstretch(0, 1); itslayout -> setcolumnminimumwidth(2, 20); itsmainbutton -> setsizepolicy(qsizepolicy::expanding, qsizepolicy::minimum); itshiddencombobox -> setsizepolicy(qsizepolicy::expanding, qsizepolicy::minimum); setsizepolicy(qsizepolicy::expanding, qsizepolicy::maximum); // formally appoint layout setlayout(itslayout); // stylesheet settings setstylesheet("qpushbutton { border: none; padding-left: 10px; } "); itsmainbutton -> settext("an extraordinarily long label button"); } private: qpushbutton *itsmainbutton; qcombobox *itshiddencombobox; qgridlayout *itslayout; }; int main(int argc, char *argv[]) { qapplication a(argc, argv); stylesheettest t; t.show(); return a.exec(); } #include "main.moc" the 1 shortcoming of solution that, having eliminated border qpushbutton, no longer responds visibly being clicked (even though usual signals sent). has been documented elsewhere: see here. happens can cope this, ultimate intention pressing qpushbutton cause change of cursor , make obvious enough user button has been clicked.
Comments
Post a Comment