C++,QT und Eclipse (Windows)

Heute werden wir anfangen Eclipse und QT zu integrieren.

http://qt-project.org/downloads

Wir erstellen ein neues C++ Projekt in Eclipse.

Folgenden Ordner mit Headerfiles und Libs benötigen wir:

QtCore.

Als Test erstellen wir eine einfache QT KLasse Names Start.

[codesyntax lang=“cpp“]

#ifndef START_H_
#define START_H_
#include <QtCore/qobject.h>

class Start: public QObject {
	Q_OBJECT
	public:
		Start();
		virtual ~Start();
		void show();
	public slots:
		void actionGameeditor();
		void actionModeleditor();
		void actionExperimental();
};

#endif /* START_H_ */

[/codesyntax]

Die CPP Datei:

[codesyntax lang=“cpp“]

#include <QDebug>
#include <Start.h>

#include "qmessagebox.h"
#include "qpushbutton.h"
Start::Start() {
}

Start::~Start() {
}

void Start::actionGameeditor() {
    qDebug() << "actionGameeditor";
}

void Start::actionModeleditor() {
    qDebug() << "actionModeleditor";
}

void Start::actionExperimental() {
    qDebug() << "actionExperimental";
}

void Start::show() {
    qDebug() << "show";
    QMessageBox* msg = new QMessageBox(0);
    msg->setWindowTitle("Startup TC");
    QPushButton * btn = msg->addButton("Gameeditor", QMessageBox::NoRole);
    connect(btn, SIGNAL(pressed()), this, SLOT(actionGameeditor()));
    btn = msg->addButton("Modeleditor", QMessageBox::NoRole);
    connect(btn, SIGNAL(pressed()), this, SLOT(actionModeleditor()));
    btn = msg->addButton("Experimental", QMessageBox::NoRole);
    connect(btn, SIGNAL(pressed()), this, SLOT(actionExperimental()));
    msg->setModal(true);
    msg->show();
}
[/codesyntax]

 

Jetzt kommt der Teil das wir die Moc Files und die UI-KLassen generieren müssen.

Hierzu erstellen wir eine Batchdatei mit folgendem Inhalt:

[codesyntax lang=“winbatch“]

@echo off 
set PATH=%PATH%;D:/TC-SYSTEM/ide/Qt5.2.1/5.2.1/mingw48_32/bin


FOR /r %%F in (*.h) DO moc --no-warnings -o "%%~dF%%~pF/MOC_%%~nF.cpp" "%%F"

FOR /r %%F in (*.ui) DO uic.exe -o "%%~dF%%~pF/ui_%%~nF.h" "%%F"

FOR /r %%F in (*.qrc) DO rcc.exe -o "%%~dF%%~pF/qrc_%%~nF.cpp" "%%F"

for /r %%F in (*.cpp) do if %%~zF==0 del "%%F"
@echo on

[/codesyntax]

 

Das Batch ruft „moc“ von QT auf und erstellt die Generierten Files die zum Kompilieren nötig sind.

Der 2. Befehlt erstellt aus den QT UI Files die dazugehörenden Headerfiles.

Der 3. Befehl erstellte die Resourcedateien.

Der 4. Befehlt löscht Leere Dateien.

Anschließend müssen wir Eclipse beibringen unsere Batchdatei auch aufzurufen:

qtBuild

 

Abschließend noch die main.cpp.

[codesyntax lang=“cpp“]

#include <Start.h>

#include "qapplication.h"
int main(int argc, char *argv[]) {

	if (qApp == 0) {
		new QApplication(argc, argv);
	}

	Start start;
	start.show();
	return qApp->exec();
}

[/codesyntax]