1
votes

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

1
I guess you are trying to do things based on this: stackoverflow.com/questions/20801622/… Any reason why you did not go or QML instead of the QPainter approach? It would be easier in new code like this.lpapp
Yes I am, thing is I have never used QML, and was pretty happy with the QPainter result you suggested.Tcanarchy
except for the scrolling of courseTcanarchy
I do not see any QScrollArea experiment in your code. So what happens to it when you add these two lines: scrollArea = new QScrollArea; scrollArea->setWidget(labeltime);? Also, why do you need a layout for this? I think you could set the widget directly.lpapp
I am sorry, ui->overview is a predefined scrollArea in my guiTcanarchy

1 Answers

1
votes

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);