Results 1 to 5 of 5

Thread: HOW TO DISPLAY A RECTANGLE ON AN IMAGE on a label

  1. #1
    Join Date
    Jul 2010
    Posts
    50
    Thanks
    7
    Platforms
    Windows

    Default HOW TO DISPLAY A RECTANGLE ON AN IMAGE on a label

    I have already made a video player(it runs different frames according to a timer) on a "QLabel"(not a QScrollArea)......now I have added another functionality that when the "process" button is clicked, the images are displayed along with some rectangles......For this I have used QPainter class's drwaPixel() and drwaRectangle() functions.................but both the functions are not displaying anything on the Label..........Here is the part of the code for the newer(process) functionality..........
    Qt Code:
    1. void VideoDisplayer::processNextPicture()
    2. {
    3.  
    4. if((i >= startFrameMarkerPosition)&&(i < endFrameMarkerPosition))
    5. {
    6. //videoLabel->setPixmap(QPixmap(localFileName.at(i)));
    7. //I used this function for the earlier case(normal video display without process functionality) and it works fine but it can't be used now otherwise the drawRectangle() function doesn't work over it
    8.  
    9. // QFrame::paintEvent(event);
    10.  
    11. QPainter painter(videoLabel);
    12.  
    13. painter.drawPixmap(0,0,400,400,QPixmap(localFileName.at(i)));
    14. //painter.drawPixmap(0,0,100,100,QPixmap(":/images/frame0000.png"));
    15.  
    16. int x,y,w,h;
    17. x=100; y=200; w=80;h=60;
    18. painter.setPen(Qt::red);
    19. painter.drawRect(x,y,w,h);
    20. videoLabel->adjustSize();
    21. //videoLabel->setScaledContents(true);
    22. localSlider->setValue(i);
    23. videoLabel->show();
    24. i++;
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 
    This code displays nothing on the label
    I have also tried using the paintEvent function by defining it with the similar contents and calling it in place of the above code by repaint();This showed me that the paint function is getting called but the drawRectangle() and drawPixmap() on a QLabel are the real culprits.......please suggest a solution......thanx in advance
    Last edited by qt_user; 6th August 2010 at 08:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: HOW TO DISPLAY A RECTANGLE ON AN IMAGE on a label

    Widget painting can only take part in the paint event or a function called from the paint event. Unless you set a flag to paint outside the paint event (not supported on all platforms).

    Basically you draw the frame of a movie, and then on top of that your other things.

  3. #3
    Join Date
    Jul 2010
    Posts
    50
    Thanks
    7
    Platforms
    Windows

    Default Re: HOW TO DISPLAY A RECTANGLE ON AN IMAGE on a label

    Quote Originally Posted by tbscope View Post
    Widget painting can only take part in the paint event or a function called from the paint event. Unless you set a flag to paint outside the paint event (not supported on all platforms).

    Basically you draw the frame of a movie, and then on top of that your other things.
    I have also tried by making a paint event function as shown below:
    Qt Code:
    1. void VideoDisplayer::processNextPicture()
    2. {
    3.  
    4. if((i >= startFrameMarkerPosition)&&(i < endFrameMarkerPosition))
    5. videoLabel->repaint();
    6. }
    7.  
    8.  
    9. void VideoDisplayer::paintEvent(QPaintEvent *event)
    10. {
    11. if((i >= startFrameMarkerPosition)&&(i < endFrameMarkerPosition))
    12. {
    13. //videoLabel->setPixmap(QPixmap(localFileName.at(i)));//Please note that this command, when uncommented, correctly displays the images/frames according to the value of i
    14.  
    15. QFrame::paintEvent(event);
    16.  
    17. //QPainter painter(this -> viewport());
    18. QPainter *painter= new QPainter(videoLabel);
    19.  
    20.  
    21. painter->drawPixmap(0,0,400,400,QPixmap(localFileName.at(i)));
    22.  
    23. int x,y,w,h;
    24. x=100; y=200; w=80;h=60;
    25. painter->setPen(Qt::red);
    26. painter->drawRect(x,y,w,h);
    27.  
    28.  
    29. videoLabel->adjustSize();
    30. localSlider->setValue(i);
    31. videoLabel->show();
    32. i++;
    33. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by qt_user; 6th August 2010 at 09:08.

  4. #4
    Join Date
    Jul 2010
    Posts
    50
    Thanks
    7
    Platforms
    Windows

    Default Re: HOW TO DISPLAY A RECTANGLE ON AN IMAGE on a label

    plz help anyone!!!!!

  5. #5
    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 DISPLAY A RECTANGLE ON AN IMAGE on a label

    Your paintEvent seems too heavy... and error prone too..
    you are creating painter on heap.. and not deleting it !!
    better create it on stack.

    thn whats the purpose of calling label->show() from paintEvent ??

Similar Threads

  1. Replies: 6
    Last Post: 30th July 2010, 07:23
  2. Replies: 1
    Last Post: 23rd May 2009, 09:04
  3. display number on label
    By aj2903 in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 07:24
  4. Display rectangle on Image
    By parmar ranjit in forum Qt Programming
    Replies: 3
    Last Post: 22nd February 2008, 13:09
  5. Display a Label on top of a QGlWidget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2008, 16:11

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.