Results 1 to 4 of 4

Thread: add QPainter widget

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default add QPainter widget

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

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: add QPainter widget

    Well, actually this is not really a Qt but pure C++ issue. It should be something more like:
    Qt Code:
    1. QLabel* l = new QLabel; // don't just declare a pointer, actually allocate something
    2. l->setPixmap(pm); // use "->", not "."
    3. mainLayout->addWidget(l);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    tommy (9th November 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default how to thank jpn and the others

    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?

  5. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to thank jpn and the others

    There should be a Thanks button at the bottom right of every post.

  6. The following user says thank you to pherthyl for this useful post:

    tommy (9th November 2007)

Similar Threads

  1. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 06:29
  2. Replies: 3
    Last Post: 17th October 2007, 12:52
  3. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  4. Replies: 7
    Last Post: 20th March 2006, 20:03
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.