c++ - How to create a QPushButton with the text displayed over the icon? -


i trying create qpushbutton in project such text shows on top of custom button image or icon. tried below methods:

imagepath = path; qpixmap pixmap(imagepath); qicon buttonicon(pixmap); button->seticon(buttonicon); button->seticonsize(pixmap.rect().size()); button->setgeometry(0,0,height,width); button->setstylesheet(     "background-color: gray;"     "border: 1px solid black;"     "border-radius: "+qstring::number(radius)+"px;"     "color: lightgray; "     "font-size: 25px;"     ); 

when try use settext here, shows icon first , text on right. want text appear on top of icon.

i tried below method found online:

imagepath = path; button->setgeometry(0,0,height,width); button->setstylesheet("background-image: url(:/images/images/2adjacenttracksbutton.png));"                       "background-position: center center"); 

this 1 not accepting url path, hence not displaying image need on button.

how can solve this?

when comes manipulate button, may want own class, implement qabstractbutton. this:

class mybutton : public qabstractbutton {     q_object  public:     static mybutton* createbutton(qicon icon, qwidget *parent);     ~mybutton();      void settext(qstring);     void seticon(eicon);     void setorientation(qt::orientation);  protected :      mybutton(qwidget *parent);      // here, can reimplement event mousepressevent or paintevent  private :     qboxlayout*  m_buttonlayout;     qlabel*      m_iconlabel;     qicon        m_icon;     qlabel*      m_textlabel; } 

in .cpp :

mybutton::mybutton(qwidget *parent)     : qabstractbutton(parent) {         m_buttonlayout = new qboxlayout(qboxlayout::lefttoright, this);     m_buttonlayout->setalignment(qt::aligncenter);     m_buttonlayout->setcontentsmargins(0, 0, 0, 0);     m_buttonlayout->setspacing(1);      m_iconlabel = new qlabel(this);     m_iconlabel->setalignment(qt::aligncenter);     m_buttonlayout->addwidget(m_iconlabel);      m_textlabel = new qlabel(this);     m_textlabel->setalignment(qt::aligncenter);     m_buttonlayout->addwidget(m_textlabel);     //m_textlabel->hide(); }  mybutton* mybutton::createbutton(qicon icon, qwidget *parent) {     mybutton* pbutton = new mybutton(parent);     pbutton->seticon(icon);      return pbutton; }  void mybutton::settext(qstring text) {     m_textlabel->setvisible(!text.isempty());     m_textlabel->settext(text);     qabstractbutton::settext(text); }  void mybutton::seticon(qicon icon) {     m_icon = icon;     m_iconlabel->setvisible(true); }  void mybutton::setorientation(qt::orientation orientation) {     if (orientation == qt::horizontal)         m_buttonlayout->setdirection(qboxlayout::lefttoright);     else         m_buttonlayout->setdirection(qboxlayout::toptobottom); } 

and can create button icon calling static method:

mybutton* button = mybutton::createbutton(mybuttonicon, this); 

it basic example gave you, , not sure work (this thing did time ago) can give shot. hope helps !


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -