Results 1 to 6 of 6

Thread: QPainter

  1. #1
    Join Date
    Jan 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default QPainter

    Hello,

    I want to use QPainter to draw very simple lines and points. I checked the docs and wanted to copy the example, but the following code doesn't work:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QWidget fenster1;
    5. fenster1.resize(400,400);
    6.  
    7.  
    8. QPainter achsen(&fenster1);
    9.  
    10. achsen.setPen(QPen(Qt::blue,2));
    11.  
    12. achsen.drawPoint(200,100);
    13.  
    14. achsen.drawLine(100,100,301,301);
    15.  
    16. fenster1.show();
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    It doesn't return an error, but I can neither see the line nor the point. It just displays an empty window. Where is the mistake?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    You can't paint on hidden widgets, because these operations will be just ignored. Another problem is that everything you paint on a widget isn't remembered, so if that widget is hidden or covered by another window, you will loose everything you have drawn. Third problem is that you shouldn't draw anything outside paintEvent() in Qt4, because of "backing store".

    The correct solution is to subclass QWidget and reimplement paintEvent() (see the attachment).
    Attached Files Attached Files

  3. #3
    Join Date
    Jan 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter

    Thanks very much. After trying to compile your main.cpp I get a problem that most likely is caused by my qmake:

    Qt Code:
    1. release/main.moc:35: error: `Test' has not been declared
    2. release/main.moc:40: error: `Test' has not been declared
    3. release/main.moc:41: error: non-member function `const QMetaObject* metaObject()
    4. ' cannot have `const' method qualifier
    5. release/main.moc:45: error: `Test' has not been declared
    6. release/main.moc: In function `void* qt_metacast(const char*)':
    7. release/main.moc:49: error: `Test' has not been declared
    8. release/main.moc:49: error: expected `>' before '*' token
    9. release/main.moc:49: error: expected `(' before '*' token
    10. release/main.moc:49: error: expected primary-expression before '>' token
    11. release/main.moc:49: error: invalid use of `this' in non-member function
    12. release/main.moc:49: error: expected `)' before ';' token
    13. release/main.moc:50: error: cannot call member function `virtual void* QWidget::
    14. qt_metacast(const char*)' without object
    15. release/main.moc: At global scope:
    16. release/main.moc:53: error: `Test' has not been declared
    17. release/main.moc: In function `int qt_metacall(QMetaObject::Call, int, void**)':
    18.  
    19. release/main.moc:55: error: cannot call member function `virtual int QWidget::qt
    20. _metacall(QMetaObject::Call, int, void**)' without object
    21. mingw32-make[1]: *** [release\main.o] Error 1
    22. mingw32-make[1]: Leaving directory `D:/projekte/Schaubild/cpp2'
    23. mingw32-make: *** [release] Error 2
    To copy to clipboard, switch view to plain text mode 

    Is there perhaps any option I have to change in any settings?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    If you have saved that file as "main.cpp" in an empty directory, you should be able to compile it using:
    qmake -project
    qmake
    make

  5. #5
    Join Date
    Jan 2006
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter

    No, that doesn't work.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter

    Quote Originally Posted by phil_ View Post
    No, that doesn't work.
    How exactly it "doesn't work"? Do you issue these commands from the "Qt 4.x.x Command Prompt"?

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. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 14:48
  3. QPainter in console apps?
    By Funklord in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2006, 14:03
  4. Replies: 7
    Last Post: 20th March 2006, 20:03
  5. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 15:27

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.