PDA

View Full Version : How to set limits on a standalone QwtScaleWidget?



SeanM
19th August 2014, 14:57
I'm trying to have a QwtScaleWidget that isn't associated with any specific plot, so I've instantiated one, put it in my layout and it shows up, so far so good. However, I can't seem to figure out how to manually set the limits on it! I've got the data I need in a vector, so it seems like it should be as simple as calling something like
scaleWidget->setLimits(vec.first(), vec.last())but there doesn't seem to be any function like that. I'm sure I'm missing something obvious...

Longer explanation of what I'm trying to do if it helps: I've got a plot container widget that holds multiple plots, all vertically aligned in a vertical scroll area. All the plots share a common x-axis time scale, but the y-axes can vary wildly. So to save vertical pixels, I'd like to display a single scale widget OUTSIDE of the scroll area, and then hide all the x-axis scale widgets of each plot. So the scale widget I'm trying to create isn't associated with any one plot, but as the data comes in (this is a real-time data stream), I just need to continously update the limits of the scale widget to match the underlying data.

Feel free to offer other suggestions if QwtScaleWidget isn't the correct thing to use, but it seem like it is, I'm just missing how to set the values of it.

Cah
19th August 2014, 20:26
I can't seem to figure out how to manually set the limits on it!
I would try...

QwtScaleDiv scaleDiv (lowerBound, upperBound);
scaleWidget->setScaleDiv (scaleDiv ) ;
or, depending on how far along you are...

QwtScaleDraw * scaleDraw = new QwtScaleDraw;
QwtScaleDiv scaleDiv (lowerBound, upperBound);
scaleDraw->setScaleDiv(scaleDiv);
scaleWidget->setScaleDraw (scaleDraw );

SeanM
19th August 2014, 21:14
Thanks for the reply. I thought I had tried that between my original post and your response, but it doesn't seem to have any effect? I'm trying to put together a minimal example right now...

It looks like you need to use a QwtScaleEngine to produce a proper QwtScaleDiv object, unless I'm still missing something obvious. Here's my code:
mainwindow.h

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void updateScale();

private:
Ui::MainWindow *ui;
QTimer* scaleUpdateTimer;
QwtScaleWidget* scaleWidget;
double limit;
QVBoxLayout* vbox;
QCheckBox* useScaleEngine;
};
mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
limit = 0;

vbox = new QVBoxLayout(this);
useScaleEngine = new QCheckBox("Use Scale Engine",this);
scaleUpdateTimer = new QTimer(this);
scaleWidget = new QwtScaleWidget(this);

vbox->addWidget(useScaleEngine);
vbox->addWidget(scaleWidget);
centralWidget()->setLayout(vbox);

useScaleEngine->setChecked(true);

scaleWidget->setAlignment(QwtScaleDraw::BottomScale);

scaleUpdateTimer->setInterval(1000);
connect(scaleUpdateTimer, SIGNAL(timeout()),
this, SLOT(updateScale()));
scaleUpdateTimer->start();
}

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

void MainWindow::updateScale()
{
if (useScaleEngine->isChecked())
{
QwtLinearScaleEngine engine;
scaleWidget->setScaleDiv(engine.divideScale(limit, limit+1, 10, 10));
} else
{
scaleWidget->setScaleDiv(QwtScaleDiv(limit, limit+1));
}
limit++;
}
By default it starts out using a scale engine to create the QwtScaleDiv, which appears to work. If you uncheck the checkbox, I just get a flat line for the scale widget, no tickmarks, no values, etc.

Cah
19th August 2014, 22:33
and then hide all the x-axis scale widgets of each plot

How did you hide the widgets...

plot->axisWidget(QwtPlot::xBottom)->hide(); //I do not believe this will do.
QwtPlot::updateLayout() //toggles hide to show for all enabled axes


plot->enableAxis(QwtPlot::xBottom, false); //This will

But...
QwtPlot::updateLayout() //ignores axes that are not enabled

Thus a heads up..

I

Uwe
20th August 2014, 08:50
By default it starts out using a scale engine to create the QwtScaleDiv, which appears to work. If you uncheck the checkbox, I just get a flat line for the scale widget, no tickmarks, no values, etc.
Sure because in this case you pass a scale division which has valid boundaries, but no ticks at all.

It doesn't matter whether you create the ticks using a scale engine or you fill in the vectors manually - as long as the tick values are inside the boundaries. Note that tick labels are only at major ticks.

Uwe

SeanM
20th August 2014, 14:42
How did you hide the widgets...
I'm using this method:

plot->enableAxis(QwtPlot::xBottom, false);


But...
QwtPlot::updateLayout() //ignores axes that are not enabled
Unless I'm misunderstanding your point, I'm fine with disabled axes being ignored, since I'm not going to be using them.