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
) :{
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);
connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Original mainwindow.cpp (not working)
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tabs.h"
#include <QHBoxLayout>
MainWindow
::MainWindow(QWidget *parent
) :{
//Add a tab bar at the top
//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"));
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
vLayout->addWidget(tab_main);
vLayout->setSpacing(2);
setCentralWidget(centralWidget);
//tab_main->setFixedSize(500,500);
centralWidget->setLayout(vLayout);
}
MainWindow::~MainWindow()
{
delete ui;
}
#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;
}
To copy to clipboard, switch view to plain text mode
tabs.cpp
{
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
page.cpp
{
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);
.............
connect (timer, SIGNAL(timeout()), plot, SLOT(setPlot())); //setPlot() is called every 3secs
timer->start(1000);
}
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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks