QT学习

一.关于QT

1,QT不是一种新的编程语言,而是一种基于c++的图形用户界面编程库.

2,Qt支持下述平台:

  MS/Windows - 95、98、NT4.0、ME、和2000

  Unix/X11 - Linux、SunSolaris、HP-UX、Compaq Tru64 UNIX、IBM AIX、SGIIRIX和其它很多X11平台

  Macintosh - Mac OS X

  Embedded -有帧缓冲(framebuffer)支持的Linux平台

3,Qt是Trolltech公司的一个产品.

4,QT有不同的发行版本:

  Qt企业版Qt专业版提供给商业软件开发。

  Qt自由是Qt仅仅为了开发自由和开放源码软件提供的Unix/X11版本。

  Qt/嵌入式自由是Qt为了开发自由软件提供的嵌入式版本。

二,QT的安装

1,下载QT源程序.

2, 在X11下安装:  

    cd/usr/local
    gunzipqt-x11-version.tar.gz   # 对这个包进行解压速
    tar xfqt-x11-version.tar      # 对这个包进行解包
    mv qt-version qt
这里假设Qt要被安装到/usr/local/qt路径下。

在你的主目录下的.profile文件(或者.login文件,取决于你的shell)中设置一些环境变量。如果它们并不存在的话,就创建它们。

  • QTDIR -- 你安装Qt的路径
  • PATH -- 用来定位moc程序和其它Qt工具
  • MANPATH -- 访问Qt man格式帮助文档的路径
  • LD_LIBRARY_PATH -- 共享Qt库的路径

就像下面这样做:

在.profile文件(如果你的shell是bash、ksh、zsh或者sh)中,添加下面这些行:

    QTDIR=/usr/local/qt    PATH=$QTDIR/bin:$PATH    MANPATH=$QTDIR/man:$MANPATH    LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH    export QTDIR PATH MANPATH LD_LIBRARY_PATH在.login文件(如果你的shell是csh或者tcsh的情况下),添加下面这些行: 
    setenv QTDIR /usr/local/qt    setenv PATH $QTDIR/bin:$PATH    setenv MANPATH $QTDIR/man:$MANPATH    setenv LD_LIBRARY_PATH $QTDIR/lib:$LD_LIBRARY_PATH做完这些之后,你需要重新登录,或者在继续工作之前重新指定你的配置文件,
这样至少$QTDIR被设置了。否则的话安装程序就会给出一个错误信息并且不再进行下去了。
 安装你的许可证文件。对于自由版本,你不需要一个许可证文件。对于专业版和企业版,
你就需要安装一个和你的发行版一致的许可证文件。 编译Qt库,并且连编实例程序、教程和工具(比如Qt设计器),
就像下面这样。 

输入:

    ./configure

这样的话就为你的机器配置Qt库。注意在默认条件下GIF文件支持选项是关闭的。运行./configure-help就会得到配置选项的一个列表。阅读PLATFORMS文件能够得到被支持的平台的列表。

生成库和编译所有的例程和教程:

    make

如果你有问题,请看http://www.trolltech.com/platforms/

在很少的情况下,如果你使用了共享库,在这个地方你也许需要运行/sbin/ldconfig或者其它相似的东西。

如果你在运行实例程序的时候遇到问题,比如消息如下

    can’t load library ’libqt.so.2’

你也许需要在配置文件中给定一个qt库的定位,并且以root的身份在你的系统中运行/sbin/ldconfig。并且你不要忘记了在上面的第二步中提到的设置一个LD_LIBRARY_PATH环境变量。

在线的HTML文档被安装到了/usr/local/qt/doc/html/,主页面是/usr/local/qt/doc/html/index.html。man帮助文档被安装到了/usr/local/qt/doc/man/。

你已经做完了。Qt已经安装完毕。

