PDA

View Full Version : How to Implement old ui into new project?



KillerBerry
11th July 2015, 20:48
My problem is basically very simple.
I have built a simple color picker application using Qt Creator 3.4.2 on Windows 7 Sp 1. This application has mutiple forms that are combined in a single widget. This is done through the usual way by inhariting a base class and using qt to build the ui header files.
I now want to include this application into another project. Trying to do so I imported the header file of the base widget (the one which includes the other parts of the color picker ui).
However when I try to instantiate an object of this class I get the linker errors (MinGW):
undefined reference to `ColorPicker::ColorPicker(QWidget*)
undefined reference to `ColorPicker::~ColorPicker()

And the a bit more cryptic message with MSVC:
unresolved external symbol "public: virtual __cdecl ColorPicker::~ColorPicker(void)" (??1ColorPicker@@UEAA@XZ) referenced in function main

The code that provokes the errors is shown below.

#include "dialog.h"
#include <QApplication>
#include <colorpicker.h>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();

ColorPicker picker;

return a.exec();
}

The header colorpicker.h is the one that I wrote and that should includes the other classes required to build the Ui.
It has been placed in a subdirectory of the project and imported using the INCLUDEPATH command in the .pro file. The neccessary ui_classname.h files have been placed in the same folder.
Its constructor is defined as follows in the header file:

explicit ColorPicker(QWidget *parent = 0);
With the following implementation in the source file

ColorPicker::ColorPicker(QWidget *parent) :
QWidget(parent),
ui(new Ui::ColorPicker)
{
ui->setupUi(this);
Some more code ...
}
Directly using the "ui_classname.h" includes the ui but none of the functionality (like signals and slots)
My suspicion is that the LINKER is unable to find the source file implementation for the headers.
How can a import a class which has a ui into my project (preferebly the same way you include e.g QWidget)

d_stranz
11th July 2015, 22:42
Your linker errors are telling you that the linker can't find the object code for your ColorPicker class. Using a class in another project means more than just including the header file. The linker needs to be able to find the implementation of the class as well.

You either 1) add the cpp file to your new project or 2) make a library that contains the classes that you want to re-use and link that to your new projects.

KillerBerry
12th July 2015, 23:29
Thank you very much kind stranger.
Worked flawlessly.