我有Qt插件,它应该在QMainWindow表单上添加一个QPushButton,或者只返回指向按钮的指针。
这是插件的功能:
QPushButton* ButPlugin::addButton(QMainWindow* w) const
{
QPushButton* but = new QPushButton(" Hello from ButPlugin.dll", w);
but->setGeometry(70, 70, 150, 150);
return but;
}
不幸的是,当我尝试show()
but
时(如果我从主程序或插件中调用show()
并不重要),该程序无法完成'工作正常。它面临未经处理的例外。
如何解决?感谢您的任何想法
这是插件的头文件:
#include "mainwindow.h"
#include "PluginInterface.h"
#include "ButPluginInterface.h"
#include <QPushButton>
class ButPlugin : public QObject, public ButPluginInterface, public PluginInterface
{
Q_OBJECT
Q_INTERFACES(ButPluginInterface PluginInterface)
public:
ButPlugin() {};
~ButPlugin() {};
QString Name() const;
QPushButton* addButton(QMainWindow*) const;
};
#endif // BUTPLUGIN_H
ButPlugin.cpp
#include "ButPlugin.h"
Q_EXPORT_PLUGIN2(ButPlugin, ButPlugin)
QString ButPlugin::Name() const
{
return "ButPlugin";
}
QPushButton* ButPlugin::addButton(QMainWindow* window) const
{
QPushButton* but = new QPushButton(" Hello from ButPlugin.dll! ", window);
but->setGeometry(70, 70, 150, 150);
//but->show();
return but;
}
插件的.pro文件:
TEMPLATE = lib
TARGET = ButtonPlugin
CONFIG += plugin debug
VERSION = 1.0
DEFINES += VERSION="$$VERSION"
INSTALLS += target
HEADERS += ./mainwindow.h \
./Descriptor.h \
./PluginInterface.h \
./ButPlugin.h \
./ButPluginInterface.h
SOURCES += ./main.cpp \
./mainwindow.cpp \
./ButPlugin.cpp
FORMS += ./mainwindow.ui
RESOURCES += mainwindow.qrc
主要应用程序代码:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow* w = new MainWindow();
w->show();
try
{
QDir _path(pluginsPath);
QStringList dllList = _path.entryList(QStringList("*.dll"));
foreach (QString fileName, dllList)
{
PossiblePlugin tempPlugin;
QPluginLoader* loader= new QPluginLoader(_path.absoluteFilePath(fileName));
QObject* possiblePlugin = loader->instance();
QObject* tryPlugin = i.value()->_loader->instance();
if (tryPlugin)
{
ButPluginInterface* tempButInt = tryInterface<ButPluginInterface>(tryPlugin);
if (tempButInt)
{
auto newPlugin = _pluginsList.find(i.key());
if (newPlugin == _pluginsList.end())
_pluginsList.insert(i.key(), i.value()->_loader);
QMainWindow* win = new MainWindow();
win->show();
QPushButton* bt = nullptr;
bt = tempButInt->addButton(win);
//bt->setGeometry(20,20,20,20);
// when this method was executed
// application continues working
// and shows warning only when it tries to
// do return a.exec();
bt->show();
}
}
}
}
catch(...)
{
showMsgBox(QString("Unknown error occured"), QString(""), QMessageBox::StandardButton::Cancel);
}
return a.exec();
}
对于主应用程序的.pro文件,此项目中没有.pro文件。该程序正在MS Visal Studio中开发,Qt库只是一个加载项。所以它在没有.pro文件的情况下运行。
MainWindow类是由Qt。
生成的MainWindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~MainWindow();
private:
Ui::MainWindowClass ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
}
MainWindow::~MainWindow()
{
}
错误: “PluginsManager_3.exe中0x5a1a1a37(QtCored4.dll)的未处理异常:0xC0000005:访问冲突读取位置0x5c8f996c。”
我想指针有一些麻烦。
我得到了所有表格的孩子QObjectList list = win->children();
,事实证明表格知道这个按钮......
只有在调用show()函数后才会抛出异常,如果你试图设置Geometry(70,70,150,150);从主应用程序,它将正常工作。