3,在嵌入式系统下的安装

  1. Unpack the archive if you have not done so already

        cd 
        
        ;    gunzip qt-embedded-VERSION-commercial.tar.gz    # uncompress the archive    tar xf qt-embedded-VERSION-commercial.tar       # unpack it
    Replace VERSION with the Qt/Embedded version number throughout.

    This document assumes that the archive is installed as~/qt-VERSION.

  2. Compile the Qt/Embedded library and examples.
        cd ~/qt-VERSION    export QTDIR=~/qt-VERSION    ./configure    make

    The configuration system is designed to allow platform-specificoptions to be added, but in general all Linux system which haveframebuffer support can use the "linux-generic-g++" platform. Theconfiguration system also supports cross-compilers: to buildon Linux/x86 for the Linux/MIPSEL target, youwould use:

        ./configure -platform linux-x86-g++ -xplatform linux-mips-g++

    Only a small number of configurations are predefined, all muchthe same. Configurations files are found in configs/.

  3. Enable framebuffer support.

    You may need to recompile your kernel to enable the framebuffer.This document does not describe how to do this; the HOWTO-Framebuffer page contains a shortdescription. (You should see a penguin logo at boot time when theframe buffer is enabled.)

    For Matrox G100/G200/G400 use the matrox frame bufferdriver.

    For NVidia TNT cards use the nvidia frame buffer driver.

    For Mach64 and most other cards, use the vesafb driver.

    Note that some cards are only supported in VGA16 mode, this willnot work with the current version of Qt/Embedded, since VGA/16 isnot yet supported. You may need to upgrade your kernel, or evenswitch to an experimental kernel.

    The frame buffer must also be enabled with a boot parameter. See/usr/src/linux/Documentation/fbfor details.

    The fbset program, whichshould be included in Linux distributions, may be used to switchvideo modes without rebooting the system. The video mode activewhen the server is started will be used. (8-bit modes are stillexperimental.) Note: fbset does not work with the vesafbdriver.

  4. Change permissions.

    To run Qt/Embedded, you need write access to the framebufferdevice /dev/fb0.

    You also need read access to the mouse device. (Note that/dev/mouse is normally asymbolic link; the actual mouse device must be readable.)

  5. How to run the demonstration program.

    Log into a virtual console and do:

        cd ~/qt-VERSION/    ./start-demo
  6. Miscellaneous troubleshooting and known bugs.

    To kill gpm, run the following command as root:

        gpm -k

    In some cases, if the server does not work, it will work whenrun as root.

    Some example programs may not compile with GCC 2.95.

    Show processes using the framebuffer:

        fuser -v /dev/fb0

    Kill such processes:

        fuser -vk /dev/fb0
    or harsher:
        fuser -k -KILL /dev/fb0

    Show existing semaphores:

        ipcs            

    Remove semaphores:

        ipcrm

    The communication between client and server is done through thenamed pipe /tmp/.QtEmbedded;sometimes it may need to be deleted (eg. if you run Qt/Embedded asroot then later as an unprivileged user).

  7. Customization.

    The Qt/Embedded library can be reduced in size by removing unnecessary features

三,QT编程

1,hello,world

源程序:

#include
#include
int main( int argc, char **argv )
{
    QApplicationa( argc, argv );
    QPushButtonhello( "Hello world!", 0 );
   hello.resize( 100, 30 );

   a.setMainWidget( &hello );
   hello.show();
    returna.exec();
}

解释:
#include
这一行包含了QApplication类的定义。在每一个使用Qt的应用程序中都必须使用一个QApplication对象。QApplication管理了各种各样的应用程序的广泛资源,比如默认的字体和光标。
#include
这一行包含了QPushButton类的定义。参考文档的文件的最上部分提到了使用哪个类就必须包含哪个头文件的说明。
QPushButton是一个经典的图形用户界面按钮,用户可以按下去,也可以放开。它管理自己的观感,就像其它每一个QWidget。一个窗口部件就是一个可以处理用户输入和绘制图形的用户界面对象。程序员可以改变它的全部观感和它的许多主要的属性(比如颜色),还有这个窗口部件的内容。一个QPushButton可以显示一段文本或者一个QPixmap。
int main( int argc, char **argv )
    {
main()函数是程序的入口。几乎在使用Qt的所有情况下,main()只需要在把控制转交给Qt库之前执行一些初始化,然后Qt库通过事件来向程序告知用户的行为。
argc是命令行变量的数量,argv是命令行变量的数组。这是一个C/C++特征。它不是Qt专有的,无论如何Qt需要处理这些变量(请看下面)。
QApplication a( argc, argv );
a是这个程序的QApplication。它在这里被创建并且处理这些命令行变量(比如在X窗口下的-display)。请注意,所有被Qt识别的命令行参数都会从argv中被移除(并且argc也因此而减少)。关于细节请看QApplication::argv()文档。
注意:在任何Qt的窗口系统部件被使用之前创建QApplication对象是必须的。
QPushButton hello( "Hello world!", 0 );
这里,在QApplication之后,接着的是第一个窗口系统代码:一个按钮被创建了。
这个按钮被设置成显示“Helloworld!”并且它自己构成了一个窗口(因为在构造函数指定0为它的父窗口,在这个父窗口中按钮被定位)。
 hello.resize( 100, 30 );
这个按酒被设置成100像素宽,30像素高(加上窗口系统边框)。在这种情况下,我们不用考虑按钮的位置,并且我们接受默认值。
 a.setMainWidget( &hello );
这个按钮被选为这个应用程序的主窗口部件。如果用户关闭了主窗口部件,应用程序就退出了。
你不用必须设置一个主窗口部件,但绝大多数程序都有一个。
hello.show();
当你创建一个窗口部件的时候,它是不可见的。你必须调用show()来使它变为可见的。
return a.exec();
这里就是main()把控制转交给Qt,并且当应用程序退出的时候exec()就会返回。
在exec()中,Qt接受并处理用户和系统的事件并且把它们传递给适当的窗口部件。
 }
你现在可以试着编译和运行这个程序了。
编译
编译一个C++应用程序,你需要创建一个makefile。创建一个Qt的makefile的最容易的方法是使用Qt提供的连编工具qmake。如果你已经把main.cpp保存到它自己的目录了,你所要做的就是这些:
qmake -project
qmake
第一个命令调用qmake来生成一个.pro(项目)文件。第二个命令根据这个项目文件来生成一个(系统相关的)makefile。你现在可以输入make(或者nmake,如果你使用VisualStudio),然后运行你的第一个Qt应用程序!
行为
当你运行它的时候,你就会看到一个被单一按钮充满的小窗口,在它上面你可以读到著名的词:HellowWorld!



欢迎您使用http://Blogmove.cn提供的"博客搬家"和"博文三窟"服务.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值