PDA

View Full Version : HOW TO DISPLAY A RECTANGLE ON AN IMAGE on a label



qt_user
6th August 2010, 08:00
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..........



void VideoDisplayer::processNextPicture()
{

if((i >= startFrameMarkerPosition)&&(i < endFrameMarkerPosition))
{
//videoLabel->setPixmap(QPixmap(localFileName.at(i)));
//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

// QFrame::paintEvent(event);

QPainter painter(videoLabel);

painter.drawPixmap(0,0,400,400,QPixmap(localFileNa me.at(i)));
//painter.drawPixmap(0,0,100,100,QPixmap(":/images/frame0000.png"));

int x,y,w,h;
x=100; y=200; w=80;h=60;
painter.setPen(Qt::red);
painter.drawRect(x,y,w,h);
videoLabel->adjustSize();
//videoLabel->setScaledContents(true);
localSlider->setValue(i);
videoLabel->show();
i++;

}


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

tbscope
6th August 2010, 08:54
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.

qt_user
6th August 2010, 08:59
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:



void VideoDisplayer::processNextPicture()
{

if((i >= startFrameMarkerPosition)&&(i < endFrameMarkerPosition))
videoLabel->repaint();
}


void VideoDisplayer::paintEvent(QPaintEvent *event)
{
if((i >= startFrameMarkerPosition)&&(i < endFrameMarkerPosition))
{
//videoLabel->setPixmap(QPixmap(localFileName.at(i)));//Please note that this command, when uncommented, correctly displays the images/frames according to the value of i

QFrame::paintEvent(event);

//QPainter painter(this -> viewport());
QPainter *painter= new QPainter(videoLabel);


painter->drawPixmap(0,0,400,400,QPixmap(localFileName.at(i) ));

int x,y,w,h;
x=100; y=200; w=80;h=60;
painter->setPen(Qt::red);
painter->drawRect(x,y,w,h);


videoLabel->adjustSize();
localSlider->setValue(i);
videoLabel->show();
i++;
}

qt_user
6th August 2010, 11:05
plz help anyone!!!!!

aamer4yu
6th August 2010, 15:19
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 ??