Results 1 to 4 of 4

Thread: Qwt and QMainWindow singleton instance sharing method across classes

  1. #1
    Join Date
    Jul 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question 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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default 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?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: Qwt and QMainWindow singleton instance sharing method across classes

    Main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "viewer.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Viewer w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Viewer.cpp == MainWindow
    Qt Code:
    1. #include "viewer.h"
    2. #include "ui_viewer.h"
    3.  
    4. Viewer* Viewer::instance = 0;
    5.  
    6. Viewer* Viewer::getInstance(){
    7. if(instance==0){
    8. instance = new Viewer;
    9. }
    10. return instance;
    11. }
    12.  
    13. #include <QtGui>
    14. #include <QDebug>
    15.  
    16. Viewer::Viewer(QWidget *parent) :
    17. QMainWindow(parent),
    18. ui(new Ui::Viewer)
    19. {
    20. ui->setupUi(this);
    21. //connects(...);
    22. }
    23.  
    24. Viewer::~Viewer()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void Viewer::setPlot(QwtPlot *plot, QString plotType){
    30. if(plotType=="dark") ui->darkPlot->addWidget(plot);
    31. else ui->lightPlot->addWidget(plot);
    32. }
    33.  
    34. Various functions without QwtPlot () {...}
    To copy to clipboard, switch view to plain text mode 

    viewer.h
    Qt Code:
    1. class Viewer : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Viewer(QWidget *parent = 0);
    7. static Viewer* getInstance();
    8. ~Viewer();
    9. void VariousFunctionsWithoutQwt(...);
    10.  
    11. private slots:
    12. void VariousFunctionsWithoutQwt(...);
    13.  
    14. private:
    15. Ui::Viewer *ui;
    16. static Viewer* instance;
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    and finally my plot.cpp and plot.h where there is Qwt used.
    plot.cpp:
    Qt Code:
    1. #include "plot.h"
    2. #include "viewer.h"
    3.  
    4. QwtPlot *plotD;
    5. QwtPlot *plotL;
    6.  
    7. Viewer* viewer = Viewer::getInstance();
    8.  
    9. Plot::Plot(QWidget *parent) : QWidget(parent)
    10. {
    11. }
    12.  
    13. void Plot::initPlot(){
    14. if(viewer->isPlotEmpty("dark")){
    15. plotD = new QwtPlot();
    16. ...
    17. viewer->setPlot(plotD,"dark");
    18. }
    19. if(viewer->isPlotEmpty("light")){
    20. plotL = new QwtPlot();
    21. ...
    22. viewer->setPlot(plotL,"light");
    23. }
    24.  
    25. }
    26.  
    27. void Plot::plot(){
    28. ...
    29. plotD->replot();
    30. plotL->replot();
    31. }
    To copy to clipboard, switch view to plain text mode 

    plot.h
    Qt Code:
    1. #ifndef PLOT_H
    2. #define PLOT_H
    3.  
    4. #include <QWidget>
    5.  
    6. #include "qwt_plot.h"
    7. #include "qwt_plot_curve.h"
    8. #include "qwt_symbol.h"
    9. #include "qwt_plot_marker.h"
    10. #include "qwt_legend.h"
    11. #include "qwt_scale_engine.h"
    12.  
    13. class Plot : public QWidget
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit Plot(QWidget *parent);
    18.  
    19. private:
    20. void initPlot();
    21.  
    22. private slots:
    23. void plot();
    24.  
    25. };
    26.  
    27. #endif // PLOT_H
    To copy to clipboard, switch view to plain text mode 

    Thanks

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt and QMainWindow singleton instance sharing method across classes

    Quote Originally Posted by picobuntu View Post
    "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

Similar Threads

  1. Replies: 1
    Last Post: 9th April 2011, 21:07
  2. Classes with Implicit Sharing
    By weaver4 in forum Newbie
    Replies: 5
    Last Post: 15th January 2011, 18:57
  3. How to correctly subclass classes that use implicit sharing
    By Gh0str1d3r in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2010, 09:42
  4. Object instance in a singleton class dissapears
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 9th August 2009, 23:51
  5. Replies: 2
    Last Post: 8th October 2006, 16:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.