PDA

View Full Version : How to call painEvent for a slot



Jyoti.verma
19th August 2009, 21:31
Hi

I am using QT4.4 on Mac.

I just want to know why we need QPaintEvent for painting?

I don't want to call paintEvent(QPaintEvent *) on loading?

and can we call paintEvent(QPaintEvent *) in a slot's definition.

for eg.


void Painting:: browse()
{
/// code for selecting file

paintEvent(event);
}

void Painting:: paintEvent(QPaintEvent *)
{

QRectF rect(10.0, 20.0, 80.0, 60.0);

QPainter painter(this);

painter.drawRect(rect);
painter.end();
}

////

Thanks
Jyoti

nish
20th August 2009, 04:27
I am using QT4.4 on Mac.

dosent matter which platform u are using.



I just want to know why we need QPaintEvent for painting?
because there are some rules, and we need to follow it. Its just like asking "why i need declare a variable before using it?"



I don't want to call paintEvent(QPaintEvent *) on loading?
i dont know what do you mean but you you can disable paintevents by using setUpdatesEnabled(false), remember to make it true sometime.



and can we call paintEvent(QPaintEvent *) in a slot's definition.

no.. but you can call update() (better), or repaint();



void Painting:: browse()
{
/// code for selecting file

paintEvent(event);
}

just call update(), this code wont work.

wagmare
20th August 2009, 05:29
I just want to know why we need QPaintEvent for painting?

because the drawing u want to do using QPainter will painted only in a paintEvent() ..



I don't want to call paintEvent(QPaintEvent *) on loading?
where ..? if u want to design a different shape or look .. u have to paint ..and u need QPaintEvent()


void Painting:: browse()
{
/// code for selecting file

paintEvent(event);
}


no need to call paintEvent() manually like this ..

paintEvent() will call or occured :
1.your widget is shown for first time .
2.after a window moved to reveal some part of widget.
3.the window i which the widget lies is restored after bieng minimized.
4.the window in which the widget lies is resized.
5. the user switch to other desktop

*** but generally paintEvent called manually by calling QWidget::update() . as Mr.Death suggest ..

Jyoti.verma
20th August 2009, 23:13
thanks for reply.

I had followed as you told me.

Please have a look at my code
my code is:



myApp:: myApp(QDialog *parent): QDialog(parent)
{
ui.setupUi(this);
connect(ui.selectButton,SIGNAL(clicked()),this,SLO T(paintFile()));

}

void myApp :: paintFile()
{
update();
}
void myApp:: paintEvent(QPaintEvent *)
{

painter =new QPainter(this);
QRectF rect1(180, 2, 246,305);
QRectF rect2(426, 2, 246,305);



painter->drawRect(rect1);
painter->drawRect(rect2);
browse();
painter->end();
}

void myApp::browse()
{
//// code


painter->setFont(QFont("Halvatica",11));
painter->setPen(Qt::black);
painter->drawText(181,2,492,305,Qt::AlignTop,myString,0);

////code

}

}

there is no compilation error but there is run time error
on debugger console :


QWidget::repaint: Recursive repaint detected
QPainter::begin: A paint device can only be painted by one painter at a time.
QPainter::setClipRegion: Painter not active

I don't understand what this error is...please provide some code example for using it correctly.

thanks
Jyoti

Archimedes
21st August 2009, 05:12
The function void paintEvent(QPainteEvent *) is triggered when 1 of the conditions wagmare described occurred. Read more for events and how they work in the qt documentation.

What this code will do is when the application starts it will draw the 2 rects. If you want them to be drawn after the button click you must do some checks in the event.
Also the function browse() needs as argument a QPainter object to do the drawings due to scopes, unless you define one in the function.

aamer4yu
21st August 2009, 06:07
You dont use painter =new QPainter(this);
If you use it,, you need to delete it...
But the simplest way is...

{
QPainter painter(this);
// draw drawing using painter..
// done... no need to call end() also as it gets called in destructor of QPainter..
}

wagmare
21st August 2009, 06:34
try to do all your painting inside the paintEvent() ..

not in the separate function..

this is the way u can update the paintEvent() ..


void myApp::browse()
{
//// code


painter->setFont(QFont("Halvatica",11));
painter->setPen(Qt::black);
painter->drawText(181,2,492,305,Qt::AlignTop,myString,0) ;

////code

}

just try to set some flag and use update() in the above code ...
as

void myApp::browse()
{
textFlag = 1;
update();
}

and in paintEvent()

{

QPainter painter(this);
QRectF rect1(180, 2, 246,305);
QRectF rect2(426, 2, 246,305);

painter.drawRect(rect1);
painter.drawRect(rect2);
if(textFlag == 1){
painter.setFont(QFont("Halvatica",11));
painter.setPen(Qt::black);
painter.drawText(181,2,492,305,Qt::AlignTop,myStri ng,0) ;
}
}

then call the browser() function at run time (ex: timer) .. see it will display the text ..

nish
24th August 2009, 01:47
jyoti jee.,,, this wont work... you need to read the qt painting example... your code shows that you dont have enough background knowledge of qt. Even if you get the solution this time but you will stuck at another problem now and then if you dont make you base strong...

give three days to qt assistant and going through the examples... thats all needed to learn qt :)

wysota
25th August 2009, 09:49
there is no compilation error but there is run time error
on debugger console :

Really? Impossible... Where is the "painter" variable you use in browse() declared? Unless you create another painter there which would explain the error message you were getting.