PDA

View Full Version : double buffering



HelloDan
30th March 2009, 13:41
In my this program, I want to draw a coordinate in an off-screen pixmap, and then paint it to the screen.

When I run the app step by step, it work well, the pixmap changes. But if I run 2000 time at one time, the pixmap painted to screen will change only once, the last time, 2000. How can I make it changes every time?

By the way, there three buttons at the top of the dialog. when I run the app, I want to disable the three buttons, but fail. But it's strange that it works in the constructor function if I disable it.

see code fragmemts:



GenePlotter::GenePlotter(QWidget* parent):QDialog(parent)
{
setBackgroundRole(QPalette::Dark);
setAutoFillBackground(true);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

//buttons settings
settingButton=new QPushButton(this);
settingButton->setText("settings");
settingButton->adjustSize();
oneStepButton=new QPushButton(this);
oneStepButton->setText("one step");
oneStepButton->adjustSize();
runAllButton=new QPushButton(this);
runAllButton->setText("run all");
runAllButton->adjustSize();
//place the buttons in a proper position
settingButton->move(50, 10);
oneStepButton->move(50 + settingButton->width() + 5, 10);
runAllButton->move(60+settingButton->width()+oneStepButton->width(),10);
settingButton->show();
oneStepButton->show();
runAllButton->show();


//plotter settings
minX = 0.0;
maxX = 10.0;
numXTicks = 5;
minY = 0.0;
maxY = 10.0;
numYTicks = 5;
pMutation=0.8;
pXCross=0.15;
popSize=100;
generations=200;
iniPoints();
resize(600,400);

// signals and slots
connect(settingButton,SIGNAL(clicked()),this,SLOT( showSettings()));
connect(oneStepButton,SIGNAL(clicked()),this,SLOT( oneStep()));
connect(runAllButton,SIGNAL(clicked()),this,SLOT(r unAll()));

//runAllButton->setEnabled(false); // It works if I post it here.
}





void GenePlotter::runAll()
{
settingButton->setEnabled(false);
oneStepButton->setEnabled(false);
runAllButton->setEnabled(false);

if(couter==0)
++couter;

while(couter<generations)
{
++couter;
execOnce();
}
settingButton->setEnabled(true);
oneStepButton->setEnabled(true);
runAllButton->setEnabled(true);

}




I will appreciate any reply! Thanks!

drhex
30th March 2009, 13:49
The 3 buttons are not disabled onscreen because there is no event Processing in your runAll-method

HelloDan
30th March 2009, 14:07
The 3 buttons are not disabled onscreen because there is no event Processing in your runAll-method

But It can disable runAllButton when I add the code :runAllButton->setEnabled(false); inside oneStep() slot.

connect(oneStepButton,SIGNAL(clicked()),this,SLOT( oneStep()));

drhex
30th March 2009, 14:17
you haven't shown that onestep slot. You need some event processing between setEnabled(false) and setEnabled(true). Try calling QCoreApplication::processEvents after disabling the buttons.

HelloDan
30th March 2009, 15:00
you haven't shown that onestep slot. You need some event processing between setEnabled(false) and setEnabled(true). Try calling QCoreApplication::processEvents after disabling the buttons.

It's an exercise, very simple. I had tried it. Thanks for your advise, it works. But that's still one problem. How can I force to update the pixmap every time?



void GenePlotter::oneStep()
{
++couter;
if(couter<generations)
execOnce();
}

drhex
30th March 2009, 18:03
I'd try calling update() or repaint() on the widget that has the pixmap, and probably processEvents too, but I'm sure there are more elegant ways.

faldzip
30th March 2009, 18:28
i would also suggest using layouts than setting the buttons positions manually :D

HelloDan
30th March 2009, 18:32
I'd try calling update() or repaint() on the widget that has the pixmap, and probably processEvents too, but I'm sure there are more elegant ways.

repaint() is OK

Thanks

HelloDan
30th March 2009, 18:36
Why must I call repaint, but oneStep can do the same thing without call it?

drhex
30th March 2009, 19:47
Because when you draw in execOnce(), a flag is set indicating that an update is necessary. That flag is acted upon when the main event loop is run (after your slot has finished). runAll(), though, wants to do many updates without returning from the slot between each and so needs to do more explicit repainting.

HelloDan
31st March 2009, 03:14
Thanks to drhex