c++ - Qt - How to change the size of QRadiobutton while the size of window is changing -
i trying let size of qradiobutton
change size of window. need when adjusting size of window become bigger (or smaller), qradiobutton
should adjust become bigger (or smaller).
i tried use method used qpushbutton
, found doesn't work (check pic1 , pic2 @ bottom). size of qradiobutton
stays same.
besides, i've tried setstylesheet
qradiobutton
, clicking on picture invalid, not result want (clicking on picture should valid in case). know how should modify code. thanks!
here of related code:
.h file
qradiobutton *funcsourceorientedbutton; qpushbutton *funclockbutton;
.cpp file
qpixmap pixmap(":/images/function/sourceoriented.png"); qicon buttonicon(pixmap); funcsourceorientedbutton->seticon(buttonicon); funcsourceorientedbutton->seticonsize(function_oriented_button_size); funcsourceorientedbutton->setminimumsize(function_oriented_button_size); funcsourceorientedbutton->setsizepolicy(qsizepolicy::expanding, qsizepolicy::expanding); funclockbutton->setstylesheet("border-image: url(:/images/function/wlock.png);"); funclockbutton->setminimumsize(function_button_size); funclockbutton->setsizepolicy(qsizepolicy::expanding, qsizepolicy::expanding);
here pics:
pic1
pic2
you need create layout , add buttons it
mywidget.h
#ifndef mywidget_h #define mywidget_h #include <qwidget> class qradiobutton; class mywidget : public qwidget { q_object public: mywidget(qwidget *parent = 0); ~mywidget(); bool eventfilter(qobject *obj, qevent *event); private: qsize __calculateindicatorsize(const qsize & radiobtnsize); private: qradiobutton * m_radiobtn1; qradiobutton * m_radiobtn2; qstring styleforbutton_; qstring styleforindicator_; }; #endif // mywidget_h
mywidget.cpp
#include "mywidget.h" #include <qhboxlayout> #include <qpushbutton> #include <qradiobutton> #include <qevent> mywidget::mywidget(qwidget *parent) : qwidget(parent) { styleforbutton_ = "qradiobutton{border-width: 10 10 10 10; border-image: url(c:/users/phamat/desktop/bg.png) 10 10 10 10;}"; styleforindicator_ = "qradiobutton::indicator{width: %1px; height: %2px;}"; qhboxlayout * layout = new qhboxlayout(); layout->setcontentsmargins(20, 20, 20, 20); layout->setspacing(20); setlayout(layout); m_radiobtn1 = new qradiobutton(this); m_radiobtn1->settext("radio button 1"); m_radiobtn1->setsizepolicy(qsizepolicy::expanding, qsizepolicy::expanding); m_radiobtn1->setstylesheet(styleforbutton_); m_radiobtn1->installeventfilter(this); layout->addwidget(m_radiobtn1); m_radiobtn2 = new qradiobutton(this); m_radiobtn2->settext("radio button 2"); m_radiobtn2->setsizepolicy(qsizepolicy::expanding, qsizepolicy::expanding); m_radiobtn2->setstylesheet(styleforbutton_); m_radiobtn2->installeventfilter(this); layout->addwidget(m_radiobtn2); } mywidget::~mywidget() { } bool mywidget::eventfilter(qobject *obj, qevent *event) { if (obj == m_radiobtn1) { if (event->type() == qevent::resize) { qsize size = __calculateindicatorsize(m_radiobtn1->size()); m_radiobtn1->setstylesheet(styleforbutton_ + styleforindicator_.arg(size.width()).arg(size.height())); } } else if (obj == m_radiobtn2) { if (event->type() == qevent::resize) { qsize size = __calculateindicatorsize(m_radiobtn2->size()); m_radiobtn2->setstylesheet(styleforbutton_ + styleforindicator_.arg(size.width()).arg(size.height())); } } return qwidget::eventfilter(obj, event); } qsize mywidget::__calculateindicatorsize(const qsize & radiobtnsize) { if (radiobtnsize.width() > 300) { return qsize(20, 20); } else if (radiobtnsize.width() > 200) { return qsize(15, 15); } else { return qsize(10, 10); } }
pic 1
pic 2
Comments
Post a Comment