Qwt and QMainWindow singleton instance sharing method across classes
Hi,
When I started using Qwt I used it in a single class (my only class), but getting bigger and bigger I decided to split it.
So now I have a class handling the mainWindow and another one using only Qwt for plotting curves.
In order to have access to my UI, I used a singleton technique (with getInstance() and static member) with methods in my mainwindow class (like getters or setters to use the ui pointer).
But with that, I have an error when it compile : "QWidget: Must construct a QApplication before a QPaintDevice". With Google I learned it was caused by my static instance of mainwindow.
How can I reconcile a nice way to access my Ui in other classes and Qwt ?
Thanks in advance
Have a nice day :)
Re: Qwt and QMainWindow singleton instance sharing method across classes
Show us your main(), and where do you create QApplication, QMainWindow and other Qwt objects?
Re: Qwt and QMainWindow singleton instance sharing method across classes
Main.cpp
Code:
#include <QtGui/QApplication>
#include "viewer.h"
int main(int argc, char *argv[])
{
Viewer w;
w.show();
return a.exec();
}
Viewer.cpp == MainWindow
Code:
#include "viewer.h"
#include "ui_viewer.h"
Viewer* Viewer::instance = 0;
Viewer* Viewer::getInstance(){
if(instance==0){
instance = new Viewer;
}
return instance;
}
#include <QtGui>
#include <QDebug>
ui(new Ui::Viewer)
{
ui->setupUi(this);
//connects(...);
}
Viewer::~Viewer()
{
delete ui;
}
if(plotType=="dark") ui->darkPlot->addWidget(plot);
else ui->lightPlot->addWidget(plot);
}
Various functions without
QwtPlot () {...
}
viewer.h
Code:
{
Q_OBJECT
public:
explicit Viewer
(QWidget *parent
= 0);
static Viewer* getInstance();
~Viewer();
void VariousFunctionsWithoutQwt(...);
private slots:
void VariousFunctionsWithoutQwt(...);
private:
Ui::Viewer *ui;
static Viewer* instance;
};
and finally my plot.cpp and plot.h where there is Qwt used.
plot.cpp:
Code:
#include "plot.h"
#include "viewer.h"
Viewer* viewer = Viewer::getInstance();
{
}
void Plot::initPlot(){
if(viewer->isPlotEmpty("dark")){
...
viewer->setPlot(plotD,"dark");
}
if(viewer->isPlotEmpty("light")){
...
viewer->setPlot(plotL,"light");
}
}
void Plot::plot(){
...
plotD->replot();
plotL->replot();
}
plot.h
Code:
#ifndef PLOT_H
#define PLOT_H
#include <QWidget>
#include "qwt_plot.h"
#include "qwt_plot_curve.h"
#include "qwt_symbol.h"
#include "qwt_plot_marker.h"
#include "qwt_legend.h"
#include "qwt_scale_engine.h"
{
Q_OBJECT
public:
private:
void initPlot();
private slots:
void plot();
};
#endif // PLOT_H
Thanks
Re: Qwt and QMainWindow singleton instance sharing method across classes
Quote:
Originally Posted by
picobuntu
"QWidget: Must construct a QApplication before a QPaintDevice".
AFAIR this is the result of a build issue even if it happens at runtime - but as I'm not using Windows myself I'm not 100% sure.
Do you have a properly installed version ( which one ? ) of Qwt and are you using qwt.prf included from a qmake project file ?
Uwe