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:

simple.ui
Added: Qt ui file is xml, simple.ui is follow xml:
<ui version="4.0" >
<class>Form</class>
<widget class="QWidget" name="Form" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>510</width>
<height>90</height>
</rect>
</property>
<property name="windowTitle" >
<string>Simple form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout" >
<item>
<widget class="QLabel" name="label" >
<property name="text" >
<string>Title</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit" />
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

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;
form.setupUi(w);
w->show();

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");
file.open(QFile::ReadOnly);
QWidget *myWidget = loader.load(&file);
file.close();

myWidget->show();

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 комментарий:

Анонимный комментирует...

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