用Eclipse开发QT4的C++程序
2010-01-25 11:12
288 查看
用Eclipse开发QT4的C++程序
如何安装
我用的平台是Ubuntu904,在Ubuntu下安装QT4的相当简单的事情:sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-designer
ctags
如果本机还没有安装C++开发环境,那你还需要:
sudo apt-get install build-essential g++ gcc-
4.3
-doc
出于推崇Linux的目的,这里只介绍Ubuntu平台的安装方法. Windows平台的同学请自行Google. :-)
配置Eclipse开发环境
从这个地址下载Eclipse CDT. 记得下载C/C++开发版本. 顺便说句题外话,现在的Eclipse版本号是Galileo,这是谁? 玩了两个铁球同时着地的伟大科学家加利略! 向伟人致敬.Eclipse download: http://www.eclipse.org/downloads/
从这个地址下载Qt的Eclipse插件:
QT4 Integration download: http://www.qtsoftware.com/developer/eclipse-integration
打开Eclipse , 在"Window->Preference->qt" 里配置如下信息:
Name : Qt4
Bin Path : /usr/bin
Include Path : /usr/include/qt4
这是Ubuntu8.x/904 默认安装的qt4路径.如果你的路径和此不同,请自行修改.
然后你会看到一个漂亮的GUI设计界面. 曾经在vb/delphi 下做过程序的同学,估计会对这个界面感慨万千.......
一个HelloWorld工程
QT4的官方网站有一篇很棒的介绍如何在Eclipse下进行开发的文章: http://doc.trolltech.com/qt-eclipse-1.5.0/eclipse-integration-getting-started.html但这篇文章有点长. 所以我自行写了一个HelloWorld例子介绍开发流程.
首先,New 一个QT Gui Project:
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
点next
之后,
输入lHelloWorld
作为工程名字,
如下图:
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
你可以直接点Finish
结束.
也可以点Next
看看QT
为你生成了什么:
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
我们不需对预定义的设置作改动.
直接点Finish
即可.
结束工程配置之后,
你会看到QT
为你生成了许多文件:
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
双击helloworld.ui
文件,
拖动一个push button
和
一个label
对象到helloworld.ui
上.
并且设置
push button
的 Object
name
属性为 helloButton,text
属性为”Click
me”, label
的Object name
为 helloLabel, text
属性为”Hello,QT!”
如下图:
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
现在,
我们想在单击button
的时候改变label
显示的文字.
如果用VB/Delphi/
浏览器里常用的事件术语来描述我们的需求的话,
需求如下:
1
单击button
的时候,
发生一个button
的click
事件.
2
必须编写一个事件处理函数,
在button
的onclick
发生后,
处理该事件.
3
这个事件处理函数的内容很简单.
改变label
的text
值即可.
很显然,
我们只需要关注3,
即如何添加事件处理函数即可.
打开helloworld.h,
添加如下代码:
private
slots:
void
on_helloButton_clicked
();
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
打开helloworld.cpp
添加如下代码:
void
HelloWorld::on_helloButton_clicked
(){
ui
.
helloLabel
->
setText
(
"Great!Your
first
Qt
application works fine!"
);
}
最后,这两个文件的代码如下所示:
helloworld.h
#ifndef HELLOWORLD_H
#define
HELLOWORLD_H
#include
<
QtGui
/
QWidget
>
#include
"
ui_helloworld.h
"
class
HelloWorld :
public
QWidget
{
Q_OBJECT
public
:
HelloWorld(QWidget
*
parent
=
0
);
~
HelloWorld();
private
:
Ui::HelloWorldClass ui;
private
slots:
void
on_helloButton_clicked();
};
#endif
//
HELLOWORLD_H
helloworld.cpp
#include
"
helloworld.h
"
HelloWorld::HelloWorld(QWidget
*
parent)
: QWidget(parent)
{
ui.setupUi(
this
);
}
HelloWorld::
~
HelloWorld()
{
}
void
HelloWorld::on_helloButton_clicked(){
ui.helloLabel
->
setText(
"
Great!Your first Qt application works fine!
"
);
}
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
快捷键
ctrl+b
编译工程
.
然后打开
Eclipse->Run->Run
configurations,
配置如下
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
然后点击run,
你第一个Qt
程序就跑起来了.
<!--
@page { margin: 2cm }
P { margin-bottom: 0.21cm }
-->
伟大的<C programming
language>
里说过,
学一门语言,
只要你懂得如何写程序,
编译,
运行,
那么学习这门语言将不再是难事.
本文介绍了在Eclipse
里基本的Qt
开发流程,
希望对大家有帮助.
相关文章推荐
- 如何在Linux使用Eclipse + CDT开发C/C++程序? (OS) (Linux) (C/C++) (gcc) (g++)
- 如何在Linux下用eclipse开发C/C++程序
- Eclipse开发C/C++程序的MinGw配置
- 如何在Linux使用Eclipse + CDT开发C/C++程序?
- 如何在Linux使用Eclipse + CDT开发C/C++程序?
- 如何在Linux使用Eclipse + CDT开发C/C++程序?
- Eclipse开发C/C++程序的配置方法.(ZT)
- Linux环境下使用eclipse开发C++动态链接库程序
- 配置Eclipse+MinGW开发C/C++程序
- 使用Eclipse+Cygwin开发Windows C++程序
- linux 下eclipse+cdt安装 用于c++程序开发
- 如何在Linux使用Eclipse + CDT开发C/C++程序?
- 集成 CDT 插件至 Eclipse 全过程,并开发你第一个 Eclipse 下的 C/C++ 程序
- Linux环境下使用eclipse开发C++动态链接库程序
- Eclipse 开发C++ 程序 (三) 支持 C++ 11
- 初步学习:Eclipse3.6开发C/C++程序(Windows下)
- 如何在Linux使用Eclipse + CDT开发C/C++程序?
- 如何在Linux使用Eclipse + CDT开发C/C++程序? (OS) (Linux) (C/C++) (gcc) (g++)
- 在Linux使用Eclipse + CDT开发C/C++程序