I'm trying to draw with QPainter in a QPixmap, put that QPixmap in a QLabel, and that QLabel in a QScrollArea.

Sometimes the painted pixmap is bigger then my ScrollArea allows, but somehow it doesn't scroll to show the rest. What am I doing wrong?

QPixmap *pixmap = new QPixmap(10000,500);
QLabel *labeltime = new QLabel;

QHBoxLayout *layout = new QHBoxLayout;
pixmap->fill(QColor("transparent"));

int currentX = 0;
const int currentY = 220;
const int height = 50; // Coming from some static data initialization

QPainter *painter = new QPainter(pixmap);
QPen pen(Qt::gray, 2);
painter->setPen(pen);

for(int i = 0; i< viewlist.size(); i++)
{
    QBrush brush(QColor(viewlist[i].color));
    painter->setBrush(brush);
    painter->drawRect(currentX, currentY, viewlist[i].length, height);
    currentX += viewlist[i].length;
}

labeltime->setPixmap(*pixmap);
layout->addWidget(labeltime);

ui->overview->setLayout(layout);

I know this is a long and weird way to add a pixmap, but I want it to be scrollable, and I can't paint on a QScrollArea. Is there a better way? Or can someone tell me what is wrong?

Thanks

有帮助吗?

解决方案

Since you only have one child widget, it is simpler to eliminate your layout. Change these lines:

layout->addWidget(labeltime);
ui->overview->setLayout(layout);

to:

ui->overview->setWidget(labeltime);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top