问题描述
我在使用创建的自定义窗口小部件时遇到问题.我已经成功创建了一个生成.dll文件的自定义窗口小部件.我将此.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):错误C2491:'ScribbleArea :: staticMetaObject':不允许定义dllimport静态数据成员
debug\moc_scribblearea.cpp(44) : error C2491: 'ScribbleArea::staticMetaObject' : definition of dllimport static data member not allowed
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(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):警告C4273:'ScribbleArea :: qt_metacast':DLL链接不一致d:\ project \ qt \ workspace \ sample-build-desktop \ debug ../../sample/scribblearea.h(53):请参阅"qt_metacast"的先前定义
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):警告C4273:'ScribbleArea :: qt_metacall':DLL链接不一致d:\ project \ qt \ workspace \ sample-build-desktop \ debug ../../oep/scribblearea.h(53):请参阅"qt_metacall"的先前定义
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'
如何正确使用创建的自定义窗口小部件?从复制.dll到bin/designer文件夹到构建项目,是否有任何文档/参考描述了如何使用自定义窗口小部件?
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?
推荐答案
我从未创建过存储在.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:
如果您的自定义窗口小部件相对简单(它不具有显式需要Form-Editor的复杂属性),则可以在容器表单中创建另一种占位符(例如,窗口小部件的超类).设置基本属性,例如位置,几何形状,尺寸和size-policy,然后使用容器形式的构造函数中的一些简单代码将此占位符替换为您的自定义小部件.
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.
假设您具有以下条件:
a.从QGraphicsView继承的自定义小部件GuiInpImageView:GuiInpImageView :: GuiInpImageView(QWidget * parent):QGraphicsView(parent)
a. A custom widget GuiInpImageView inheriting from QGraphicsView:GuiInpImageView::GuiInpImageView(QWidget *parent):QGraphicsView(parent)
b.您实际上要在其中放置GuiInpImageView实例的mainwindowbase.ui(MainWindow类)
b. A mainwindowbase.ui (class MainWindow) at which you actually want to put an instance of GuiInpImageView
这是逐步解决方案:
-
放入.cpp源&GuiInpImageView的头文件直接在您的项目中(而不是在DLL中)
Put the .cpp source & header file for GuiInpImageView directly in your project (not in a DLL)
在MainWindow类头中定义以下私有成员:GuiInpImageView * inpImageView;
Define the following private member in MainWindow class header:GuiInpImageView *inpImageView;
在Form-Editor中打开mainwindowvase.ui,然后将QGraphicsView小部件放置在您实际想要放置自定义GuiInpImageView小部件的位置.假设您已经创建了一个名为inpImageViewVertLayout的垂直布局,其中包含一个名为inpImageViewPH(PH = PlaceHolder)的QGraphicsView.如果需要,还可以设置几何形状Min&使用QtCreator的表单编辑器中公开的属性来设置最大大小"和inpImageViewPH的大小策略.
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.
将以下代码放入您的MainWindow构造函数:
Put the following code into your MainWindow constructor:
//主窗口构造器
MainWindow :: MainWindow(QWidget * parent):QMainWindow(parent)
{
// Main Window Constructor
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupUi(this); // Setup UI
// Replace inpImageViewPH with custom Widget
// (inpImageViewPH is just a place holder to visualize UI in design mode)
inpImageView = new GuiInpImageView(centralwidget);
inpImageView->setGeometry(inpImageViewPH->geometry());
inpImageView->setMinimumSize(inpImageViewPH->minimumSize());
inpImageView->setMaximumSize(inpImageViewPH->maximumSize());
inpImageView->setSizePolicy(inpImageViewPH->sizePolicy());
inpImageViewVertLayout->addWidget(inpImageView);
// Remove and hide placeholder, keep only the Custom View
inpImageViewVertLayout->removeWidget(inpImageViewPH);
inpImageViewPH->hide();
...
这是我发现的最简单的方法,即使用代码在表单中插入自定义窗口小部件(不创建.DLL),但同时具有图形预览和一些关于位置&的控件.表单容器中小部件的大小.
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...
这篇关于在设计器中使用创建的自定义窗口小部件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!