Results 1 to 11 of 11

Thread: double buffering

  1. #1
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default double buffering

    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:

    Qt Code:
    1. GenePlotter::GenePlotter(QWidget* parent):QDialog(parent)
    2. {
    3. setBackgroundRole(QPalette::Dark);
    4. setAutoFillBackground(true);
    5. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    6.  
    7. //buttons settings
    8. settingButton=new QPushButton(this);
    9. settingButton->setText("settings");
    10. settingButton->adjustSize();
    11. oneStepButton=new QPushButton(this);
    12. oneStepButton->setText("one step");
    13. oneStepButton->adjustSize();
    14. runAllButton=new QPushButton(this);
    15. runAllButton->setText("run all");
    16. runAllButton->adjustSize();
    17. //place the buttons in a proper position
    18. settingButton->move(50, 10);
    19. oneStepButton->move(50 + settingButton->width() + 5, 10);
    20. runAllButton->move(60+settingButton->width()+oneStepButton->width(),10);
    21. settingButton->show();
    22. oneStepButton->show();
    23. runAllButton->show();
    24.  
    25.  
    26. //plotter settings
    27. minX = 0.0;
    28. maxX = 10.0;
    29. numXTicks = 5;
    30. minY = 0.0;
    31. maxY = 10.0;
    32. numYTicks = 5;
    33. pMutation=0.8;
    34. pXCross=0.15;
    35. popSize=100;
    36. generations=200;
    37. iniPoints();
    38. resize(600,400);
    39.  
    40. // signals and slots
    41. connect(settingButton,SIGNAL(clicked()),this,SLOT(showSettings()));
    42. connect(oneStepButton,SIGNAL(clicked()),this,SLOT(oneStep()));
    43. connect(runAllButton,SIGNAL(clicked()),this,SLOT(runAll()));
    44.  
    45. //runAllButton->setEnabled(false); // It works if I post it here.
    46. }
    47.  
    48.  
    49.  
    50.  
    51.  
    52. void GenePlotter::runAll()
    53. {
    54. settingButton->setEnabled(false);
    55. oneStepButton->setEnabled(false);
    56. runAllButton->setEnabled(false);
    57.  
    58. if(couter==0)
    59. ++couter;
    60.  
    61. while(couter<generations)
    62. {
    63. ++couter;
    64. execOnce();
    65. }
    66. settingButton->setEnabled(true);
    67. oneStepButton->setEnabled(true);
    68. runAllButton->setEnabled(true);
    69.  
    70. }
    To copy to clipboard, switch view to plain text mode 

    I will appreciate any reply! Thanks!
    Last edited by HelloDan; 30th March 2009 at 14:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: double buffering

    The 3 buttons are not disabled onscreen because there is no event Processing in your runAll-method

  3. #3
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: double buffering

    Quote Originally Posted by drhex View Post
    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()));

  4. #4
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: double buffering

    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.

  5. #5
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: double buffering

    Quote Originally Posted by drhex View Post
    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?

    Qt Code:
    1. void GenePlotter::oneStep()
    2. {
    3. ++couter;
    4. if(couter<generations)
    5. execOnce();
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: double buffering

    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.

  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: double buffering

    i would also suggest using layouts than setting the buttons positions manually
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #8
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: double buffering

    Quote Originally Posted by drhex View Post
    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

  9. #9
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: double buffering

    Why must I call repaint, but oneStep can do the same thing without call it?

  10. #10
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: double buffering

    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.

  11. #11
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: double buffering

    Thanks to drhex

Similar Threads

  1. Double Click Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2011, 14:12
  2. Full Screen Double Click?
    By winston2020 in forum Qt Programming
    Replies: 4
    Last Post: 3rd June 2010, 12:37
  3. Double Buffering for plot graphs
    By Tavit in forum Qt Programming
    Replies: 0
    Last Post: 20th March 2008, 13:10
  4. Double Buffer
    By ^NyAw^ in forum Qt Programming
    Replies: 23
    Last Post: 29th January 2008, 18:35
  5. Replies: 5
    Last Post: 12th January 2006, 15:40

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.