Results 1 to 9 of 9

Thread: Qwtplot is not refreshed automatically with QTimer

  1. #1
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qwtplot is not refreshed automatically with QTimer

    Hallo All,

    I would like to refresh my plot every 3 secs. In otherwords, the cycle of plot display, clear screen, and plot appears with newly read data will happen every 3secs. The problem is unless I use mouse click the plot or clear screen remains still. In otherwords, I need to click/hover mouse to see any change on screen. The setPlot() is called at an interval of 3sec or more (as expected), and the timing of calling the populate() is also ok. But the outputs (qDebug()) are printed only when there is a mouse movement. However, I expect the refreshing to happen by itself.

    Any help would be greatly appreciated.

    In page.cpp

    Qt Code:
    1. void RealPlotWidget::plotData(QStringList fileList, const char* title, QStringList cBoxList, bool stackedButton )
    2. {
    3. Plot *plot = new Plot(fileList, title);
    4. ........
    5. ........
    6. QTimer *timer = new QTimer(this);
    7. connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
    8. timer->start(3000);
    9. }
    To copy to clipboard, switch view to plain text mode 

    In dataplot.cpp
    Qt Code:
    1. void Plot::populate()
    2. {
    3. qDebug()<<"populate:"<<QTime::currentTime();
    4. curvePlot = false;
    5. .........reads data + attaches to curve...
    6. .....
    7. replot();
    8. curvePlot = true;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Plot::setPlot()
    2. {
    3. qDebug()<<"setPlot:"<<QTime::currentTime();
    4. if (this->curvePlot == true)
    5. this->detachItems(); //removes all plot items incl curves, markers etc.
    6.  
    7. QTimer::singleShot(1000, this, SLOT(populate())); //populate() is called after 1sec
    8. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Qwtplot is not refreshed automatically with QTimer

    Call update() after replot()
    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.

  3. #3
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwtplot is not refreshed automatically with QTimer

    Thank you Santosh for your reply.
    I have tried to use update() after replot()- still the same. Previously I tried with Paintevent as well & got the same behaviour.

    PS. I use Zoomer as in Qwt example stockchart & it works fine in my application i.e left button to zoom-in and right button to zoom out. However, when I use QTimer as mentioned before the zoom-out does not work. Just wondering if somehow the timer and mouse event getting connected.
    Last edited by GG2013; 31st October 2013 at 04:56.

  4. #4
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwtplot is not refreshed automatically with QTimer

    Hi Everybody,
    I haven't got it working yet.
    Any idea/suggestion?
    Thank you for your time.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qwtplot is not refreshed automatically with QTimer

    There is something you are not showing us. Post a minimal, compilable example that demonstrates the problem

  6. #6
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwtplot is not refreshed automatically with QTimer

    Thanks Chris.
    I have narrowed down the problem. I now directly create the plot object in mainwindow.cpp, and plot is used as central widget. To my surprise this code is working fine i.e the plot is refreshed automatically as expected.

    The LiveTrafficTab class as in the original mainwindow.cpp (see below) has a constructor (in tabs.cpp) where page1 object is created from TabTwo_PageOne class. The plot is created inside TabTwo_PageOne constructor, and QTimer is used to refresh the plots (which is not working properly). For the original case, I have shown only relevant potions to show how Plot & QTimer have been used.

    Any idea on how to use the timer correctly so that my original code structure works?
    Thank you for your time.

    mainwindow.cpp (working)
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "dataplot.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent), ui(new Ui::MainWindow)
    7. {
    8.  
    9. QStringList fileList;
    10. fileList<< "/home/gita/Traffic/data/live-traffic-1-total-traffic-graph&table-packets.csv"
    11. << "/home/gita/Traffic/data/live-traffic-1-total-traffic-graph&table-bytes.csv";
    12.  
    13. Plot *plot = new Plot(fileList);
    14. setCentralWidget(plot);
    15.  
    16. QTimer *timer = new QTimer(plot);
    17. connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
    18. timer->start(1000);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 

    Original mainwindow.cpp (not working)
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "tabs.h"
    4.  
    5. #include <QHBoxLayout>
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent), ui(new Ui::MainWindow)
    9. {
    10. //Add a tab bar at the top
    11. QTabWidget* tab_main = new QTabWidget();
    12. //tab_main->setTabsClosable(true); //shows a x but doesn't work i.e doesn't close tab
    13.  
    14. SystemTab *tab1 = new SystemTab;
    15. LiveTrafficTab *tab2 = new LiveTrafficTab;
    16. PacketPoolTab * tab3 = new PacketPoolTab;
    17. ConnectionPoolTab *tab4 = new ConnectionPoolTab;
    18. HostPoolTab *tab5 = new HostPoolTab;
    19. ReportsTab *tab6 = new ReportsTab;
    20. MoreTab *tab7 = new MoreTab;
    21.  
    22. tab_main->addTab(tab1, tr("System") );
    23. tab_main->addTab(tab2, tr("Live Traffic"));
    24. tab_main->addTab(tab3, tr("Packet Pool"));
    25. tab_main->addTab(tab4, tr("Connection\nPool"));
    26. tab_main->addTab(tab5, tr("Host Pool"));
    27. tab_main->addTab(tab6, tr("Reports"));
    28. tab_main->addTab(tab7, tr("More"));
    29.  
    30. QPushButton *start = new QPushButton(tab_main);
    31. QPushButton *stop = new QPushButton(tab_main);
    32. start->setStyleSheet("QPushButton {"
    33. "margin-left: 1px;"
    34. "border: 2px solid;"
    35. "height: 50px;"
    36. "width: 50px;"
    37. "font: 12pt;"
    38. "font: bold;"
    39. "background-color:white;"
    40. "border-top-left-radius:20px;"
    41. "border-top-right-radius: 20px;"
    42. "border-bottom-left-radius: 20px;"
    43. "border-bottom-right-radius: 20px;"
    44. "} QPushButton::selected{color:#000000; background-color:red;}"
    45. ); //all features should be set in one statement with stylesheet
    46. stop->setStyleSheet("QPushButton {"
    47. "margin-left: 1px;"
    48. "border: 2px solid;"
    49. "height: 50px;"
    50. "width: 50px;"
    51. "font: 12pt;"
    52. "font: bold;"
    53. "background-color:white;"
    54. "border-top-left-radius:20px;"
    55. "border-top-right-radius: 20px;"
    56. "border-bottom-left-radius: 20px;"
    57. "border-bottom-right-radius: 20px;"
    58. "} QPushButton::selected{color:#000000; background-color:red;}"
    59. ); //all features should be set in one statement with stylesheet
    60.  
    61. start->setIcon(QIcon("/home/gita/Traffic/start-icon.png"));
    62. tab_main->setCornerWidget(start, Qt::TopLeftCorner);
    63. stop->setIcon(QIcon("/home/gita/Traffic/Stop-Normal-Red-icon.png"));
    64. tab_main->setCornerWidget(stop, Qt::TopRightCorner);
    65.  
    66. //tab_main->setTabShape(QTabWidget::Triangular); //default is rectangular
    67.  
    68. tab_main->setStyleSheet("QTabBar::tab {"
    69. "selection-color: yellow;"
    70. "margin-left: 1px;"
    71. "border: 2px solid;"
    72. "height: 50px;"
    73. "width: 150px;"
    74. "font:12pt;"
    75. "font: bold;"
    76. "background-color:white;"
    77. "border-top-left-radius:20px;"
    78. "border-top-right-radius: 20px;"
    79. "border-bottom-left-radius: 20px;"
    80. "border-bottom-right-radius: 20px;"
    81. "} QTabBar::tab:selected{color:#000000; background-color:#C2DFFF; QTabBar::pane{color:red;}}"
    82. ); //all features should be set in one statement with stylesheet
    83.  
    84. QVBoxLayout* vLayout = new QVBoxLayout();
    85. vLayout->addWidget(tab_main);
    86. vLayout->setSpacing(2);
    87.  
    88. QWidget * centralWidget = new QWidget();
    89. setCentralWidget(centralWidget);
    90.  
    91. //tab_main->setFixedSize(500,500);
    92.  
    93. centralWidget->setLayout(vLayout);
    94. }
    95.  
    96. MainWindow::~MainWindow()
    97. {
    98. delete ui;
    99. }
    To copy to clipboard, switch view to plain text mode 

    tabs.cpp
    Qt Code:
    1. LiveTrafficTab::LiveTrafficTab(QWidget *parent): QWidget(parent)
    2. {
    3. QHBoxLayout *layout = new QHBoxLayout;
    4. HorizontalTabWidget *tabwidget = new HorizontalTabWidget;
    5.  
    6. QWidget *page1 = new TabTwo_PageOne;
    7. QWidget *page2 = new TabTwo_PageTwo;
    8. QWidget *page3 = new TabTwo_PageThree;
    9. QWidget *page4 = new TabTwo_PageFour;
    10. QWidget *page5 = new TabTwo_PageFive;
    11.  
    12. tabwidget->addTab(page1, "Total\nTraffic");
    13. tabwidget->addTab(page2, "Network\nLayer");
    14. tabwidget->addTab(page3, "Transport\nLayer");
    15. tabwidget->addTab(page4, "Application\nLayer");
    16. tabwidget->addTab(page5, "Host");
    17. //tabwidget->setStyleSheet("HorizontalTabBar::tab{height: 0px; width: 50px; font: 10pt} HorizontalTabBar::tab:selected{color:#000000; background-color:#C2DFFF;}");
    18.  
    19. setLayout(layout);
    20. layout->addWidget(tabwidget);
    21. tabwidget->setTabPosition(HorizontalTabWidget::West);
    22. }
    To copy to clipboard, switch view to plain text mode 

    page.cpp
    Qt Code:
    1. TabTwo_PageOne::TabTwo_PageOne(QWidget *parent): QWidget(parent)
    2. {
    3. RealPlotWidget *plot = new RealPlotWidget;
    4. ........
    5. plot->plotData(fileList, "Total Hourly Traffic", comboBoxList, 0);
    6. ........
    7. }
    8.  
    9. void RealPlotWidget::plotData(QStringList fileList, const char* title, QStringList cBoxList, bool clickButton, bool stackedButton )
    10. {
    11. Plot *plot = new Plot(fileList, title);
    12. .............
    13. QTimer *timer = new QTimer(plot);
    14. connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
    15. timer->start(1000);
    16. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwtplot is not refreshed automatically with QTimer

    Hi All,
    No idea? Just wondering do I need to use eventFilter?
    Any help would be greatly appreciated.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qwtplot is not refreshed automatically with QTimer

    Your last listing, page.cpp, line 14 connects the timeout to a local Plot object (a new one created and likely leaked every time plotData() is called). This is not in any way related to the RealPlotWidget created at line 3 of that listing.

  9. #9
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwtplot is not refreshed automatically with QTimer

    Thanks Chris. That's a good pick up. I now don't use plotData(), and instead create plot (& other stuffi i.e everything in plotData()) inside RealPlotWidget constructor. However,
    it's still the same. Auto refreshing works only in mainwindow i.e if plot is created and set in the central widget.

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 20:47
  2. Replies: 15
    Last Post: 4th August 2012, 20:11
  3. Checkboxes in Treeview do not get refreshed
    By mesch.t in forum Newbie
    Replies: 5
    Last Post: 13th April 2011, 21:53
  4. QTableView is not refreshed
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2009, 22:23
  5. Replies: 6
    Last Post: 14th May 2009, 13:02

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.