PDA

View Full Version : [Solved] Adding a qwtPlot to a layout



cl90
4th March 2015, 10:22
Hi!

What i'm trying to do should be pretty easy. But somehow it doesn't work.
All i'm trying to do is to add a qwtPlot to an qt application, without launching the plot in its own application.
Every time i try, i end up with 2 application windows (one for the application and one for the plot) or with no application (because it crashes).
I tired this for quiet some time without any progress, so maybe you can help me out.

If i run this somewhere in my code i end up with 2 application windows. (not what i want)


QwtPlot* plot = new QwtPlot();
plot->setTitle( "Plot Demo" );
plot->setCanvasBackground( Qt::white );
plot->setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );
plot->insertLegend( new QwtLegend() );
[...]
plot->show();

What i want is to add a plot to a specified layout inside my application.
every time i try to add a qwtPlot as a widget, my application crashes.


QwtPlot* plot = new QwtPlot();
[...]
ui->horizontalLayout->addWidget(plot);


Is this even Possible? i cant even find anything about this.

Uwe
4th March 2015, 11:24
Everything fine with your code snippet - putting QwtPlot inside a layout is probably what almost all applications do.

The bug is somewehere else in your code ( unrelated to Qwt ) - the debugger is your friend.

Uwe

Santosh Reddy
4th March 2015, 11:32
I will take a guess, clean build the application (just your application not Qwt).

cl90
4th March 2015, 11:40
Thanks for the quick replys.

I thought it must be something else, because qwt would be some kind of useless.

here is the whole code of a new app. which should work (but doesn't)


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qwtplot w;
w.show();

return a.exec();
}



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

QwtPlot* plot = new QwtPlot();

plot->setTitle( "Plot Demo" );
plot->setCanvasBackground( Qt::white );
plot->setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );
plot->insertLegend( new QwtLegend() );

ui->hlayout->addWidget(plot); // the app crashes here.

[...]

plot->show();



ui->setupUi(this);
}


Could this mean that my qwt is not compiled right? or do i something wrong with linking?
I use Qt 5.2.1 with MSVC addin 5.2.1 with qwt 6.1.2.
I can run plots and they work as expected.

Santosh Reddy
4th March 2015, 11:53
Call ui->setupUI(this) before using anything in ui, better call it as first statement in constructor.

cl90
4th March 2015, 12:10
...
that was it -.-

Thanks. hard to believe that i didn't tryed that.