将从共享库获取的QWidget添加到QTabWidget时,应用程序崩溃

时间:2017-06-11 10:43:32

标签: qt shared-libraries qtabwidget

csrss.exe

当我向//the subclass of widget in shared library class MYWIDGETSHARED_EXPORT MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = 0); void do_something(); }; extern "C"{ MYWIDGETSHARED_EXPORT MyWidget *getMyWidget(){ return new MyWidget;} } //the application which will use the shared library void MainWindow::creatCentralWidget() { QTabWidget *tabWidget = new QTabWidget(this); QLibrary myLib("xxx/MyWidget.dll"); if(myLib.load()){ MyWidget *widget = (MyWidget*)myLib.resolve("getMyWidget"); tabWidget->addTab(widget,"MyWidget");//Here cause crash! } //.......do_something()...... } 添加MyWidget时,应用程序崩溃了代码tabWidget

我设置了255LIBSINCLUDEPATH,它们似乎没问题。

所以我想知道,我如何正确地将共享库中的小部件嵌入DEPENDPATH?谢谢!

1 个答案:

答案 0 :(得分:0)

由于您使用了以下演员资格,您有未定义的行为......

MyWidget *widget = (MyWidget*)myLib.resolve("getMyWidget");

getMyWidget的正确类型是......

MyWidget *(*)()

您需要解析符号,然后调用它...

typedef MyWidget *(*get_my_widget_type)();
if (get_my_widget_type get_my_widget = get_my_widget_type(myLib.resolve("getMyWidget"))) {
  MyWidget *widget = get_my_widget();
  tabWidget->addTab(widget, "MyWidget");
}