PDA

View Full Version : using a custom widget



GrahamLabdon
21st March 2011, 15:50
Hi
So I have a custom widget and want to use it in another project.
In the .pro file for the new project I have


LIBS += C://QTProjects/cccc-build-desktop/debug/mycustomwidgetplugin.dll

When I compile the project I get


debug/MainWindow.o:C:\QTProjects\cccc-build-desktop/./ui_MainWindow.h:43: undefined reference to `MyCustomWidget::MyCustomWidget(QWidget*)'


Any ideas?

Graham

high_flyer
21st March 2011, 16:08
did you specify in your project where to find the headers of your custom widgets?

GrahamLabdon
21st March 2011, 16:09
Hi
I have hard coded these for now
#include "C:/QTProjects/MyCustomWidget/MyCustomWidget.h"

high_flyer
21st March 2011, 16:13
where?
The error comes from an ui file - and it gets generated by the uic... if you are using your custom widget there - you should promote a widget to your custom widget so that the uic will include the correct file.

GrahamLabdon
21st March 2011, 16:22
hi
I have now added the include path the .pro file
My custom widget is built in a dll and is accessible in QtDesigner (although not in QtCreator)
To get my application that uses the custom widget to compile I added an include path to the . pro file
I also added the dll to the LIBS -


#-------------------------------------------------
#
# Project created by QtCreator 2011-03-21T11:54:03
#
#-------------------------------------------------

QT += core gui

TARGET = cccc
TEMPLATE = app


SOURCES += main.cpp\
MainWindow.cpp

HEADERS += MainWindow.h

FORMS += MainWindow.ui

INCLUDEPATH = C:/QTProjects/MyCustomWidget
win32::LIBS += C:/QTProjects/MyCustomWidget-build-desktop/release/mycustomwidgetplugin.dll


When I build I get a undefined reference -
/ui_MainWindow.h:43: undefined reference to `MyCustomWidget::MyCustomWidget(QWidget*)'

I am struggling to understand why this is

Any help appreciated

Graham

high_flyer
21st March 2011, 16:24
I am struggling to understand why this is
read my last post again.

GrahamLabdon
21st March 2011, 16:30
Hi
In Qt Designer I can see my custom widget and can drag it onto a form.
This generates a ui file with an #include for the custom widget, which is located because of the INCLUDEPATH directive in the .pro file.
If I drag a widget onto the form and try to promote it I get an error stating that the class already exists
So I am confused!!

high_flyer
21st March 2011, 16:36
Hi
In Qt Designer I can see my custom widget and can drag it onto a form.
This generates a ui file with an #include for the custom widget, which is located because of the INCLUDEPATH directive in the .pro file.
That piece of information you want to give at the beginning, and that is - that you have a designer plug in for your custom widget!!
So there is no need for promotion.

does your custom widget have a constructor that take a QWidget parent defined?
Post the header for your custom widget.

GrahamLabdon
21st March 2011, 16:38
Header File


#ifndef MYCUSTOMWIDGET_H
#define MYCUSTOMWIDGET_H

#include <QtGui/QWidget>

class MyCustomWidget : public QWidget
{
Q_OBJECT

public:
MyCustomWidget(QWidget *parent = 0);
};

#endif


cpp


#include "MyCustomWidget.h"

MyCustomWidget::MyCustomWidget(QWidget *parent) :
QWidget(parent)
{
}



Thanks for your help!

high_flyer
21st March 2011, 16:45
ok, show the include section of ui_MainWindow.h

GrahamLabdon
21st March 2011, 16:48
Here ya go



#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
#include "MyCustomWidget.h"

high_flyer
21st March 2011, 16:55
and the custom widget header is under C:/QTProjects/MyCustomWidget?
Then I don't see where the problem is...

GrahamLabdon
21st March 2011, 16:57
:)
I found this link
http://stackoverflow.com/questions/2487846/qt-plugins-not-working/2491825#2491825
But don't really know how to create the other lib

high_flyer
21st March 2011, 17:03
you are getting a compile error, not a link error.
You have a definitive path problem that's all, I just can't tell where.
What is said in that post you linked to is not correct.
I link my apps to my custom widgets against their designer plugin dll with no problem.

GrahamLabdon
21st March 2011, 17:17
Hi
Im not certain that its a compiler error
Here is the output from the build


