在设计器中使用创建的自定义小部件时出错

发布于 2024-10-31 17:09:34 字数 1189 浏览 0 评论 0原文

我在使用创建的自定义小部件时遇到问题。我已经成功创建了一个生成 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2024-11-07 17:09:34

我从未创建过存储在 .dll 中的自定义小部件,因此我无法帮助您解决您的主要问题,但我可以提供一个解决方法:

如果您的自定义小部件相对简单(它没有明确要求的复杂属性)表单编辑器)您可以在容器表单中创建另一种占位符(例如小部件的超类),以便能够设置基本属性,例如位置,几何形状,大小和位置。 size-policy ,然后使用容器形式构造函数中的一些简单代码将此占位符替换为您的自定义小部件。

假设您有以下条件

:继承自 QGraphicsView 的自定义小部件 GuiInpImageView:
GuiInpImageView::GuiInpImageView(QWidget *parent):QGraphicsView(parent)

b.您实际上想要在其中放置 GuiInpImageView 实例的 mainwindowbase.ui(MainWindow 类)

这是逐步解决的解决方案:

  1. Put the .cpp source &直接在项目中(而不是在 DLL 中)

  2. 在 MainWindow 类头中定义以下私有成员:
    GuiInpImageView *inpImageView;

  3. 在表单编辑器中打开 mainwindowvase.ui 并将 QGraphicsView 小部件放在您实际想要放置自定义 GuiInpImageView 小部件的位置。
    假设您创建了一个名为 inpImageViewVertLayout 的垂直布局,其中包含一个名为 inpImageViewPH (PH = PlaceHolder) 的 QGraphicsView。
    如果您愿意,您还可以设置几何形状、最小值和最小值。 Max Size 和 inpImageViewPH 的大小策略使用 QtCreator 的表单编辑器中公开的属性。

  4. 将以下代码放入您的 MainWindow 构造函数中:


// 主窗口构造函数
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)的最简单方法,但还可以进行图形预览和对位置和表单容器中小部件的大小。

希望这有帮助...

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:

  1. Put the .cpp source & header file for GuiInpImageView directly in your project (not in a DLL)

  2. Define the following private member in MainWindow class header:
    GuiInpImageView *inpImageView;

  3. 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.

  4. Put the following code into your MainWindow constructor:


// 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();

...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文