3

I have already posted this in the OSG mailing list, but the mailing list seems to be a bit slow.

Anyway, I'm trying to modify the osgViewerQt example by adding a new class of my own that will contain the viewer. The design is:

  • wrapper.h: Defines class Wrapper. It inherits from QMainWindow and has a QDockWidget where the ViewerWidget will be attached.
  • viewer.h: Defines ViewerWidget class. It's the class from the example, with a few mods by me.
  • prueba_qt.cpp: Main function and where a QApplication is created. A Wrapper object is created here.

The project compiles, but when I execute it, I get an error:

QWidget: Must construct a QApplication before a QPaintDevice*

If I remove the Q_OBJECT line, the signal and the slot from wrapper.h and compile the files from the terminal using

g++ -IE:/osg-3.0.1/install/include -LE:/osg-3.0.1/install/bin -IC:/Qt64/4.8/include -LC:/Qt64/4.8/bin -losgViewer -lOpenThreads -losgDB -losg -losgGA -losgQt -lQtCore4 -lQtGui4 prueba_qt.cpp

I can execute the app.

Can you please tell me what can I do to make this work? I've struggling all morning but couldn't find the solution.

Thanks for your time!

PS: SO is Windows 7 64 bits # MingW compiler # Qt 4.8 # OSG 3.0.1

PS2: Here're the files I used in this project, including the pro file from qmake:

wrapper.h

    #ifndef Wrapper_hpp
    #define Wrapper_hpp

    #include "viewer.h"
    #include <QtGui/QMainWindow>
    #include <QtGui/QDockWidget>

    class Wrapper: public QMainWindow {
     Q_OBJECT
    private:
      ViewerWidget* view;
      QDockWidget* dock;
    public:
      Wrapper(void) {
        view = new ViewerWidget();
        dock = new QDockWidget;
        dock->setWidget( view );
        dock->setGeometry( 100, 100, 800, 600 );
        dock->setAllowedAreas(Qt::RightDockWidgetArea);
        addDockWidget(Qt::RightDockWidgetArea, dock);
        dock->show();
      }
      void Do(void) { view->Do(); }
      void Add(void) { view->Add(); }
      virtual ~Wrapper(void) {}
      public slots:
        void MySlot(void) {}
      signals:
        void MySignal(void);
    };
    #endif

wrapper.cpp

(This exists only because I read in the Qt forum that moc can only parse cpp files and thus one is needed for the signal/slot mechanism.)

    #include "wrapper.h"

    Wrapper::Wrapper(void) {
        view = new ViewerWidget();
    //    view->setGeometry( 100, 100, 800, 600 );
        dock = new QDockWidget;
        dock->setWidget( view );
        dock->setGeometry( 100, 100, 800, 600 );
        dock->setAllowedAreas(Qt::RightDockWidgetArea);
        addDockWidget(Qt::RightDockWidgetArea, dock);
        dock->show();
    }

prueba_qt.cpp

#include <QtGui/QApplication>
#include <iostream>
#include "wrapper.h"

int main( int argc, char** argv ) {
    osg::ArgumentParser arguments(&argc, argv);
    QApplication app(argc, argv);
    Wrapper wrap;
    wrap.resize(800,600);
    wrap.setWindowTitle("Cow");
    wrap.showNormal();
    wrap.Do();    
    return app.exec();
}

prueba_qt.pro

    ######################################################################
    # Automatically generated by qmake (2.01a) mar 12. mar 13:45:28 2013
    ######################################################################
    QT += core gui
    TEMPLATE = app
    TARGET = 
    DEPENDPATH += .
    INCLUDEPATH += . E:/osg-3.0.1/install/include
    LIBS += -LE:/osg-3.0.1/install/bin -losg -lOpenThreads -losgDB -losgGA -losgQt -losgViewer

    # Input
    HEADERS += viewer.h wrapper.h
    SOURCES += prueba_qt.cpp wrapper.cpp

viewer.h: This is quite big, so I uploaded it to pastebin

EDIT #1

I have set OSG_NOTIFY_LEVEL to DEBUG_INFO and got this humongous output. The line with the error is:

FindFileInPath() : trying C:\cygwin\bin\osgPlugQWidget: Must construct a QApplication before a QPaintDevice

EDIT #2

The signal and slot were missing in the code. I have just added them to wrapper.h along with the call to Q_OBJECT.

4

1 回答 1

2

小憩片刻后,我再次修改了项目中使用的库。问题是其中一些库处于调试模式,而一些处于发布模式。使用信号和插槽时,moc发疯了。

在构建OSG调试库之后,我再次尝试了我的小例子并工作了。

所以它完成了!

于 2013-03-13T16:12:51.600 回答