Ubuntu QT配置入门

来源:互联网 发布:指画修图软件 编辑:程序博客网 时间:2024/06/18 05:57

    Ubuntu QT配置入门
  1. QT是什么?
  2.     QT是一个跨平台的C++ GUI库,其官方网站为http://www.qtsoftware.com。
  3. QT的安装
    • 源码安装
    • 下载qt-x11-opensource-src-4.3.2.tar.gz,解压到临时目录中。
      因Ubuntu在默认安装下,并没安装编程环境,则必须安装编译环境:
      sudo apt-get install build-essential
      安装后,则g,g++,make会被安装。否则下一步执行时会出错终止!
      在安装完编译环境后,进入安装程序所以目录,在终端下,运行
      ./configure
      在输入"yes"同意后, Qt将自动configure。这一阶段要很长时间!
      Qt会默认安装到/usr/local/Trolltech/Qt-4.3.1这个目录下。当configure完后,运行make,等待编译Qt。
      当make完成后,运行
      sudo make install
      当这几个步骤全部完成后,把“PATH=/usr/local/Trolltech/Qt-4.3.1/bin:$PATH。export PATH”加到.profile中,Qt就安装成功了。
    • Synaptic软件管理器(新立得)
    •     在Synaptic中搜索与QT4相关的包,标记安装即可。也可以使用命令行,命令如下:
      sudo apt-get install qt4-demos qt4-designer qt4-dev-tools qt4-doc qt4-doc-html
      这种方法比较自动化,可以自动设置好QT开发的相关路径环境等等。
  4. 安装方法有两种:
  5. 小问题(Tips)
  6. 写个程序测试一下:

    #include <QApplication>
    #include <QString>
    #include <QLabel>
    #include <QWidget>
    #include <QDebug>
    #include <QTextStream>

    int main(int argc, char * argv[])
    {
        QApplication myapp(argc, argv);
        QWidget wid;
        qDebug() << "sizeof widget: " << sizeof(wid)
            << " sizeof qapplication: " << sizeof(myapp)
            << endl;
        QString message;
        QTextStream buf(&message);
       
        buf << "A QWidget is " << sizeof(wid) << "bytes. /nA QObject is " << sizeof(QObject)
            << "bytes. /nA QApplication is " << sizeof(myapp) << "bytes." << endl;
       
        qDebug() << message;
        QLabel label(message);
        return myapp.exec();
    }
    可能会出现QApplication not found的问题。具体参考这篇文章下面是引用:
    具体报错为:QApplication not found or not such a file 之类的一大堆。由于是用的Ubuntu在线安装的Qt4,故一般不会出现类库缺失或者无法使用的情况。根据以往的经验,问题计有可能出在编译器上。
    执行:
    ~$  export
    显示系统环境变量,可以找到类库的路径,没问题。
    ~$  qmake -v
    QMake version 2.01a
    Using Qt version 3.2 in /usr/lib

    问题出来了,由于我们这个测试程序是基于Qt4的,在Qt3下是无法编译通过的,因此应该将编译器改为Qt4。但是我明明在系统中安装的是Qt4,没有用 Qt3啊~~于是继续:
    ~$  which qmake
    qmake: /etc/alternatives

    ok,找到了,
    ~$  cd  /etc/alternatives
    找到qmake,是一个链接,找到它的源文件,竟然是/usr/bin/qmake-qt3 !!!!原来如此。接下来就简单了,删掉这个链接,然后做一个到/usr/bin/qmake-qt4的链接就ok了。如下:
    ~$  sudo rm /etc/alternatives/qmake
    ~$  sudo ln -s  /usr/bin/qmake-qt4 /etc/alternatives/qmake

    再试一下,现在系统的Qt版本,
    ~$  qmake -v
    QMake version 2.01a
    Using Qt version 4.3.4 in /usr/lib

    哈哈,搞定,编译及结果:
    ~$  qmake -project
    ~$  qmake
    ~$  make

    默认结果是一个名为qt的可执行程序(当然也可以在Makefile中将 TARGET 项改为别的名字),执行之:
    ~$  ./qt
    "A QWidget is 20bytes. 
    A QObject is 8bytes. 
    A QApplication is 8bytes.


        先写这么多,学习笔记。