简体   繁体   中英

Qt with qwt runtime error: QWidget: Must construct a QApplication before a QWidget

I am trying to make a app using qt 5.4.1(and qwt 6.1.2).Here is my environment:

  • Windows 7 x64
  • visual studio 2012
  • Qt5.4.1 static
  • qwt6.1.2

and I have built qwt with my Qt static libs successfully. I creat a widget class inherited from QwtPlot,and I creat a mainWindow which has a object of that widget. Then I build the project. However,there is a runtime error: QWidget: Must construct a QApplication before a QWidget .


This is my widget class inherited from QwtPlot:

 #pragma once #include <QWT/qwt_plot.h> #include <QWT/qwt_plot_curve.h> class DrawWidget: public QwtPlot { public: DrawWidget(QWidget *parent ); ~DrawWidget(void); }; 

 DrawWidget::DrawWidget(QWidget *parent ) : QwtPlot( parent ), carve(NULL) { } 

And the follow is my mainWindow class:

 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include "DrawWidget.h" #include <QtWidgets/QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: DrawWidget *drawWidget; }; #endif // MAINWINDOW_H 

  MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent) { QWidget *widget = new QWidget(this); this->setCentralWidget(widget); QHBoxLayout *mainLayout = new QHBoxLayout(widget); drawWidget = new DrawWidget(widget); mainLayout->addWidget(drawWidget); centralWidget()->setLayout(mainLayout); } 

And this is my main.cpp:

 #include "mainwindow.h" #include <QtWidgets/QApplication> #include <QtPlugin> Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin); int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.resize(1000,600); w.show(); return a.exec(); } 

I build this project in release version.Any idea? Thank you!

I had the same problem few weeks ago in my case the problem was the additional library. Make sure that you got builds of the additional library debug for debug building and release for release building. Add AdditionalLibraryDebug/bin folder to the Path environment variable when you build Debug Version and AdditionalLibraryRelease/bin when you build Release version (not both in same time)

Good Luck :) i solved my problem on this way. :)

I found this very useful

http://www.itk.org/Wiki/VTK/FAQ#Shared_builds_of_VTK_and_debugging_QVTKWidget_using_Visual_Studio

It happens that libraries depend on the debug version of VTK so you have to build both release and debug and be sure to add the path in the debugging properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM