c++ - How to draw line behind buttons in Qt? -
question
in custom widget, i'd draw lines (using qpainter
) connect buttons in qgridlayout
. lines shall behind buttons, in order to
- a) not interfere buttons in between
- b) allow starting lines in center of buttons, not edges
considering ideas this question, realize simple, basic version running in gui application (source code below).
as long use qpushbutton
standard qt style, works charm (left), but, want use custom style, lines overlap (right):
what property or mechanism causing behavior?
code
myframe.h:
#include <qframe> class myframe : public qframe { public: myframe(); virtual ~myframe() = default; };
myframe.cpp:
#include "myframe.h" #include "linedrawwidget.h" #include <qvboxlayout> #include <qgridlayout> #include <qpushbutton> #include <qbuttongroup> myframe::myframe() { auto* mainlayout = new qvboxlayout(this); auto* buttonlayout = new qgridlayout(); qpushbutton* button; auto* buttons = new qbuttongroup(); (int = 0; < 3; ++i) { button = new qpushbutton(); button->settext(qstring::number(i+1)); button->setfixedheight(40); button->setfixedwidth(40); button->setstylesheet("qpushbutton { color : black; background-color : white; }"); button->setstylesheet("qpushbutton { border-style : outset; border-color: black; border-width: 2px; border-radius: 6px; }"); buttonlayout->addwidget(button); buttons->addbutton(button, i); } auto* linedraw = new linedrawwidget( buttons->button(0), buttons->button(2)); linedraw->setlayout(buttonlayout); mainlayout->addwidget(linedraw); }
linedrawwidget.h:
#include <qwidget> class linedrawwidget : public qwidget { public: linedrawwidget( qwidget* from, qwidget* to, qwidget* parent = nullptr); virtual ~linedrawwidget() = default; protected: virtual void paintevent(qpaintevent* e) final override; private: qwidget* _from; qwidget* _to; };
linedrawwidget.cpp:
#include "linedrawwidget.h" #include <qpainter> linedrawwidget::linedrawwidget( qwidget* from, qwidget* to, qwidget* parent) : qwidget(parent), _from(from), _to(to) { } void linedrawwidget::paintevent(qpaintevent* e) { (void)e; qpainter painter(this); qpoint start = _from->maptoglobal(_from->rect().bottomleft()); qpoint end = _to->maptoglobal(_to->rect().topright()); painter.drawline(mapfromglobal(start), mapfromglobal(end)); }
i think problem 2 separate calls setstyle
single qpushbutton
-- second call appears reset properties not present in it. try putting in single call...
button->setstylesheet("color : black; background-color : white; border-style : outset; border-color: black; border-width: 2px; border-radius: 6px;");
seems work me.
Comments
Post a Comment