Results 1 to 14 of 14

Thread: Problems with QPainter

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems with QPainter

    Hi to all,
    I'm writing a config dialog that I exec from my main application.
    With the dialog the user can choose the colors that will be used to draw an audio waveform.
    In the dialog I would show a preview of the waveform so I added a little QFrame named previewFrame and in the paintEvent I would
    draw a little sine function over it.

    This is part of the code of the paint event.

    Qt Code:
    1. void ConfigDlg::paintEvent( QPaintEvent* event )
    2. {
    3. int h = ui.previewFrame->height();
    4. int w = ui.previewFrame->width();
    5.  
    6. QPainter p( ui.previewFrame ); //<--give error!!!!!!!!
    7. p.setRenderHint( QPainter::Antialiasing, true );
    8. p.fillRect( 0, 0, w, h, bgColor );
    9. QPen pen = QPen( Qt::darkRed, 1 );
    10. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I get the following error
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setRenderHint: Painter must be active to set rendering hints
    Where the code is wrong?

    Best Regards
    Franco Amato

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Problems with QPainter

    hi,

    put; p.begin(this); and try again.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPainter

    Quote Originally Posted by zgulser View Post
    hi,

    put; p.begin(this); and try again.
    I set
    Qt Code:
    1. p.begin(ui.previewFrame);
    To copy to clipboard, switch view to plain text mode 

    but the error is still there
    Franco Amato

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Problems with QPainter

    Hımm..Then once more;

    1. check paintEvent if it's virtual
    2. it seems -unexpectedly- paintEngine doesn't work. So you force it to work by putting the line; p.paintEngine()->setActive(true);

  5. #5
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Problems with QPainter

    Please let me know the result

  6. #6
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPainter

    What type ui.previewFrame have?
    I think, in paintEvent you can paint only on this object, if the object inherit QWidget. If object inherit QImage QPixmap, you cat paint on it everywhere.
    east or west home is best

  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPainter

    Quote Originally Posted by kwisp View Post
    What type ui.previewFrame have?
    I think, in paintEvent you can paint only on this object, if the object inherit QWidget. If object inherit QImage QPixmap, you cat paint on it everywhere.
    Hi ui.previewFrame is a QFrame, so the problem is that I don't paint on this ?

    So how can I draw on my ui.previewFrame object tha's part of the dialog?
    Franco Amato

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QPainter

    Quote Originally Posted by franco.amato View Post
    so the problem is that I don't paint on this ?
    Yes. The paint event only affect the current widget, no other.

  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPainter

    I changed with:

    Qt Code:
    1. QPainter p( this );
    To copy to clipboard, switch view to plain text mode 

    and I don't get the error but it doesn't paint where it should!
    Franco Amato

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QPainter

    Ehm, what is so hard to understand: You CAN'T paint on your frame from an other widgets paint method. If you want to paint on that frame you have to subclass that frame and use its paint event.

  11. #11
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPainter

    Quote Originally Posted by Lykurg View Post
    Ehm, what is so hard to understand: You CAN'T paint on your frame from an other widgets paint method. If you want to paint on that frame you have to subclass that frame and use its paint event.
    Lykurg please don't scream I'm not deaf and it can affect your heart.
    I wrote my last post WITHOUT seeing your FIRST reply .
    Meanwhile thank you and I think Qt should allow to draw on a widget wihout subclass everything.

    Best Regards
    Franco Amato

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QPainter

    Quote Originally Posted by franco.amato View Post
    Qt should allow to draw on a widget wihout subclass everything.
    That would break the logic of OOP. You could never say how your widget would look like since it could altered from everywere. And why is subclassing always a problem. I like it...

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with QPainter

    Quote Originally Posted by franco.amato View Post
    Meanwhile thank you and I think Qt should allow to draw on a widget wihout subclass everything.
    It does. You can install an event filter on the widget you wish to paint on and intercept its paint events. Of course you will still have to subclass some QObject to implement the filter.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QPainter

    Yes. U could install event filter on widget and handle its paint event as follows :
    Qt Code:
    1. bool ConfigDlg::eventFilter(QObject *obj, QEvent *eve)
    2. {
    3. switch(eve->type())
    4. {
    5. case QEvent::Paint:
    6. {
    7. return handlePaintEvent(obj, eve);
    8. }
    9.  
    10. default:
    11. {
    12. return QObject::eventFilter(obj, eve);
    13. }
    14. }
    15. } // eventFilter
    16.  
    17. bool ConfigDlg::handlePaintEvent(QObject *obj, QEvent *eve)
    18. {
    19. QPainter painter(static_cast<QWidget *>(obj)); //here u may check if obj is your ui.frame()
    20. ......
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  2. Problems with QPainter
    By franco.amato in forum Qt Programming
    Replies: 11
    Last Post: 23rd November 2009, 20:54
  3. Replies: 5
    Last Post: 7th September 2009, 20:57
  4. Weird color problems related to QTextEdit and QPainter
    By Erlendhg in forum Qt Programming
    Replies: 5
    Last Post: 18th June 2007, 21:57
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.