Running build steps for project cccc...
Starting: "C:/mingw/bin/mingw32-make.exe" clean -w
mingw32-make: Entering directory `C:/QTProjects/cccc-build-desktop'

C:/mingw/bin/mingw32-make -f Makefile.Release clean

mingw32-make[1]: Entering directory `C:/QTProjects/cccc-build-desktop'

del release\moc_MainWindow.cpp

del ui_MainWindow.h

del release\main.o release\MainWindow.o release\moc_MainWindow.o

mingw32-make[1]: Leaving directory `C:/QTProjects/cccc-build-desktop'

C:/mingw/bin/mingw32-make -f Makefile.Debug clean

mingw32-make[1]: Entering directory `C:/QTProjects/cccc-build-desktop'

del debug\moc_MainWindow.cpp

del ui_MainWindow.h

del debug\main.o debug\MainWindow.o debug\moc_MainWindow.o

mingw32-make[1]: Leaving directory `C:/QTProjects/cccc-build-desktop'

mingw32-make: Leaving directory `C:/QTProjects/cccc-build-desktop'

Could Not Find C:\QTProjects\cccc-build-desktop\debug\moc_MainWindow.cpp

Could Not Find C:\QTProjects\cccc-build-desktop\ui_MainWindow.h

Could Not Find C:\QTProjects\cccc-build-desktop\debug\main.o

The process "C:/mingw/bin/mingw32-make.exe" exited normally.
Configuration unchanged, skipping qmake step.
Starting: "C:/mingw/bin/mingw32-make.exe" -w
mingw32-make: Entering directory `C:/QTProjects/cccc-build-desktop'

C:/mingw/bin/mingw32-make -f Makefile.Release

mingw32-make[1]: Entering directory `C:/QTProjects/cccc-build-desktop'

c:\Qt\4.7.2\bin\uic.exe ..\cccc\MainWindow.ui -o ui_MainWindow.h

g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\Qt\4.7.2\include\QtCore" -I"..\..\Qt\4.7.2\include\QtGui" -I"..\..\Qt\4.7.2\include" -I"..\MyCustomWidget" -I"..\..\Qt\4.7.2\include\ActiveQt" -I"release" -I"." -I"..\cccc" -I"." -I"..\..\Qt\4.7.2\mkspecs\win32-g++" -o release\main.o ..\cccc\main.cpp

g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\Qt\4.7.2\include\QtCore" -I"..\..\Qt\4.7.2\include\QtGui" -I"..\..\Qt\4.7.2\include" -I"..\MyCustomWidget" -I"..\..\Qt\4.7.2\include\ActiveQt" -I"release" -I"." -I"..\cccc" -I"." -I"..\..\Qt\4.7.2\mkspecs\win32-g++" -o release\MainWindow.o ..\cccc\MainWindow.cpp

C:\Qt\4.7.2\bin\moc.exe -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\Qt\4.7.2\include\QtCore" -I"..\..\Qt\4.7.2\include\QtGui" -I"..\..\Qt\4.7.2\include" -I"..\MyCustomWidget" -I"..\..\Qt\4.7.2\include\ActiveQt" -I"release" -I"." -I"..\cccc" -I"." -I"..\..\Qt\4.7.2\mkspecs\win32-g++" -D__GNUC__ -DWIN32 ..\cccc\MainWindow.h -o release\moc_MainWindow.cpp

g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\Qt\4.7.2\include\QtCore" -I"..\..\Qt\4.7.2\include\QtGui" -I"..\..\Qt\4.7.2\include" -I"..\MyCustomWidget" -I"..\..\Qt\4.7.2\include\ActiveQt" -I"release" -I"." -I"..\cccc" -I"." -I"..\..\Qt\4.7.2\mkspecs\win32-g++" -o release\moc_MainWindow.o release\moc_MainWindow.cpp

g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads -Wl -Wl,-subsystem,windows -o release\cccc.exe release/main.o release/MainWindow.o release/moc_MainWindow.o -L"c:\Qt\4.7.2\lib" -lmingw32 -lqtmain C:/QTProjects/MyCustomWidget-build-desktop/release/mycustomwidgetplugin.dll -lQtGui4 -lQtCore4

mingw32-make[1]: Leaving directory `C:/QTProjects/cccc-build-desktop'

mingw32-make: Leaving directory `C:/QTProjects/cccc-build-desktop'

