PDA

View Full Version : Qwtplot is not refreshed automatically with QTimer



GG2013
30th October 2013, 03:29
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



void RealPlotWidget::plotData(QStringList fileList, const char* title, QStringList cBoxList, bool stackedButton )
{
Plot *plot = new Plot(fileList, title);
........
........
QTimer *timer = new QTimer(this);
connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
timer->start(3000);
}


In dataplot.cpp



void Plot::populate()
{
qDebug()<<"populate:"<<QTime::currentTime();
curvePlot = false;
.........reads data + attaches to curve...
.....
replot();
curvePlot = true;
}




void Plot::setPlot()
{
qDebug()<<"setPlot:"<<QTime::currentTime();
if (this->curvePlot == true)
this->detachItems(); //removes all plot items incl curves, markers etc.

QTimer::singleShot(1000, this, SLOT(populate())); //populate() is called after 1sec
}

Santosh Reddy
30th October 2013, 14:07
Call update() after replot()

GG2013
30th October 2013, 23:40
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.

GG2013
1st November 2013, 03:56
Hi Everybody,
I haven't got it working yet.
Any idea/suggestion?
Thank you for your time.

ChrisW67
1st November 2013, 06:49
There is something you are not showing us. Post a minimal, compilable example that demonstrates the problem

GG2013
4th November 2013, 03:04
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)


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dataplot.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{

QStringList fileList;
fileList<< "/home/gita/Traffic/data/live-traffic-1-total-traffic-graph&table-packets.csv"
<< "/home/gita/Traffic/data/live-traffic-1-total-traffic-graph&table-bytes.csv";

Plot *plot = new Plot(fileList);
setCentralWidget(plot);

QTimer *timer = new QTimer(plot);
connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
timer->start(1000);
}

MainWindow::~MainWindow()
{
delete ui;
}


Original mainwindow.cpp (not working)


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tabs.h"

#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
//Add a tab bar at the top
QTabWidget* tab_main = new QTabWidget();
//tab_main->setTabsClosable(true); //shows a x but doesn't work i.e doesn't close tab

SystemTab *tab1 = new SystemTab;
LiveTrafficTab *tab2 = new LiveTrafficTab;
PacketPoolTab * tab3 = new PacketPoolTab;
ConnectionPoolTab *tab4 = new ConnectionPoolTab;
HostPoolTab *tab5 = new HostPoolTab;
ReportsTab *tab6 = new ReportsTab;
MoreTab *tab7 = new MoreTab;

tab_main->addTab(tab1, tr("System") );
tab_main->addTab(tab2, tr("Live Traffic"));
tab_main->addTab(tab3, tr("Packet Pool"));
tab_main->addTab(tab4, tr("Connection\nPool"));
tab_main->addTab(tab5, tr("Host Pool"));
tab_main->addTab(tab6, tr("Reports"));
tab_main->addTab(tab7, tr("More"));

QPushButton *start = new QPushButton(tab_main);
QPushButton *stop = new QPushButton(tab_main);
start->setStyleSheet("QPushButton {"
"margin-left: 1px;"
"border: 2px solid;"
"height: 50px;"
"width: 50px;"
"font: 12pt;"
"font: bold;"
"background-color:white;"
"border-top-left-radius:20px;"
"border-top-right-radius: 20px;"
"border-bottom-left-radius: 20px;"
"border-bottom-right-radius: 20px;"
"} QPushButton::selected{color:#000000; background-color:red;}"
); //all features should be set in one statement with stylesheet
stop->setStyleSheet("QPushButton {"
"margin-left: 1px;"
"border: 2px solid;"
"height: 50px;"
"width: 50px;"
"font: 12pt;"
"font: bold;"
"background-color:white;"
"border-top-left-radius:20px;"
"border-top-right-radius: 20px;"
"border-bottom-left-radius: 20px;"
"border-bottom-right-radius: 20px;"
"} QPushButton::selected{color:#000000; background-color:red;}"
); //all features should be set in one statement with stylesheet

start->setIcon(QIcon("/home/gita/Traffic/start-icon.png"));
tab_main->setCornerWidget(start, Qt::TopLeftCorner);
stop->setIcon(QIcon("/home/gita/Traffic/Stop-Normal-Red-icon.png"));
tab_main->setCornerWidget(stop, Qt::TopRightCorner);

//tab_main->setTabShape(QTabWidget::Triangular); //default is rectangular

tab_main->setStyleSheet("QTabBar::tab {"
"selection-color: yellow;"
"margin-left: 1px;"
"border: 2px solid;"
"height: 50px;"
"width: 150px;"
"font:12pt;"
"font: bold;"
"background-color:white;"
"border-top-left-radius:20px;"
"border-top-right-radius: 20px;"
"border-bottom-left-radius: 20px;"
"border-bottom-right-radius: 20px;"
"} QTabBar::tab:selected{color:#000000; background-color:#C2DFFF; QTabBar::pane{color:red;}}"
); //all features should be set in one statement with stylesheet

QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->addWidget(tab_main);
vLayout->setSpacing(2);

QWidget * centralWidget = new QWidget();
setCentralWidget(centralWidget);

//tab_main->setFixedSize(500,500);

centralWidget->setLayout(vLayout);
}

MainWindow::~MainWindow()
{
delete ui;
}


tabs.cpp


LiveTrafficTab::LiveTrafficTab(QWidget *parent): QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout;
HorizontalTabWidget *tabwidget = new HorizontalTabWidget;

QWidget *page1 = new TabTwo_PageOne;
QWidget *page2 = new TabTwo_PageTwo;
QWidget *page3 = new TabTwo_PageThree;
QWidget *page4 = new TabTwo_PageFour;
QWidget *page5 = new TabTwo_PageFive;

tabwidget->addTab(page1, "Total\nTraffic");
tabwidget->addTab(page2, "Network\nLayer");
tabwidget->addTab(page3, "Transport\nLayer");
tabwidget->addTab(page4, "Application\nLayer");
tabwidget->addTab(page5, "Host");
//tabwidget->setStyleSheet("HorizontalTabBar::tab{height: 0px; width: 50px; font: 10pt} HorizontalTabBar::tab:selected{color:#000000; background-color:#C2DFFF;}");

setLayout(layout);
layout->addWidget(tabwidget);
tabwidget->setTabPosition(HorizontalTabWidget::West);
}


page.cpp


TabTwo_PageOne::TabTwo_PageOne(QWidget *parent): QWidget(parent)
{
RealPlotWidget *plot = new RealPlotWidget;
........
plot->plotData(fileList, "Total Hourly Traffic", comboBoxList, 0);
........
}

void RealPlotWidget::plotData(QStringList fileList, const char* title, QStringList cBoxList, bool clickButton, bool stackedButton )
{
Plot *plot = new Plot(fileList, title);
.............
QTimer *timer = new QTimer(plot);
connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
timer->start(1000);
}

GG2013
5th November 2013, 03:53
Hi All,
No idea? Just wondering do I need to use eventFilter?
Any help would be greatly appreciated.

ChrisW67
5th November 2013, 08:04
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.

GG2013
6th November 2013, 03:36
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.