PDA

View Full Version : how to use Qt dll in Win32 project



Fastman
9th July 2009, 09:16
Hello !

I created a project Qt dll, It compiles and runs perfectly.
I get in the output files:
test.dll
test.lib
also have the following files:
test.h
test_global.h

Now, faced with the task, to connect the library to the project in C + + (without Qt!!!)
but in the files
test.h
test_global.h
there is a connection # include <Qt\qglobal.h>
And the project is not going to, and not linked.

Prompt how to create a header file that can connect the library to the project Win32 ?

wysota
9th July 2009, 09:23
You have to expose some interface that will not contain any Qt related things. Then make your Qt class inherit and implament that interface. Then you'll only deliver the base class header file, without any Qt related things.

Fastman
9th July 2009, 10:45
You have to expose some interface that will not contain any Qt related things. Then make your Qt class inherit and implament that interface. Then you'll only deliver the base class header file, without any Qt related things.

May be you can show some example ?

wysota
9th July 2009, 12:03
May be you can show some example ?

Let's see...


class SomeInterface {
public:
virtual ~SomeInterface(){}
virtual int method1() const = 0;
virtual std::string method2(int) = 0;
// etc.
};

class MyQtImplementation : public SomeInterface {
public:
MyQtImplementation(...){...}
int method1() const { qDebug() << "Qt here"; return 1; }
std::string method2(int num) { return QString::number(num).toStdString(); }
};

NoRulez
9th July 2009, 12:07
// H - Interface
class ISampleClass {
public:
virtual ~ISampleClass() {}

void DemoFunction1();
void DemoFunction2() = 0; // [OPTIONAL] Abstract function, so the complete class is abstract
};

// H - Implementation
class SampleClass : public QObject, public ISampleClass {
Q_OBJECT
public:
SampleClass(QObject *parent = 0);
virtual ~SampleClass() {}

void DemoFunction1();
void DemoFunction2();
};

// CPP - Implementation
SampleClass::SampleClass(QObject *parent) : QObject(parent) {
}

void SampleClass::DemoFunction1() {
// Do Something
}

void SampleClass::DemoFunction2() {
// Do Something
}


Hope this helps

LG NoRulez

Fastman
9th July 2009, 12:31
thx for help !!!

Fastman
9th July 2009, 22:49
Hmmm... but i'am have some trouble:


error LNK2019: unresolved external symbol "public: void __thiscall ISampleClass::DemoFunction1(void)" (?DemoFunction1@ISampleClass@@QAEXXZ) referenced in function _wmain


#include "stdafx.h"
#include <string.h>
#include <iostream>
#include "../test/ISample.h"
#pragma comment(lib,"D:\\QT_PROJECT\\test\\debug\\test.lib")

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
ISampleClass a;
a.DemoFunction1();
return 0;
}

What wrong ???

nish
10th July 2009, 02:04
do it this way..


// H - Interface
class ISampleClass {
public:
virtual ~ISampleClass() {}

static ISampleClass* getInstance();
virtual void DemoFunction1() = 0; //make everything pure virtual
virtual void DemoFunction2() = 0; // [OPTIONAL] Abstract function, so the complete class is abstract
private:
static ISampleClass *p;
};

// CPP - Implementation

ISampleClass* ISampleClass::p=0;

static ISampleClass* ISampleClass::getInstance();
{
if(!p)
p= new SampleClass;
return p;
}



// H - Implementation
class SampleClass : public QObject, public ISampleClass {
Q_OBJECT
public:
SampleClass(QObject *parent = 0);
virtual ~SampleClass() {}

void DemoFunction1();
void DemoFunction2();
};

// CPP - Implementation
SampleClass::SampleClass(QObject *parent) : QObject(parent) {
}

void SampleClass::DemoFunction1() {
// Do Something
}

void SampleClass::DemoFunction2() {
// Do Something
}

//MAIN
int _tmain(int argc, _TCHAR* argv[])
{
ISampleClass* a= ISampleClass::getInstance();
a->DemoFunction1();
return 0;
}

Fastman
10th July 2009, 12:16
Sorry, i'am stupid :) :o

Now everything works.
I realized QScritEngin in a DLL and is connected to an external win32 project.
Once again, thank you very much.