PDA

View Full Version : Render child widget relative to paint event rectangles



jmalicke
4th March 2016, 21:11
We want to paint some custom rectangles and then put a QPushButton in them.

11779

Let's say we want to put the QPushButton in the top rectangle. How do we do it?

Here's our paint event:


void ChannelAxisList::paintEvent(QPaintEvent* pEvent)
{
QPainter painter(this);

QRect rt = pEvent->rect();
painter.setBrush(QBrush(QColor(255, 255, 255, 0)));
painter.setPen(QPen(QColor(255, 255, 255), 1));

rt.setRight(rt.right() - 1);
rt.setBottom(rt.bottom() - 1);

painter.drawRect(rt);

rt.setTopLeft(QPoint(0, 0));
calculateLayout(rt);


painter.setBrush(QBrush(QColor(255, 255, 255, 0)));
painter.setPen(QPen(QColor(255, 255, 255), 1));

painter.drawRect(m_mainRect);

painter.setBrush(QBrush(QColor(255, 0, 0)));
painter.setPen(QPen(QColor(0, 255, 0), 1));
painter.setRenderHint(QPainter::Antialiasing, false);

painter.drawRoundedRect(m_header, 2, 2);
painter.drawRoundedRect(m_list, 2, 2);
}

kmy
5th March 2016, 03:32
Creating QPusuButton code isn't in paintEvent, but in constructor.

It seems you already prepared 'm_header'.
Hou about use QPushButton::setGeometry() ?



ChannelAxisList()
{
this->m_button = new QPushButton(this);
}

void ChannelAxisList::otherMethod()
{
// m_header is changed here...
this->m_header = QRect(QPoint(), this->size()); // sample

this->m_button->setGeometry(this->m_header);
}

jmalicke
8th March 2016, 20:53
Very bizarre things happen when I move() or setGeometry() inside a paintEvent(). It appears in the right place until the mouse hovers over it. Then the widget jumps away from the mouse and leaves a trailing rectangle behind.

anda_skoa
8th March 2016, 22:20
Very bizarre things happen when I move() or setGeometry() inside a paintEvent(). It appears in the right place until the mouse hovers over it. Then the widget jumps away from the mouse and leaves a trailing rectangle behind.

Why would you move a widget in its parent's paintEvent()?
That is where you paint.

Cheers,
_