1 Attachment(s)
How to draw in a QScrollArea?
Hi,
I have a classed that inherits QWidget. Inside of it, I have a QScrollArea with some QLabel items wrapped in a QGridLayout. Since I want to connect my labels through lines, I reimplemented the method paintEvent(QPaintEvent *). I unfortunately have the problem that the drawing occurs outside the scroll area as you can see in the attached image. Any hints?
Thanks.
Re: How to draw in a QScrollArea?
Looks like you are painting in the widget containing the scroll-area and not in the scroll area widget.
Why don't you put your labels in to a widget, do all the special stuff on that widget, and put that widget as content widget for the scroll area?
Re: How to draw in a QScrollArea?
You're completely right. I just can't figure out why? I subclassed QWidget and setup the UI like the following:
Code:
void PaintEventInScrollArea::setupUi()
{
for (int i = 0; i < inputCount; ++i) {
inputLabel->setMargin(60);
gridLayout->addWidget(inputLabel, i + 1, 0);
}
for (int i = 0; i < outputCount; ++i) {
outputLabel->setMargin(60);
gridLayout->addWidget(outputLabel, 0, i + 1);
}
for (int i = 0; i < inputCount; ++i) {
for (int j = 0; j < outputCount; ++j) {
connectionLabel->setFixedHeight(20);
connectionLabel->setMargin(60);
connectionLabel
->setFrameStyle
(QFrame::Box);
connectionLabel->setAlignment(Qt::AlignCenter);
gridLayout->addWidget(connectionLabel, i + 1, j + 1);
}
}
viewport->setLayout(gridLayout);
//scrollArea->setFrameShape(QFrame::NoFrame);
scrollArea->setWidget(viewport);
mainLayout->addWidget(scrollArea);
this->setLayout(mainLayout);
}
and reimplement paintEvent. A simple working example looks like
Code:
{
painter.
setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::darkRed);
QRectF rectangle
(10.0,
20.0,
80.0,
60.0);
painter.drawRect(rectangle);
}
If my painter take as parent the widget inside the scroll area, I get the error
Code:
paint device returned engine == 0
I think I will subclass QAbstractScrollArea and insert my widget inside it.
Re: How to draw in a QScrollArea?
Quote:
You're completely right. I just can't figure out why?
Why what? your are painting in the upper parent widget!
Your upper parent widget IS PaintEventInScrollArea.
You put in to it a QSrollArea and a QWidget.
And its in the QWidget where you should paint, not in PaintEventInScrollArea which is the upper one!
Subclass your QWidget (the one you put in to your QScrollArea, the one your call 'viewport ') and overload it's paintEvent().
Re: How to draw in a QScrollArea?
Fine. I simply left the widget as it was and later wrap the whole stuff as you suggested in a QScrollArea with the method setWidget().