Results 1 to 11 of 11

Thread: Refresh problem with QPaintEvent

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Refresh problem with QPaintEvent

    Hi,

    I'm using NightCharts to draw 3 piecharts on a QDialog. When I draw those three charts in paintEvent() and run the program, one of the charts is not painted fully. But when I resize the QDialog manually, the incomplete chart is now fully painted. How do I make this paint properly.

    Qt Code:
    1. void Dialog::paintEvent(QPaintEvent *event)
    2. {
    3. Q_UNUSED(event);
    4. QPainter painter(this);
    5. addGraphs(&painter);
    6. }
    7.  
    8. void Dialog::addGraphs(QPainter *qp)
    9. {
    10. Nightcharts chart1;
    11. chart1.setShadows(false);
    12. chart1.setType(Nightcharts::Pie);
    13. chart1.setLegendType(Nightcharts::Vertical);
    14. chart1.setCords(50, 50, 80, 80);
    15. chart1.addPiece("Item1",QColor(200,10,50),34);
    16. chart1.addPiece("Item2",Qt::green,27);
    17. chart1.addPiece("Item3",Qt::cyan,14);
    18. chart1.addPiece("Item4",Qt::yellow,7);
    19. chart1.addPiece("Item5",Qt::blue,4);
    20. chart1.draw(qp);
    21. // chart1.drawLegend(qp);
    22.  
    23. Nightcharts chart2;
    24. chart2.setShadows(false);
    25. chart2.setType(Nightcharts::Pie);
    26. chart2.setLegendType(Nightcharts::Vertical);
    27. chart2.setCords(300, 50, 80, 80);
    28. chart2.addPiece("Item2",Qt::green,45);
    29. chart2.addPiece("Item3",Qt::cyan,25);
    30. chart2.addPiece("Item4",Qt::yellow,15);
    31. chart2.addPiece("Item5",Qt::blue,15);
    32. chart2.draw(qp);
    33. // chart2.drawLegend(qp);
    34.  
    35. Nightcharts chart3;
    36. chart3.setShadows(false);
    37. chart3.setType(Nightcharts::Pie);
    38. chart3.setLegendType(Nightcharts::Vertical);
    39. chart3.setCords(550, 50, 80, 80);
    40. chart3.addPiece("Item1",QColor(200,10,50), 25);
    41. chart3.addPiece("Item3",Qt::cyan, 25);
    42. chart3.addPiece("Item4",Qt::yellow, 15);
    43. chart3.addPiece("Item5",Qt::blue, 15);
    44. chart3.draw(qp);
    45. // chart3.drawLegend(qp);
    46. }
    To copy to clipboard, switch view to plain text mode 

    Before resizing QDialog
    OnExec.jpg

    After resizing QDialog
    Resize.jpg

    Kindly help me. Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Refresh problem with QPaintEvent

    Are you sure you should be recreating those charts in each and every paint event?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Refresh problem with QPaintEvent

    Not every paintEvent(), but I have to update those charts based on some activities.

    And with NIghtCharts I'm not able to make it as a widget. Are there any other options ?
    Thank you.
    Last edited by rawfool; 23rd April 2013 at 10:24.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Refresh problem with QPaintEvent

    I don't know how NightCharts works but it seems to me you should keep instances of "Nightcharts" as members of your class and in the paint event simply call draw() on each of them passing the painter where they should be drawn. Also make sure that each time you modify any of the charts, you call update( ) on the widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Refresh problem with QPaintEvent

    Quote Originally Posted by wysota
    I don't know how NightCharts works but it seems to me you should keep instances of "Nightcharts" as members of your class and in the paint event simply call draw() on each of them passing the painter where they should be drawn. Also make sure that each time you modify any of the charts, you call update( ) on the widget.
    @wysota, It looks like the NightCharts is designed to be created every time inside the QPaintEvent, having it as class member will not work. (meaning it is not reusable, no means to reset or clear the data in pie, so no way to write new data, only way out is create a new instance)

    @rawfool
    The problem you see is due to a bug in NightCharts. A member variable double Nightcharts::palpha; is not initialized in the ctor, just initializing it to 0 in the ctor of Nightcharts will fix the problem (unless there are other problems). Good Luck, and also report the bug to the author.

    And reason the NightCharts is creatd every time in the QpaintEvent is creating this random behaviour, as each time the NightCharts object is created double Nightcharts::palpha; takes a random value and a undefined drawing of the pie chart happens
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    rawfool (23rd April 2013)

  7. #6
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Refresh problem with QPaintEvent

    Setting palpha = 0 in ctor worked.

    no means to reset or clear the data in pie, so no way to write new data, only way out is create a new instance
    So, how do I write new data ?
    Thank you.

  8. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Refresh problem with QPaintEvent

    no means to reset or clear the data in pie, so no way to write new data, only way out is create a new instance
    It already gives the solution
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #8
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Refresh problem with QPaintEvent

    Ok. But I' unable to think how to create a new instance on some update activity.
    Based on your answer, I created a singleshot timer in my ctor to update the graph. But i was not the way.

    So, should I use QList or something like tht to create a new instance in run-time to append the new chart & delete the older one ? Kindly help me understand this. Thank you.


    Qt Code:
    1. void Dialog::paintEvent(QPaintEvent *event)
    2. {
    3. Q_UNUSED(event);
    4. QPainter painter(this);
    5. addGraphs(&painter, 20, 30, 50);
    6. }
    7.  
    8. void Dialog::addGraphs(QPainter *qp, int i, int j, int k)
    9. {
    10. Nightcharts chart1;
    11. chart1.setShadows(false);
    12. chart1.setType(Nightcharts::Pie);
    13. chart1.setLegendType(Nightcharts::Vertical);
    14. chart1.setCords(50, 50, 80, 80);
    15. chart1.addPiece("Item1",QColor(200,10,50), i);
    16. chart1.addPiece("Item2",Qt::green, j);
    17. chart1.addPiece("Item3",Qt::cyan, k);
    18. chart1.draw(qp);
    19. chart1.drawLegend(qp);
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rawfool; 23rd April 2013 at 13:39.

  10. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Refresh problem with QPaintEvent

    What you want to do?

    If you want to update the chart, then give new values and update() that's all

    I am afraid the question you are asking is making us move us away from Qt topic.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  11. #10
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Refresh problem with QPaintEvent

    I've created a dummy updater(singleshot timer, to update after few seconds) to update to new values.
    Now I don't know whr to call updateGraph() function. I've done something like this, but it's not updating the graphs.
    Can you give some sample code to handle this pls.

    Qt Code:
    1. Dialog::Dialog(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. this->setMinimumSize(800, 250);
    5. QTimer::singleShot(5000, this, SLOT(updateGraph()));
    6. }
    7.  
    8. Dialog::~Dialog()
    9. {
    10. }
    11.  
    12.  
    13. void Dialog::paintEvent(QPaintEvent *event)
    14. {
    15. Q_UNUSED(event);
    16. QPainter painter(this);
    17. addGraphs(&painter, 20, 30, 50);
    18. }
    19.  
    20. void Dialog::addGraphs(QPainter *qp, int i, int j, int k)
    21. {
    22. Nightcharts chart1;
    23. chart1.setShadows(false);
    24. chart1.setType(Nightcharts::Pie);
    25. chart1.setLegendType(Nightcharts::Vertical);
    26. chart1.setCords(50, 50, 80, 80);
    27. chart1.addPiece("Item1",QColor(200,10,50), i);
    28. chart1.addPiece("Item2",Qt::green, j);
    29. chart1.addPiece("Item3",Qt::cyan, k);
    30. chart1.draw(qp);
    31. chart1.drawLegend(qp);
    32.  
    33. Nightcharts chart2;
    34. chart2.setShadows(false);
    35. chart2.setType(Nightcharts::Pie);
    36. chart2.setLegendType(Nightcharts::Vertical);
    37. chart2.setCords(300, 50, 80, 80);
    38. chart2.addPiece("Item2",Qt::green, j);
    39. chart2.addPiece("Item3",Qt::cyan,k);
    40. chart2.addPiece("Item4",Qt::yellow,i);
    41. chart2.draw(qp);
    42. chart2.drawLegend(qp);
    43.  
    44. Nightcharts chart3;
    45. chart3.setShadows(false);
    46. chart3.setType(Nightcharts::Pie);
    47. chart3.setLegendType(Nightcharts::Vertical);
    48. chart3.setCords(550, 50, 80, 80);
    49. chart3.addPiece("Item1",QColor(200,10,50), k);
    50. chart3.addPiece("Item3",Qt::cyan, i);
    51. chart3.addPiece("Item4",Qt::yellow, j);
    52. chart3.draw(qp);
    53. chart3.drawLegend(qp);
    54. }
    55.  
    56. void Dialog::updateGraph()
    57. {
    58. qDebug("Updating after 5 seconds");
    59. QPainter painter(this);
    60. addGraphs(&painter, 50, 20, 30);
    61. this->update();
    62. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rawfool; 23rd April 2013 at 14:01.

  12. #11
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Refresh problem with QPaintEvent

    You are on you own
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3. #include "nightcharts.h"
    4.  
    5. class Dialog : public QDialog
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10.  
    11. explicit Dialog(QWidget *parent = 0)
    12. : QDialog(parent)
    13. , index(0)
    14. {
    15. this->setMinimumSize(800, 250);
    16. startTimer(1000);
    17. }
    18.  
    19. protected:
    20.  
    21. void paintEvent(QPaintEvent *event)
    22. {
    23. Q_UNUSED(event);
    24. QPainter painter(this);
    25. addGraphs(&painter, A[index], B[index], C[index]);
    26. }
    27.  
    28. private:
    29. static const int A[10];
    30. static const int B[10];
    31. static const int C[10];
    32. int index;
    33.  
    34. void addGraphs(QPainter *qp, int i, int j, int k)
    35. {
    36. Nightcharts chart1;
    37. chart1.setShadows(false);
    38. chart1.setType(Nightcharts::Pie);
    39. chart1.setLegendType(Nightcharts::Vertical);
    40. chart1.setCords(50, 50, 80, 80);
    41. chart1.addPiece("Item1", QColor(200,10,50), i);
    42. chart1.addPiece("Item2", Qt::green, j);
    43. chart1.addPiece("Item3", Qt::cyan, k);
    44. chart1.draw(qp);
    45. chart1.drawLegend(qp);
    46.  
    47. Nightcharts chart2;
    48. chart2.setShadows(false);
    49. chart2.setType(Nightcharts::Pie);
    50. chart2.setLegendType(Nightcharts::Vertical);
    51. chart2.setCords(300, 50, 80, 80);
    52. chart2.addPiece("Item2",Qt::green, j);
    53. chart2.addPiece("Item3",Qt::cyan,k);
    54. chart2.addPiece("Item4",Qt::yellow,i);
    55. chart2.draw(qp);
    56. chart2.drawLegend(qp);
    57.  
    58. Nightcharts chart3;
    59. chart3.setShadows(false);
    60. chart3.setType(Nightcharts::Pie);
    61. chart3.setLegendType(Nightcharts::Vertical);
    62. chart3.setCords(550, 50, 80, 80);
    63. chart3.addPiece("Item1",QColor(200,10,50), k);
    64. chart3.addPiece("Item3",Qt::cyan, i);
    65. chart3.addPiece("Item4",Qt::yellow, j);
    66. chart3.draw(qp);
    67. chart3.drawLegend(qp);
    68. }
    69.  
    70. void timerEvent(QTimerEvent *)
    71. {
    72. index++;
    73. index = index % 10;
    74. this->update();
    75. }
    76.  
    77. };
    78.  
    79. const int Dialog::A[] = { 10, 20, 30, 40, 50, 60, 70, 70, 50, 70 };
    80. const int Dialog::B[] = { 20, 30, 40, 50, 40, 30, 20, 10, 20, 30 };
    81. const int Dialog::C[] = { 70, 50, 30, 10, 10, 10, 10, 20, 30, 40 };
    82.  
    83. int main(int argc, char *argv[])
    84. {
    85. QApplication a(argc, argv);
    86.  
    87. Dialog dialog;
    88. dialog.exec();
    89.  
    90. return a.exec();
    91. }
    92.  
    93. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  13. The following user says thank you to Santosh Reddy for this useful post:

    rawfool (23rd April 2013)

Similar Threads

  1. QComboBox refresh problem
    By waynew in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2011, 12:30
  2. Replies: 2
    Last Post: 14th January 2011, 15:09
  3. QWidget Refresh Problem
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 15th January 2008, 11:24
  4. QpaintEvent region problem with QPopupmenus
    By Qtkiller in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2007, 09:18
  5. QTableView refresh problem
    By tebessum in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2006, 11:22

Tags for this Thread

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.