You are not logged in.

1

Tuesday, March 9th 2010, 3:58pm

(Qt Beginner) How to link previously written c++ console code to Gui created using qt

Hello everyone, I am new here. I am a beginner in Qt but i am an intermediate level programmer in c++. I had written a program previously in C++ and STL(No code is platform dependent). Now if I develop A Gui in qt can I link it to my old program or should I rewrite everything.
For instance I use file IO using fstream. But in Qt I see there is QFile so is it possible to link it. The program is pretty lengthy. So it will be very hard to rewrite everything. It is for my Project so please reply soon. Thank you in Advance... Abhi

2

Tuesday, March 9th 2010, 4:36pm

try to read andd use QTextStream and QDataStream

3

Tuesday, March 9th 2010, 5:19pm

Using those two requires a lot of modification of the code. I dont have that much time. Cant I link the two programs by some means. Can I call the functions which do the work from qt by just sending the inputs taken from Qt gui window to my main program?

Gui is not mandatory for me but I thought it would be good for presentation..

4

Tuesday, March 9th 2010, 5:46pm

imho, you might do it.
I see 2 ways:
1. communication of two (or more program) using QProcess and other tools for interprocess communication. But I don't know this way.
2. separate your code on 2 parts - Gui + Logic. Gui is designed by Qt. Logic - by any tools, because Logic parts was place in Dll.
If you have any question about second way, you're welcome)). Now I've been developing GUI (Qt, MSVC++) for strong Dll (CAE for modeling multybody mechanical systems, was written by MFC and MSVC++)

krsmichael

Trainee

Posts: 62

Location: California

Occupation: Software Engineer

  • Send private message

5

Friday, March 12th 2010, 2:35pm

Is your code one large block in main? Or did you organize your code into logical files with corresponding headers? QT can call into your libraries. You should not store QObject* derived objects in stl containers.

So for example, i used SSL and and lib curl with QT. Those are c apis and are very usable from QT.

Michael

6

Monday, March 15th 2010, 3:16pm

Quoted

Is your code one large block in main?
No, my main function is little. There was include : setting Locale, parser of command line and constructor of my-super-special-class.

Quoted


Or did you organize your code into logical files with corresponding headers?
My code is organized into files *.h, *.cpp, *.ui, *.qrc.

Quoted

QT can call into your libraries.
Yes. my-super-special-class is intended for: Load dll, start GUI and working with it.

Quoted

You should not store QObject* derived objects in stl containers.
I think this is not true. Below you can see proof-code.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <QFileDialog>
#include <QApplication>

const QString stShWarning = QString::fromUtf8("background-color: rgb(255, 255, 127);") ;

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QFileDialog *pFD = new QFileDialog();
QList<QWidget *> allWidget = pFD->findChildren<QWidget *>();
std::list<QWidget *> dbgList;
foreach(QWidget * pwgt, allWidget )
{
dbgList.push_back(pwgt);// filling stl-container
}

foreach(QWidget * pwgt,dbgList)
{
pwgt->setStyleSheet(stShWarning);// using stl-container
}
return pFD->exec();
}

7

Tuesday, March 16th 2010, 11:36am

ok, I just created a static library with all the functions and classes(basically the logic) using Dev-c++. Now is there a way to link this library with Qt application using signals and slots for instance i press a button and a slot is called and that slot calls a function from the library, maybe something like that. Can it be done???

8

Tuesday, March 16th 2010, 12:58pm

Well.
1. try to create Dll (not static library) with Logic-part.
2. Each exported function in Dll must be defined like this:
extern "C" __declspec(dllexport) TypeOfRedult __cdecl NameFunction(/*parameters*/);
for example

Source code

1
extern "C" __declspec(dllexport) double __cdecl MySquare(const double & val){return val*val;};

3. Check for exporting functions in Dll (I'm often using 'PE Explorer').
4. Try to load your Dll in your Qt-program (see QLibrary)
5. Get a pointer of function in Dll
for example

Source code

1
2
3
typedef  __declspec(dllimport) double (__cdecl *type1)(const double& );
type1 MySq = (type1)m_pLib->resolve("MySquare");//m_pLib - pointer of QLibrary
// if function was corrected, MySq!=NULL

6. now you have a good pointer at function and you might use it anywhere))

9

Tuesday, March 16th 2010, 2:33pm

Thanks man I will give it a try. BTW I have no previous experience with dlls so I have no clue what you say. Anyways I will check out online guides and get back later.. Thanks Again..

10

Wednesday, March 17th 2010, 10:42am

Ops!
If you have static library you also might be able to do.
1. append lib in your project
2. include neded h-files
3. use your function with GUI-code.

Rate this thread