You're completely right. I just can't figure out why? I subclassed QWidget and setup the UI like the following:
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);
}
void PaintEventInScrollArea::setupUi()
{
QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < inputCount; ++i) {
inputLabel = new QLabel(QString("Input %1").arg(i + 1));
inputLabel->setMargin(60);
gridLayout->addWidget(inputLabel, i + 1, 0);
}
for (int i = 0; i < outputCount; ++i) {
outputLabel = new QLabel(QString("Output %1").arg(i + 1));
outputLabel->setMargin(60);
gridLayout->addWidget(outputLabel, 0, i + 1);
}
for (int i = 0; i < inputCount; ++i) {
for (int j = 0; j < outputCount; ++j) {
connectionLabel = new QLabel(QString("Connect %1").arg(j + 1));
connectionLabel->setFixedHeight(20);
connectionLabel->setMargin(60);
connectionLabel->setFrameStyle(QFrame::Box);
connectionLabel->setAlignment(Qt::AlignCenter);
gridLayout->addWidget(connectionLabel, i + 1, j + 1);
}
}
QWidget *viewport = new QWidget;
viewport->setLayout(gridLayout);
QScrollArea *scrollArea = new QScrollArea;
//scrollArea->setFrameShape(QFrame::NoFrame);
scrollArea->setWidget(viewport);
QBoxLayout *mainLayout = new QBoxLayout(QBoxLayout::LeftToRight);
mainLayout->addWidget(scrollArea);
this->setLayout(mainLayout);
}
To copy to clipboard, switch view to plain text mode
and reimplement paintEvent. A simple working example looks like
{
painter.
setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::darkRed);
QRectF rectangle
(10.0,
20.0,
80.0,
60.0);
painter.drawRect(rectangle);
}
void PaintEventInScrollArea::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::darkRed);
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
painter.drawRect(rectangle);
}
To copy to clipboard, switch view to plain text mode
If my painter take as parent the widget inside the scroll area, I get the error
paint device returned engine == 0
paint device returned engine == 0
To copy to clipboard, switch view to plain text mode
I think I will subclass QAbstractScrollArea and insert my widget inside it.
Bookmarks