• 36271阅读
  • 28回复

【原创】学习QT4初步编程(二) [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2005-08-21
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重新编辑 ]
附件: ex32.rar (25 K) 下载次数:876
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线血魔
只看该作者 1楼 发表于: 2005-08-21
版大 看完这个以后应该怎么做了....文档该看些什么(都是英文所以不敢全看,现在看完了designer,但是好模糊) ,想作个菜单都不晓得用什么东东,还有custom widget不会全部代码都自己遍把..请指条明路.......hoho.
离线XChinux

只看该作者 2楼 发表于: 2005-08-21
最好是看书,从论坛上下载那些QT学习的书看,那上面讲得比较的适合于学习。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线晋-dragon
只看该作者 3楼 发表于: 2005-08-22
以前在RH9下学过一阵子,后来由于没有配置好,放弃了,看看现在能不能坚持下来
我记得红旗出过一本这方面的书,现在好像是科学出版社出一本linux下C++进阶,不错,以前在图书馆借的,现在忘了具体名字,好像是台湾的次元文化写的
我的blog:
http://spaces.msn.com/members/lianyunqing/
离线giscn
只看该作者 4楼 发表于: 2005-08-23
designer上提到的有三种方法,

(1)直接使用 ui_ 类
(2)single inheritance,将ui_ 类做为private member
(3)multiple inheritance, 如楼主所述
http://nzt.spaces.live.com
离线XChinux

只看该作者 5楼 发表于: 2005-08-23
对,使用哪种方法,熟悉C++的话,都可以。要知道其原理。
我是发现QT工具里的例子这样写,看来QT里也没有统一的格式。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线acefunware

只看该作者 6楼 发表于: 2005-08-23
楼主
上面的form。ui 好像不能用啊
我用uic生成的时候它说格式错误
email:acefunware@yahoo.com.cn
blog:http://acefunware.shineblog.com
离线XChinux

只看该作者 7楼 发表于: 2005-08-23
不会吧,那你下载附件,里面有所有源文件。
再看看。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线quitecn
只看该作者 8楼 发表于: 2005-09-06
XChinux大哥,
我在用QT designer加载图片时出现了问题,还请指教。我在一个Dialog里面放一个pixmaplabel然后设置属性pixmap即加载图片,程序运行正常。但当我下一次打开这个项目时,重新编译,运行,应该显示的图片就无法显示,请问该怎样设置??多多谢!
离线feihui89
只看该作者 9楼 发表于: 2006-04-19
大家好!
不知我是否可成为编程中的一员,因为我才刚开始学,虽然我很早之前就想学,不过一直没机会,现在机会有了,不过没人提示,“自学”我不懂。还请各位指点指点………………

谢谢!
做自己喜欢做的事;
让别人去说。……
离线XChinux

只看该作者 10楼 发表于: 2006-04-19
尝试着从小程序做起,慢慢就会了。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线legend_jacky
只看该作者 11楼 发表于: 2006-05-23
有个不大不小的问题,也许有点幼稚-我是菜鸟
“ex32 en
  ex32 zh_Cn
  ex32 zh_TW


总不能这样让人家执行程序吧,

可不可以,举个简单的例子,直接 ex32 就可以了啊

是不是就用tr(),可是我不会,呵呵,拜托
离线forestlier

只看该作者 12楼 发表于: 2006-08-17
form.ui

QUOTE:

<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>


这部分是怎么生成的啊
离线XChinux

只看该作者 13楼 发表于: 2006-08-17
引用第12楼forestlier2006-08-17 17:37发表的“”:
form.ui
QUOTE:
<ui version="4.0" >
.......



它是Designer设计窗体生成的.ui文件,是XML格式的,所以能看到它的实际文本内容。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线mumu902125
只看该作者 14楼 发表于: 2006-09-27
楼主你好,
我是菜鸟,弱弱的问一下:form.ui中我看不懂你到底在Designer中设置了什么控件
我很想按照你的步骤先熟悉一下这个全过程,我该怎么办?
能否将你设置的界面让我看看!
谢了
每天进步一点点!
离线XChinux

