Using Custom Designer Widgets
-
I have completed all the tutorials that shows how to make a custom widget for Qt Designer, and the custom widget is showing up fine in Qt Designer.
An example is the AnalogClock tutorial. When I make a new project and drags in AnalogClock widget into my main window, this works fine.
I then make a pri file with this content:
INCLUDEPATH += $$PWD
HEADERS += $$PWD/analogclock.h
SOURCES += $$PWD/analogclock.cppand copies the analogclock.h and .cpp files intop my project folder.
When I tries to compile it, I get this:
mainwindow.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall AnalogClock::paintEvent(class QPaintEvent *)" (?paintEvent@AnalogClock@@MAEXPAVQPaintEvent@@@Z)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall AnalogClock::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@AnalogClock@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall AnalogClock::qt_metacast(char const *)" (?qt_metacast@AnalogClock@@UAEPAXPBD@Z)
debug\test7.exe : fatal error LNK1120: 6 unresolved externals
jom: C:\Users\MyrvoldI\Documents\Qt\build-test7-Desktop_Qt_5_3_MSVC2013_32bit-Debug\Makefile.Debug [debug\test7.exe] Error 1120
jom: C:\Users\MyrvoldI\Documents\Qt\build-test7-Desktop_Qt_5_3_MSVC2013_32bit-Debug\Makefile [debug] Error 2
12:12:32: The process "C:\Qt\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project test7 (kit: Desktop Qt 5.3 MSVC2013 32bit)
When executing step "Make"
12:12:32: Elapsed time: 00:02. -
I just followed the documentation here: http://doc.qt.io/qt-5/designer-creating-custom-widgets.html where it says to create a .pri file.
But I tried your suggestion to add the analogclock.h and .cpp to the headers and source in the .pro file, and now I get this compiler output:
14:22:19: Starting: "C:\Qt\5.3\msvc2013\bin\qmake.exe" C:\Users\MyrvoldI\Documents\Qt\test7\test7.pro -r -spec win32-msvc2013 "CONFIG+=debug" 14:22:19: The process "C:\Qt\5.3\msvc2013\bin\qmake.exe" exited normally. 14:22:19: Starting: "C:\Qt\Tools\QtCreator\bin\jom.exe" C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug C:\Qt\5.3\msvc2013\bin\uic.exe ..\test7\mainwindow.ui -o ui_mainwindow.h cl -c -nologo -Zm200 -Zc:wchar_t -FS -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc /Fddebug\test7.pdb -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\Qt\5.3\msvc2013\include" -I"C:\Qt\5.3\msvc2013\include\QtWidgets" -I"C:\Qt\5.3\msvc2013\include\QtGui" -I"C:\Qt\5.3\msvc2013\include\QtANGLE" -I"C:\Qt\5.3\msvc2013\include\QtCore" -I"debug" -I"." -I"." -I"C:\Qt\5.3\msvc2013\mkspecs\win32-msvc2013" -Fodebug\ @C:\Users\MyrvoldI\AppData\Local\Temp\main.obj.18096.15.jom cl -c -nologo -Zm200 -Zc:wchar_t -FS -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc /Fddebug\test7.pdb -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\Qt\5.3\msvc2013\include" -I"C:\Qt\5.3\msvc2013\include\QtWidgets" -I"C:\Qt\5.3\msvc2013\include\QtGui" -I"C:\Qt\5.3\msvc2013\include\QtANGLE" -I"C:\Qt\5.3\msvc2013\include\QtCore" -I"debug" -I"." -I"." -I"C:\Qt\5.3\msvc2013\mkspecs\win32-msvc2013" -Fodebug\ @C:\Users\MyrvoldI\AppData\Local\Temp\analogclock.obj.18096.31.jom C:\Qt\5.3\msvc2013\bin\moc.exe -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -D_MSC_VER=1800 -D_WIN32 -IC:/Qt/5.3/msvc2013/mkspecs/win32-msvc2013 -IC:/Users/MyrvoldI/Documents/Qt/test7 -IC:/Qt/5.3/msvc2013/include -IC:/Qt/5.3/msvc2013/include/QtWidgets -IC:/Qt/5.3/msvc2013/include/QtGui -IC:/Qt/5.3/msvc2013/include/QtANGLE -IC:/Qt/5.3/msvc2013/include/QtCore -I. ..\test7\mainwindow.h -o debug\moc_mainwindow.cpp C:\Qt\5.3\msvc2013\bin\moc.exe -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -D_MSC_VER=1800 -D_WIN32 -IC:/Qt/5.3/msvc2013/mkspecs/win32-msvc2013 -IC:/Users/MyrvoldI/Documents/Qt/test7 -IC:/Qt/5.3/msvc2013/include -IC:/Qt/5.3/msvc2013/include/QtWidgets -IC:/Qt/5.3/msvc2013/include/QtGui -IC:/Qt/5.3/msvc2013/include/QtANGLE -IC:/Qt/5.3/msvc2013/include/QtCore -I. ..\test7\analogclock.h -o debug\moc_analogclock.cpp analogclock.cpp main.cpp cl -c -nologo -Zm200 -Zc:wchar_t -FS -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc /Fddebug\test7.pdb -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\Qt\5.3\msvc2013\include" -I"C:\Qt\5.3\msvc2013\include\QtWidgets" -I"C:\Qt\5.3\msvc2013\include\QtGui" -I"C:\Qt\5.3\msvc2013\include\QtANGLE" -I"C:\Qt\5.3\msvc2013\include\QtCore" -I"debug" -I"." -I"." -I"C:\Qt\5.3\msvc2013\mkspecs\win32-msvc2013" -Fodebug\ @C:\Users\MyrvoldI\AppData\Local\Temp\mainwindow.obj.18096.47.jom mainwindow.cpp cl -c -nologo -Zm200 -Zc:wchar_t -FS -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc /Fddebug\test7.pdb -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\Qt\5.3\msvc2013\include" -I"C:\Qt\5.3\msvc2013\include\QtWidgets" -I"C:\Qt\5.3\msvc2013\include\QtGui" -I"C:\Qt\5.3\msvc2013\include\QtANGLE" -I"C:\Qt\5.3\msvc2013\include\QtCore" -I"debug" -I"." -I"." -I"C:\Qt\5.3\msvc2013\mkspecs\win32-msvc2013" -Fodebug\ @C:\Users\MyrvoldI\AppData\Local\Temp\moc_analogclock.obj.18096.171.jom moc_analogclock.cpp debug\moc_analogclock.cpp(55) : warning C4273: 'AnalogClock::qt_static_metacall' : inconsistent dll linkage c:\users\myrvoldi\documents\qt\build-test7-desktop_qt_5_3_msvc2013_32bit-debug\debug\../../test7/analogclock.h(9) : see previous definition of 'qt_static_metacall' debug\moc_analogclock.cpp(62) : warning C4273: 'staticMetaObject' : inconsistent dll linkage c:\users\myrvoldi\documents\qt\build-test7-desktop_qt_5_3_msvc2013_32bit-debug\debug\../../test7/analogclock.h(9) : see previous definition of 'public: static QMetaObject const AnalogClock::staticMetaObject' debug\moc_analogclock.cpp(62) : error C2491: 'AnalogClock::staticMetaObject' : definition of dllimport static data member not allowed debug\moc_analogclock.cpp(69) : warning C4273: 'AnalogClock::metaObject' : inconsistent dll linkage c:\users\myrvoldi\documents\qt\build-test7-desktop_qt_5_3_msvc2013_32bit-debug\debug\../../test7/analogclock.h(9) : see previous definition of 'metaObject' debug\moc_analogclock.cpp(74) : warning C4273: 'AnalogClock::qt_metacast' : inconsistent dll linkage c:\users\myrvoldi\documents\qt\build-test7-desktop_qt_5_3_msvc2013_32bit-debug\debug\../../test7/analogclock.h(9) : see previous definition of 'qt_metacast' debug\moc_analogclock.cpp(82) : warning C4273: 'AnalogClock::qt_metacall' : inconsistent dll linkage c:\users\myrvoldi\documents\qt\build-test7-desktop_qt_5_3_msvc2013_32bit-debug\debug\../../test7/analogclock.h(9) : see previous definition of 'qt_metacall' jom: C:\Users\MyrvoldI\Documents\Qt\build-test7-Desktop_Qt_5_3_MSVC2013_32bit-Debug\Makefile.Debug [debug\moc_analogclock.obj] Error 2 cl -c -nologo -Zm200 -Zc:wchar_t -FS -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc /Fddebug\test7.pdb -DUNICODE -DWIN32 -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\Qt\5.3\msvc2013\include" -I"C:\Qt\5.3\msvc2013\include\QtWidgets" -I"C:\Qt\5.3\msvc2013\include\QtGui" -I"C:\Qt\5.3\msvc2013\include\QtANGLE" -I"C:\Qt\5.3\msvc2013\include\QtCore" -I"debug" -I"." -I"." -I"C:\Qt\5.3\msvc2013\mkspecs\win32-msvc2013" -Fodebug\ @C:\Users\MyrvoldI\AppData\Local\Temp\moc_mainwindow.obj.18096.171.jom moc_mainwindow.cpp ..\test7\analogclock.cpp(5) : warning C4273: 'AnalogClock::AnalogClock' : inconsistent dll linkage c:\users\myrvoldi\documents\qt\test7\analogclock.h(12) : see previous definition of '{ctor}' ..\test7\analogclock.cpp(17) : warning C4273: 'AnalogClock::paintEvent' : inconsistent dll linkage c:\users\myrvoldi\documents\qt\test7\analogclock.h(15) : see previous definition of 'paintEvent' jom: C:\Users\MyrvoldI\Documents\Qt\build-test7-Desktop_Qt_5_3_MSVC2013_32bit-Debug\Makefile [debug] Error 2 14:22:22: The process "C:\Qt\Tools\QtCreator\bin\jom.exe" exited with code 2. Error while building/deploying project test7 (kit: Desktop Qt 5.3 MSVC2013 32bit) When executing step "Make" 14:22:22: Elapsed time: 00:03.```