#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qwt_scale_engine.h>
#include <qwt_date_scale_draw.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_grid.h>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QScrollArea>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
// create horizontal layout above scroll area, with:
// QPushButton QwtScaleWidget QLabel
connect(pbAddPlot, SIGNAL(clicked()),
this, SLOT(addPlot()));
scaleWidget->setAutoFillBackground(true);
scaleWidget->setBorderDist(20,20);
QwtDateScaleDraw* dateScale = new QwtDateScaleDraw();
dateScale->setDateFormat(QwtDate::Millisecond, "ss.zzz");
dateScale->setDateFormat(QwtDate::Second, "mm:ss.zzz");
dateScale->setDateFormat(QwtDate::Minute, "hh:mm:ss.zzz");
dateScale->setDateFormat(QwtDate::Hour, "hh:mm:ss.zzz");
scaleWidget->setScaleDraw(dateScale);
scaleWidget->setPalette(pal);
hbox->addWidget(pbAddPlot);
hbox->addWidget(scaleWidget);
hbox
->addWidget
(new QLabel("Text Label",
this));
// create the scroll area to hold the vertically aligned plots
scroll->setWidgetResizable(true);
scroll
->setSizeAdjustPolicy
(QScrollArea::AdjustToContents);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroll->setWidget(widget);
widget->setLayout(scrollVBox);
vbox->addLayout(hbox);
vbox->addWidget(scroll);
centralWidget()->setLayout(vbox);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addPlot()
{
plot->setMinimumHeight(250);
plot
->enableAxis
(QwtPlot::yRight,
true);
//plot->enableAxis(QwtPlot::xBottom, false); // <- this needs to be enabled in the real code
plot->setAutoFillBackground(true);
pal = plot->palette();
plot->setPalette(pal);
grid->attach(plot);
QwtDateScaleDraw* dateScale2 = new QwtDateScaleDraw();
dateScale2->setDateFormat(QwtDate::Millisecond, "ss.zzz");
dateScale2->setDateFormat(QwtDate::Second, "mm:ss.zzz");
dateScale2->setDateFormat(QwtDate::Minute, "hh:mm:ss.zzz");
dateScale2->setDateFormat(QwtDate::Hour, "hh:mm:ss.zzz");
bottomScale->setScaleDraw(dateScale2);
bottomScale->setAutoFillBackground(true);
pal = bottomScale->palette();
bottomScale->setPalette(pal);
bottomScale->repaint();
leftScale->setAutoFillBackground(true);
rightScale->setAutoFillBackground(true);
pal = leftScale->palette();
leftScale->setPalette(pal);
leftScale->repaint();
rightScale->setPalette(pal);
rightScale->repaint();
label
= new QLabel("Custom Legend\nLeft", scroll
);
label->setAutoFillBackground(true);
label->setAlignment(Qt::AlignCenter);
label->setFixedWidth(100);
hbox->addWidget(label,0);
hbox->addWidget(plot,10);
label
= new QLabel("Custom Legend\nRight", scroll
);
label->setAutoFillBackground(true);
label->setAlignment(Qt::AlignCenter);
label->setFixedWidth(100);
hbox->addWidget(label,0);
widget->setLayout(hbox);
scrollVBox->addWidget(widget);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qwt_scale_engine.h>
#include <qwt_date_scale_draw.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_grid.h>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QScrollArea>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// create horizontal layout above scroll area, with:
// QPushButton QwtScaleWidget QLabel
QPushButton* pbAddPlot = new QPushButton("Add Plot", this);
connect(pbAddPlot, SIGNAL(clicked()),
this, SLOT(addPlot()));
scaleWidget = new QwtScaleWidget(this);
scaleWidget->setAutoFillBackground(true);
scaleWidget->setBorderDist(20,20);
scaleWidget->setAlignment(QwtScaleDraw::BottomScale);
QwtDateScaleDraw* dateScale = new QwtDateScaleDraw();
dateScale->setDateFormat(QwtDate::Millisecond, "ss.zzz");
dateScale->setDateFormat(QwtDate::Second, "mm:ss.zzz");
dateScale->setDateFormat(QwtDate::Minute, "hh:mm:ss.zzz");
dateScale->setDateFormat(QwtDate::Hour, "hh:mm:ss.zzz");
scaleWidget->setScaleDraw(dateScale);
QPalette pal = scaleWidget->palette();
pal.setColor(QPalette::Window, QColor(0,255,255));
scaleWidget->setPalette(pal);
QHBoxLayout* hbox = new QHBoxLayout();
hbox->addWidget(pbAddPlot);
hbox->addWidget(scaleWidget);
hbox->addWidget(new QLabel("Text Label", this));
// create the scroll area to hold the vertically aligned plots
scroll = new QScrollArea(this);
scroll->setWidgetResizable(true);
scroll->setSizeAdjustPolicy(QScrollArea::AdjustToContents);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QWidget* widget = new QWidget(this);
scroll->setWidget(widget);
scrollVBox = new QVBoxLayout(widget);
widget->setLayout(scrollVBox);
QVBoxLayout* vbox = new QVBoxLayout();
vbox->addLayout(hbox);
vbox->addWidget(scroll);
centralWidget()->setLayout(vbox);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addPlot()
{
QPalette pal;
QwtPlot* plot;
plot = new QwtPlot(scroll);
plot->setMinimumHeight(250);
plot->enableAxis(QwtPlot::yRight,true);
//plot->enableAxis(QwtPlot::xBottom, false); // <- this needs to be enabled in the real code
plot->setAutoFillBackground(true);
pal = plot->palette();
pal.setColor(QPalette::Window, QColor(255,255,0));
plot->setPalette(pal);
plot->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
QwtPlotGrid* grid = new QwtPlotGrid();
grid->attach(plot);
QwtScaleWidget* bottomScale = plot->axisWidget(QwtPlot::xBottom);
QwtDateScaleDraw* dateScale2 = new QwtDateScaleDraw();
dateScale2->setDateFormat(QwtDate::Millisecond, "ss.zzz");
dateScale2->setDateFormat(QwtDate::Second, "mm:ss.zzz");
dateScale2->setDateFormat(QwtDate::Minute, "hh:mm:ss.zzz");
dateScale2->setDateFormat(QwtDate::Hour, "hh:mm:ss.zzz");
bottomScale->setScaleDraw(dateScale2);
bottomScale->setAutoFillBackground(true);
pal = bottomScale->palette();
pal.setColor(QPalette::Window, QColor(0,255,255));
bottomScale->setPalette(pal);
bottomScale->repaint();
QwtScaleWidget* leftScale = plot->axisWidget(QwtPlot::yLeft);
QwtScaleWidget* rightScale = plot->axisWidget(QwtPlot::yRight);
leftScale->setAutoFillBackground(true);
rightScale->setAutoFillBackground(true);
pal = leftScale->palette();
pal.setColor(QPalette::Window, QColor(0,255,0));
leftScale->setPalette(pal);
leftScale->repaint();
rightScale->setPalette(pal);
rightScale->repaint();
QHBoxLayout* hbox = new QHBoxLayout();
QLabel* label;
label = new QLabel("Custom Legend\nLeft", scroll);
label->setAutoFillBackground(true);
label->setAlignment(Qt::AlignCenter);
label->setFixedWidth(100);
label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
hbox->addWidget(label,0);
hbox->addWidget(plot,10);
label = new QLabel("Custom Legend\nRight", scroll);
label->setAutoFillBackground(true);
label->setAlignment(Qt::AlignCenter);
label->setFixedWidth(100);
label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
hbox->addWidget(label,0);
QWidget* widget = new QWidget(scroll->widget());
widget->setLayout(hbox);
scrollVBox->addWidget(widget);
}
To copy to clipboard, switch view to plain text mode
Here's what the screen looks like after you add a couple plots (via the Add Plot button) and resize and scroll it a little:
Bookmarks