30 сент. 2008 г.

Qt4: Quick start with loading extern ui

Well... I'm java developer with more than 5 years experience, but due to some mystic forces I should build QT application.

Let's start with creation simple Qt 4 application, build simple form with Qt Designer:

Added: Qt ui file is xml, simple.ui is follow xml:
<ui version="4.0" >
<widget class="QWidget" name="Form" >
<property name="geometry" >
<property name="windowTitle" >
<string>Simple form</string>
<layout class="QHBoxLayout" name="horizontalLayout" >
<widget class="QLabel" name="label" >
<property name="text" >
<widget class="QLineEdit" name="lineEdit" />

create main.cpp:
#include <QApplication>

#include "ui_simple.h"

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

QWidget *w = new QWidget;
Ui::Form form;

return app.exec();

ok, we have main.cpp and simple.ui
simple $ ls
main.cpp simple.ui
Now, create simple.pro
simple $ qmake -project
simple $ ls
main.cpp simple.ui simple.pro
...and create Makefile
simple $ qmake simple.pro
That's all. just make it!

Current task: to load dynamical simple.ui from other application with out precompilation.
Reference doc is QUiLoader Class Reference.

 simple $ mkdir ../loader && cd ../loader

main.cpp for dynamic loading:
#include <QApplication>
#include <QUiLoader>
#include <QFile>
#include <QWidget>

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

QUiLoader loader;
QFile file("../simple/simple.ui");
QWidget *myWidget = loader.load(&file);


int res = app.exec();
delete myWidget;
return res;
create pro file
loader $ qmake -project
and add to loader.pro follow line:
CONFIG += uitools

compile and run it:
loader $ qmake loader.pro && make && ./loader

that's really all: we've got a qt 4 application with dymical ui loading from extern file.

1 комментарий:

devel0per комментирует...

Hi dude, `cat` of simple.ui is very usefull for readers of this article.