PDA

View Full Version : add QPainter widget



tommy
9th November 2007, 19:16
Hello,

I'm experimenting with QPainter and I'm able to draw simple primitives. The trouble starts when I want to add my drawing to my main layout (mainLayout->addWidget(l);). Is it true that you can't just add your drawing to your layout as a QLabel widget? Do I have to declare a new class and draw on a widget? I use mingw.

//THIS DOESN'T WORK:
//error: 'setPixmap' has not been declared
//error: request for member of non-aggregate type before '(' token

#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout* mainLayout = new QVBoxLayout(&window);
QPixmap pm(100,100);
pm.fill();

QPainter p(&pm);
QPen pen(Qt::blue, 1);
p.setPen(pen);
p.drawEllipse(0,0,80,80);

QLabel* l;
l.setPixmap(pm);
mainLayout->addWidget(l);

window.show();
return app.exec();
}

jpn
9th November 2007, 20:23
Well, actually this is not really a Qt but pure C++ issue. It should be something more like:


QLabel* l = new QLabel; // don't just declare a pointer, actually allocate something
l->setPixmap(pm); // use "->", not "."
mainLayout->addWidget(l);

tommy
9th November 2007, 20:32
Thanks jpn, everything works well now!

I've heard that it's possible to somehow thank helpful people here but I just don't find the button. How do you do it?

pherthyl
9th November 2007, 20:39
There should be a Thanks button at the bottom right of every post.