Reputation: 11
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?
Upvotes: 1
Views: 947
Reputation: 1500
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)
{
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...
Upvotes: 0