在设计器中使用创建的自定义小部件时出错
我在使用创建的自定义小部件时遇到问题。我已经成功创建了一个生成 .dll 文件的自定义小部件。我将此 .dll 文件放在 bin/designer 下。这也得到了设计师的认可。我使用了它并将其放在我的用户界面上。但是当我尝试构建它时,发生了错误。
debug\moc_scribblearea.cpp(44):警告 C4273:“staticMetaObject”:DLL 链接不一致 d:\project\qt\workspace\sample-build-desktop\debug../../oep/scribblearea.h(53) :请参阅“public: static QMetaObject const ScribbleArea::staticMetaObject”的先前定义
debug\moc_scribblearea.cpp(44):错误 C2491:'ScribbleArea::staticMetaObject':不允许定义 dllimport 静态数据成员
debug\moc_scribblearea.cpp(54) : 警告 C4273: 'ScribbleArea::metaObject' : DLL 链接不一致 d:\project\qt\workspace\sample-build-desktop\debug../../sample/scribblearea.h(53) :请参阅“metaObject”的先前定义
debug\moc_scribblearea.cpp(59) : 警告 C4273: 'ScribbleArea::qt_metacast' : DLL 链接不一致 d:\project\qt\workspace\sample-build-desktop\debug../../sample/scribblearea.h(53) :请参阅“qt_metacast”的先前定义
debug\moc_scribblearea.cpp(67) : 警告 C4273: 'ScribbleArea::qt_metacall' : DLL 链接不一致 d:\project\qt\workspace\sample-build-desktop\debug../../oep/scribblearea.h(53) :请参阅“qt_metacall”的先前定义
如何正确使用我创建的自定义小部件?是否有任何文档/参考描述如何使用自定义小部件,从将 .dll 复制到 bin/designer 文件夹直至构建项目?
I am having a problem on using created custom widget. I have successfully created a custom widget that produces .dll file. I placed this .dll file under bin/designer. It was also recognized in the designer. I used it and placed it on my ui. But when I tried to build it, error occurred.
debug\moc_scribblearea.cpp(44) : warning C4273: 'staticMetaObject' : inconsistent dll linkage
d:\project\qt\workspace\sample-build-desktop\debug../../oep/scribblearea.h(53) : see previous definition of 'public: static QMetaObject const ScribbleArea::staticMetaObject'debug\moc_scribblearea.cpp(44) : error C2491: 'ScribbleArea::staticMetaObject' : definition of dllimport static data member not allowed
debug\moc_scribblearea.cpp(54) : warning C4273: 'ScribbleArea::metaObject' : inconsistent dll linkage
d:\project\qt\workspace\sample-build-desktop\debug../../sample/scribblearea.h(53) : see previous definition of 'metaObject'debug\moc_scribblearea.cpp(59) : warning C4273: 'ScribbleArea::qt_metacast' : inconsistent dll linkage
d:\project\qt\workspace\sample-build-desktop\debug../../sample/scribblearea.h(53) : see previous definition of 'qt_metacast'debug\moc_scribblearea.cpp(67) : warning C4273: 'ScribbleArea::qt_metacall' : inconsistent dll linkage
d:\project\qt\workspace\sample-build-desktop\debug../../oep/scribblearea.h(53) : see previous definition of 'qt_metacall'
How can I use my created custom widget correctly? Are there any document/reference that describes how to use custom widget, from copying .dll to bin/designer folder upto building a project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未创建过存储在 .dll 中的自定义小部件,因此我无法帮助您解决您的主要问题,但我可以提供一个解决方法:
如果您的自定义小部件相对简单(它没有明确要求的复杂属性)表单编辑器)您可以在容器表单中创建另一种占位符(例如小部件的超类),以便能够设置基本属性,例如位置,几何形状,大小和位置。 size-policy ,然后使用容器形式构造函数中的一些简单代码将此占位符替换为您的自定义小部件。
假设您有以下条件
:继承自 QGraphicsView 的自定义小部件 GuiInpImageView:
GuiInpImageView::GuiInpImageView(QWidget *parent):QGraphicsView(parent)
b.您实际上想要在其中放置 GuiInpImageView 实例的 mainwindowbase.ui(MainWindow 类)
这是逐步解决的解决方案:
Put the .cpp source &直接在项目中(而不是在 DLL 中)
在 MainWindow 类头中定义以下私有成员:
GuiInpImageView *inpImageView;
在表单编辑器中打开 mainwindowvase.ui 并将 QGraphicsView 小部件放在您实际想要放置自定义 GuiInpImageView 小部件的位置。
假设您创建了一个名为 inpImageViewVertLayout 的垂直布局,其中包含一个名为 inpImageViewPH (PH = PlaceHolder) 的 QGraphicsView。
如果您愿意,您还可以设置几何形状、最小值和最小值。 Max Size 和 inpImageViewPH 的大小策略使用 QtCreator 的表单编辑器中公开的属性。
将以下代码放入您的 MainWindow 构造函数中:
// 主窗口构造函数
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
这是我发现使用代码在表单中插入自定义小部件(无需创建 .DLL)的最简单方法,但还可以进行图形预览和对位置和表单容器中小部件的大小。
希望这有帮助...
I have never created a custom widget stored in a .dll, therefore I can't help your on your primary question, but I have a workaround to offer:
If your custom widget is relatively simple (it does not have complex properties that explicitly require the Form-Editor) you can create a placeholder of another kind (e.g. a superclass of your widget) in your container-form, to be able to set the basic properties like position, geometry, size & size-policy and then replace this placeholder with your custom widget using some simple code in the container-form constructor.
Let's say that you have the following:
a. A custom widget GuiInpImageView inheriting from QGraphicsView:
GuiInpImageView::GuiInpImageView(QWidget *parent):QGraphicsView(parent)
b. A mainwindowbase.ui (class MainWindow) at which you actually want to put an instance of GuiInpImageView
This is the workaround solution step-by-step:
Put the .cpp source & header file for GuiInpImageView directly in your project (not in a DLL)
Define the following private member in MainWindow class header:
GuiInpImageView *inpImageView;
Open the mainwindowvase.ui in Form-Editor and put a QGraphicsView widget at the position you actually wanted to put your custom GuiInpImageView widget.
Let's say you have created a vertical layout named inpImageViewVertLayout containing a QGraphicsView named inpImageViewPH (PH = PlaceHolder).
You can also set -if you wish- the geometry, Min & Max Size and the size policy of inpImageViewPH using the properties exposed in form-editor of QtCreator.
Put the following code into your MainWindow constructor:
// Main Window Constructor
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
This is the easiest way I have found to insert a custom widget in a form using code (without creating a .DLL) but to have also a graphical-preview and some control about the position & size of the widget in the form-container.
Hope this helps...