Results 1 to 8 of 8

Thread: how to get QPainter object for QLabel

  1. #1
    Join Date
    Sep 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default how to get QPainter object for QLabel

    i have a widget and implemented a QpainetEvent;
    inside that i write
    {
    Qpainter painter(this);
    }

    here i have a QLabel widget on my main widget
    i want QPainter object of that QLabel insted of main widget
    like
    "QPainter painter(this)" instead of that statement can i write this statement "QPainter painter(label1)"

  2. #2
    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: how to get QPainter object for QLabel

    You can't. You can only paint on a widget from within its own paint event. If you want to capture paint event of another widget, please install an event filter for it.
    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.


  3. #3
    Join Date
    Sep 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get QPainter object for QLabel

    ok thanks for reply but your reply confused me more
    because i m totally new in qt so can u little bit tell me how to write event on that widget.......
    or some short code so i can get some idea.......

  4. #4
    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: how to get QPainter object for QLabel

    Open up Qt Assistant and type in "event filter" in the index tab.
    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.


  5. #5
    Join Date
    Sep 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get QPainter object for QLabel

    thanks........

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

    Default Re: how to get QPainter object for QLabel

    If your not subclassing QLabel, and instead only using QLabel, then use the following :
    Qt Code:
    1. bool myView::eventFilter(QObject *obj, QEvent *eve)
    2. {
    3. switch(eve->type())
    4. {
    5. case QEvent::Paint:
    6. {
    7. return handlePaintEvent(obj, eve);
    8. }
    9. default:
    10. {
    11. return QObject::eventFilter(obj, eve);
    12. }
    13. }
    14. } // eventFilter
    15.  
    16. bool myView::handlePaintEvent(QObject *obj, QEvent *eve)
    17. {
    18. QPainter painter(static_cast<QWidget *>(obj)); //here u may check if obj is your ui.frame()
    19. ......
    20. }
    To copy to clipboard, switch view to plain text mode 

    But if you are subclassing QLabel, better override its paintEvent() and initialize a painter like :
    Qt Code:
    1. void myLabel::paintEvent ( QPaintEvent * event )
    2. {
    3. QPainter painter(this);
    4. .......
    5. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get QPainter object for QLabel

    In my opinion it would be simple and better to go for subclassing the QLabel and doing your drawing in the paintEvent() function.
    Something as suggested above -
    Qt Code:
    1. void myLabel::paintEvent ( QPaintEvent * event )
    2. {
    3. QPainter painter(this);
    4. ....... // do your drawing..
    5. QLabel::paintEvent(event); // Pass to base class if needed.
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by aamer4yu; 15th March 2010 at 10:57.

  8. #8
    Join Date
    May 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get QPainter object for QLabel

    .. maybe this is a dirty way .. but it works for me:

    Qt Code:
    1. QPixmap *pix;
    2. pix=(QPixmap *)label->pixmap();
    3. QPainter label_painter(pix);
    To copy to clipboard, switch view to plain text mode 

    then you may use "label_painter" for your needs.

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 12:10
  2. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 18:31
  3. Open a QMainWindow Object in QDialog Object
    By chuengchuenghq in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2008, 07:33
  4. Replies: 7
    Last Post: 20th March 2006, 21:03
  5. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 16:27

Tags for this Thread

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.