只看该作者 15楼 发表于: 2006-09-27
主题帖的附件里有源码,你打开里面的.ui文件就能看到。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线cauc_dg
只看该作者 16楼 发表于: 2006-10-26
一头雾水
每天进步一点点
离线cauc_dg
只看该作者 17楼 发表于: 2006-10-28
en en 恩,好铁啊,
每天进步一点点
离线philipser

只看该作者 18楼 发表于: 2006-11-06
请问QT4由form.ui怎样生成form.h 和form.cpp,好像和以前的2.3.x不一样了,uic的参数变了
离线XChinux

只看该作者 19楼 发表于: 2006-11-07
引用第18楼philipser2006-11-06 17:13发表的“”:
请问QT4由form.ui怎样生成form.h 和form.cpp,好像和以前的2.3.x不一样了,uic的参数变了


现在的Designer不能用来写代码,只能用来设计界面,可由其生成ui_xxxx.h头文件,这样在使用的时候,包含这个头文件来使用,看看例子就明白了。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线gxichun

只看该作者 20楼 发表于: 2006-11-10
Re:【原创】学习QT4初步编程(二)
好贴,顶
[ 此贴被gxichun在2007-01-25 09:47重新编辑 ]
离线flj01

只看该作者 21楼 发表于: 2007-03-08
我要成为qt编程高手!
离线tina_peggy69
只看该作者 22楼 发表于: 2007-03-09
版主你好!不好意思在此提问的,因为我把问题发表后,不知为什么没有人能给俺指点一下的,呵呵。看这里好像高手多,请帮帮忙,多谢了!QActionGroup使用中的一个问题

我在“mode” 菜单下,有mode1,mode2,mode3 三个选项,属于QActionGroup组,每次操作只能选其一,但是,我想在选择某一项(比如mode1)后,要弹出一个对话框进行相应设置。问题是:当我第一次选择mode1时弹出对话框,设置后关闭对话框,此时mode1菜单边有一勾号表示当前选项,然而当我紧接着再次打开菜单mode->mode1时,不能弹出设置筐。若我先改变选择mode->mode2,然后再选择mode->mode1,还是可以弹出设置筐的。我的本意是要弹出设置筐,不管当前选择是否是mode1,因为我想从新设置,却又不可以先选择别的mode。请教高手给予指点!谢谢!请教高手给予指点!谢谢!
离线zhaoli
只看该作者 23楼 发表于: 2008-03-28
版主你好,我用的是 Qt4版本的,在用Qt designer设计好界面后,按你上面教程所说"然后,我们新建一个类,来与上面生成的界面结合使用mywindow.h",以及"main.cpp  mywindow.cpp的输入".我不明白,因为qt4只能生成界面,不能输入文本,我不明白在什么地方新建类,在什么地方输入.cpp .h文件.我很困惑,因为刚学qt,问题都比较低级,请版主帮忙解答.十分感谢
努力学会qt
离线xiamenxw
只看该作者 24楼 发表于: 2009-04-26
一步步来吧!有大家的帮助,我相信我可以!
我很菜,但是我想我可以!
离线四个硬币
只看该作者 25楼 发表于: 2009-09-29
向版主问一个弱智的问题哈
/********
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();
}
*****************/
上面的main中有几行有关翻译的东西:
离线四个硬币
只看该作者 26楼 发表于: 2009-09-29
QTranslator translator;
  translator.load(QString(argv[1]));
  app.installTranslator(&translator);


这是什么意思呢,具体用来做什么呢,不用它也同样可以的啊
离线348227973

只看该作者 27楼 发表于: 2010-05-18
帮助很大谢谢楼主了。。。
离线348227973

只看该作者 28楼 发表于: 2010-05-27
学习!!
快速回复
限100 字节
 
上一个 下一个