Hello, I'm trying to learn how to use Qwt and I'm stucked with one problem.
When I create a custom application with just main function everything works fine. (the code is based on examples from qwt)
int main(int argc, char *argv[])
{
double x[100];
double y[100];
for(int i=0;i<100;i++)
{
x[i] = i/20.0;
y[i] = sin(x[i]);
}
layout->setContentsMargins( 0, 0, 0, 0 );
myPlot->setMinimumSize(w.geometry().width()-20,w.geometry().height()-20);
// add curve
curve->setRawSamples(x, y, 100);
curve
->setPen
(QColor(Qt
::darkGreen));
curve->attach(myPlot);
layout->addWidget(myPlot);
layout->addWidget(b);
w.setLayout(layout);
w.show();
return a.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
double x[100];
double y[100];
for(int i=0;i<100;i++)
{
x[i] = i/20.0;
y[i] = sin(x[i]);
}
QHBoxLayout *layout = new QHBoxLayout(&w);
layout->setContentsMargins( 0, 0, 0, 0 );
QwtPlot *myPlot = new QwtPlot(&w);
myPlot->setMinimumSize(w.geometry().width()-20,w.geometry().height()-20);
// add curve
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setRawSamples(x, y, 100);
curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
QPen(Qt::black), QSize(5, 5) ) );
curve->setPen(QColor(Qt::darkGreen));
curve->setStyle(QwtPlotCurve::Lines);
curve->setCurveAttribute(QwtPlotCurve::Fitted);
curve->attach(myPlot);
QPushButton *b = new QPushButton(&w);
layout->addWidget(myPlot);
layout->addWidget(b);
w.setLayout(layout);
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
The problem is when i try to copy this part of code and put it in MainWindow designed in QtCreator.
void MainWindow::initGraph()
{
double x[100];
double y[100];
for(int i=0;i<100;i++)
{
x[i] = i/100.0;
y[i] = sin(x[i]);
}
layout->setContentsMargins( 0, 0, 0, 0 );
myPlot->setMinimumSize(ui->widget->geometry().width()-20,ui->widget->geometry().height()-20);
myPlot->updateGeometry();
// add curves
curve->setRawSamples(x, y, 100);
curve
->setPen
(QColor(Qt
::darkGreen));
curve->attach(myPlot);
myPlot->autoReplot();
myPlot->replot();
myPlot->autoRefresh();
layout->addWidget(myPlot);
ui->widget->setLayout(layout);
}
//and main
int main(int argc, char *argv[])
{
MainWindow w;
w.initGraph();
w.show();
return a.exec();
}
void MainWindow::initGraph()
{
double x[100];
double y[100];
for(int i=0;i<100;i++)
{
x[i] = i/100.0;
y[i] = sin(x[i]);
}
QHBoxLayout *layout = new QHBoxLayout(ui->widget);
layout->setContentsMargins( 0, 0, 0, 0 );
QwtPlot *myPlot = new QwtPlot(ui->widget);
myPlot->setMinimumSize(ui->widget->geometry().width()-20,ui->widget->geometry().height()-20);
myPlot->updateGeometry();
// add curves
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setRawSamples(x, y, 100);
curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
QPen(Qt::black), QSize(5, 5) ) );
curve->setPen(QColor(Qt::darkGreen));
curve->setStyle(QwtPlotCurve::Lines);
curve->setCurveAttribute(QwtPlotCurve::Fitted);
curve->attach(myPlot);
myPlot->autoReplot();
myPlot->replot();
myPlot->autoRefresh();
layout->addWidget(myPlot);
ui->widget->setLayout(layout);
}
//and main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.initGraph();
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
After running code above I see my app, axis of the plot are scaled but curve is not painted properly (I think only first point is painted). I tried couple of functions from plot to fix it (as you can see in code above). I've created 2 or 3 gui apps using qtCreator's designer and even with just 1 main widget it doesn't work fine. Could anyone tell me where do I make a mistake and how to fix that problem?
Sorry if someone finds it hard to understand what I've written here but english is not my native language.
Bookmarks