release/MainWindow.o:MainWindow.cpp:(.text$_ZN13Ui_MainWin dow7setupUiEP11QMainWindow[Ui_MainWindow::setupUi(QMainWindow*)]+0x129): undefined reference to `MyCustomWidget::MyCustomWidget(QWidget*)'

collect2: ld returned 1 exit status

mingw32-make[1]: *** [release\cccc.exe] Error 1

mingw32-make: *** [release] Error 2

The process "C:/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project cccc (target: Desktop)
When executing build step 'Make'


Does this help you locate the problem?

high_flyer
21st March 2011, 17:24
The error you posted now is a link error.

elease/MainWindow.o:MainWindow.cpp:(.text$_ZN13Ui_MainWin dow7setupUiEP11QMainWindow[Ui_MainWindow::setupUi(QMainWindow*)]+0x129): undefined reference to `MyCustomWidget::MyCustomWidget(QWidget*)'
But error you posted at the beginngin of this thread was a compile error:

debug/MainWindow.o:C:\QTProjects\cccc-build-desktop/./ui_MainWindow.h:43: undefined reference to `MyCustomWidget::MyCustomWidget(QWidget*)'

You have to be a bit more cooperative if you want effective help.

Your link line is not good:

-L"c:\Qt\4.7.2\lib" -lmingw32 -lqtmain C:/QTProjects/MyCustomWidget-build-desktop/release/mycustomwidgetplugin.dll -lQtGui4 -lQtCore4
'-l' is missing before 'C:/QTProjects/MyCustomWidget-build-desktop/release/mycustomwidgetplugin.dll'

GrahamLabdon
21st March 2011, 17:27
Hi
Sorry if there has been some confusion
I had already tried '-l'
It gives this error

c:/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot find -lC:/QTProjects/MyCustomWidget-build-desktop/release/mycustomwidgetplugin.dll

high_flyer
21st March 2011, 17:34
you need to link against the *.lib file not the dll.
The dll is loaded at runtime.

GrahamLabdon
21st March 2011, 17:36
I don't have a ".lib" file to link against

high_flyer
21st March 2011, 17:48
Well, you should.
If you have the custom widget project source, try rebuilding it.
Without a lib you can't link against this DLL.

GrahamLabdon
21st March 2011, 17:56
If I build the Qt customwidgetplugin project it produces a dll in the plugins/designer folder and a libcustomwidgetplugin.a .
These are the same outputs as my own custom widget project.

I noticed that in Qt' custom widget there is the 'QDESIGNER_WIDGET_EXPORT' macro in the class definition.
I added this to my custom widget and got the application to link.
HOWEVER - it will not run - it exits on startup with an error code of 128

Do you know of any working examples of Qt Designer addins?

Graham

high_flyer
21st March 2011, 20:17
Ah, you are using MinGW - then we are talking about the libcustomwidgetplugin.a.

GrahamLabdon
22nd March 2011, 09:15
Hi
I have a build!
It seems to make no difference whether the library is the '.a' or the '.dll' both work- I have a couple of other questions -

I need to have the '.dll' in the same folder as application to make it run - is there anyway to overcome this
I do not see my custom widget in Qt Creator but if I run Qt Designer separately it is visible - how can I make it visible
( I have set QT_PLUGIN_PATH to C:\Qt\4.7.2\plugins)

Thanks again for your time

Graham

high_flyer
22nd March 2011, 09:56
It seems to make no difference whether the library is the '.a' or the '.dll' both work-
Now that I had time to think it a bit, the *.a is not it either (although it should work as well) its just a static lib.
It was ages since I worked wit MinGW, I will assume that what you are saying here is correct, probably as it is working similar as under linux, where you just link against the *.so directly.
Sorry for misleading you there.

I need to have the '.dll' in the same folder as application to make it run - is there anyway to overcome this
Yes, your PATH inviornment variable needs to point to the location of your DLL.
One option is to install the DLL in to a known path for DLL such as system32.


I do not see my custom widget in Qt Creator but if I run Qt Designer separately it is visible - how can I make it visible
( I have set QT_PLUGIN_PATH to C:\Qt\4.7.2\plugins)

I am not using creator so I can't say.
But it is surprising for me, since I'd think creator is using the same paths as designer.
Maybe other users who use creator can help you more on this.

GrahamLabdon
22nd March 2011, 09:58
Hi
I cannot see the plugin in creator as my creator was not built with the mingw compiler

Thanks for all your help

high_flyer
22nd March 2011, 10:01
regarding the DLL linking issues, read the last part of this link:
http://www.cygwin.com/cygwin-ug-net/dll.html