QT4已经发布,与以前的版本有些区别,以及在编程方式上也有区别。
这里以QT4 OpenSource版本为例讲解说明
基于原来的帖子”
学习QT4初步编程"
中我使用了修改QT Designer设计的界面生成的文件的方法,引起初学者的一些疑问,会引起初学者以为QT编程这么麻烦的感觉。这里,我采用QT官方发布的例子的编程模式来进行解说。
这个例子和上个例子差不多,只是某些开发模式变了一下。尤其是使用QT Designer生成的界面上面。
首先,我们用QT Designer设计一个界面,如下:
form.ui
<ui version="4.0" >
<author></author>
<comment></comment>
<exportmacro></exportmacro>
<class>FormEx</class>
<widget class="QWidget" name="FormEx" >
<property name="sizePolicy" >
<sizepolicy>
<hsizetype>5</hsizetype>
<vsizetype>5</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle" >
<string>QT 4.0.1 Test Example</string>
</property>
<widget class="QWidget" name="" >
<property name="geometry" >
<rect>
<x>10</x>
<y>260</y>
<width>398</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<widget class="QPushButton" name="PushButtonInsert" >
<property name="text" >
<string>&Insert</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="PushButtonClear" >
<property name="text" >
<string>&Clear</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" >
<size>
<width>71</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="PushButtonQuit" >
<property name="text" >
<string>&Quit</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QListWidget" name="ListWidgetContent" >
<property name="geometry" >
<rect>
<x>9</x>
<y>9</y>
<width>398</width>
<height>208</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="LineEditInsert" >
<property name="geometry" >
<rect>
<x>10</x>
<y>230</y>
<width>398</width>
<height>18</height>
</rect>
</property>
</widget>
</widget>
<pixmapfunction></pixmapfunction>
<resources/>
<connections>
<connection>
<sender>PushButtonQuit</sender>
<signal>clicked()</signal>
<receiver>FormEx</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel" >
<x>356</x>
<y>265</y>
</hint>
<hint type="destinationlabel" >
<x>348</x>
<y>236</y>
</hint>
</hints>
</connection>
<connection>
<sender>PushButtonClear</sender>
<signal>clicked()</signal>
<receiver>ListWidgetContent</receiver>
<slot>clear()</slot>
<hints>
<hint type="sourcelabel" >
<x>203</x>
<y>259</y>
</hint>
<hint type="destinationlabel" >
<x>201</x>
<y>140</y>
</hint>
</hints>
</connection>
</connections>
</ui>
然后,我们新建一个类,来与上面生成的界面结合使用
mywindow.h
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QDialog>
#include "ui_form.h"
class MyWindow:public QDialog, public Ui::FormEx
{
Q_OBJECT
public:
MyWindow(QWidget *parent = 0);
~MyWindow();
public slots:
void InsertItem();
};
#endif
在上面的头文件代码中,我们要说一下,它的使用QT Designer生成的form.ui文件的方式。
#include "ui_form.h"
这个"ui_form.h"文件并不存在,它是在编译的时候,由form.ui生成的一个头文件,
还有它的类名FormEx,这个是由用户在设计界面的时候,设置的,默认为Form或者其它的默认的名字(设计时选择的是MainWindow/Dialog/Widget方式而定,这里用的是Widget),还有前面的Ui::,这个是名字空间,FormEx是定义在名字空间Ui中的。
这里的模式,采用的是多重继承的方式,从QDialog和Ui::FormEx两个类来继承出来我们使用的类。还有一种方式在"
如何修改自动生成的.h文件"一贴中说明过了,就是使用单继承,而把FormEx类在类内部声明使用。
mywindow.cpp
#include <QMessageBox>
#include "mywindow.h"
MyWindow::MyWindow(QWidget *parent): QDialog(parent)
{
setupUi(this); // 这句很重要,这是Ui::FormEx类提供的一个方法,用于不界面中设计的元素都生成。
connect(PushButtonInsert, SIGNAL(clicked()), this, SLOT(InsertItem()));
}
MyWindow::~MyWindow()
{
}
void MyWindow::InsertItem()
{
if (LineEditInsert->text().isEmpty())
{
QMessageBox::critical(this, tr("Error"), tr("the input text box has no character"), QMessageBox::Ok, 0);
}
else
{
ListWidgetContent->addItem(LineEditInsert->text());
LineEditInsert->clear();
}
LineEditInsert->setFocus();
}
好了,下面我们来写main函数:
main.cpp
#include <QApplication>
#include <QTranslator>
#include "mywindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator translator;
translator.load(QString(argv[1]));
app.installTranslator(&translator);
MyWindow mywindow;
return mywindow.exec();
}
好了,现在就可以进行编译了。
我们运行如下命令:
qmake -project
qmake
make all
上面的make all这是要生成 Debug和Release版,如果要生成单一版本,比如make release,生成release版
注意那句QTranslator....,这里要用到翻译文件,也就是程序的多国语言支撑。
好,下面我们生成多国语言支撑文件
// 这句话是根据.cpp, .h, .ui文件中的信息,生成多国语言支撑的原始文件
lupdate *.cpp *.h *.ui -ts ex32.ts
接下来,我们就可以使用QT Linguist来进行翻译了。
打开QT Linguist后,用它把ex32.ts打开,就能进行翻译了。
翻译完了每种语言后,用菜单的"release"功能,把它保存为*.qm文件。
我们这里需要三种语言,分别是英文(en),简体中文(zh_CN)和繁体中文(zh_TW)
我们把这三个文件拷贝到可执行文件目录下面(release或debug),然后就能运行了
ex32 en
ex32 zh_Cn
ex32 zh_TW
上面的三个命令,分别是英文版,简体中文和繁体中文版。
下面的附件是源文件,可执行文件的打包(Win32版的)
[ 此贴被XChinux在2005-08-21 18:21重新编辑 ]