【QT学习】搭建环境+hello world

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

绝对是被迫学习QT,本来不想看这些东西来着,志不在此,不过实验室的项目用到,想到以后万一哪天想去写一个Linux下的小程序,弄个GUI看起来也蛮不错的。

实验环境是Windows……Microsoft Visual Studio 2008 MSDN 英文版 + Microsoft Windows XP SP3

1.环境搭建

安装VS2008+qt-win-commercial-4.5.0-vs2008+qt-vsintegration-1.4.3,Win平台果然省心许多……

2.用QT Design写个Hello World

打开VS2008-> File-> New –> Project –>QT Project-> QT Application ,去一个名字,叫做MyQT_Learn,然后都使用默认设置。

双击那个UI文件,从右边的QT Tool Box中拖出来一个Push Button

在其上右键单击,调出change text,该个名字叫做hello world

然后右键单击那个ui文件,选择compile,重新编译一下这个UI。

接着双击这个button,在on_pushButton_clicked()这个函数中写下:

QMessageBox::information(this,tr("hello"),tr("Hello world!"));

并在这个文件中写入头文件:#include

这样你就可以F5了。

另外,你还可以使用 HTML-style标签去美化你的界面文字

3.用代码写一个hello world

使用典型的信号槽机制,修改main.cpp:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    //MyQT_Learn w;
    QPushButton hellobycode("Hello QT World!");
    hellobycode.resize(100,30);
    QObject::connect(&hellobycode,SIGNAL(clicked()),&a,SLOT(quit()));
    hellobycode.show();
    return a.exec();
}

点击后关闭窗体。

4.写一个比较复杂的例子

作为这个Basic入门的结尾,我们写一个像样点的程序。我们首先创建一个QWidget工程:

我们设计一下ui,然后我们编译一下这个ui.

下边我们开始写这个小程序:

我们先在myqtapp.h中定义这个程序的主类:

#ifndef MYQTAPP_H
#define MYQTAPP_H

#include "ui_myqtapp.h"

class myQtApp : public QWidget, private Ui::myQtAppDLG
{
    Q_OBJECT

public:
    myQtApp(QWidget *parent = 0);

    public slots:
        void getPath();
        void doSomething();
        void clear();
        void about();
};

#endif

这里我们使用了多重继承。QWidget是我们创建的类型类,Ui::myQtAppDLG这个则是我们ui生成的类,在ui_myqtapp.h中有定义。

Q_OBJECT是一个宏,它展开了一些信号槽机制必须的定义。只有加入了Q_OBJECT,你才能使用QT中的signal和slot机制。

在构造函数后是我们定义的一些方法。

然后我们在myqtapp.cpp中实现相关方法

#include
#include "myqtapp.h"

// including saves us to include every class user, , ,...

myQtApp::myQtApp(QWidget *parent)
{
    setupUi(this); // this sets up GUI

    // signals/slots mechanism in action
    connect( pushButton_browse, SIGNAL( clicked() ), this, SLOT( getPath() ) ); //这就是所谓的信号槽机制
    connect( pushButton_do, SIGNAL( clicked() ), this, SLOT( doSomething() ) );
    connect( pushButton_clear, SIGNAL( clicked() ), this, SLOT( clear() ) );
    connect( pushButton_about, SIGNAL( clicked() ), this, SLOT( about() ) );
}

void myQtApp::getPath()
{
    QString path;

    path = QFileDialog::getOpenFileName(
        this,
        "Choose a file to open",
        QString::null,
        QString::null);

    lineEdit->setText( path );
}

void myQtApp::doSomething()
{
    int value1, value2;
    Qt::CheckState state;
    QString str;

    textEdit->append( "Path to file: " + lineEdit->text() );

    value1 = spinBox1->value();
    value2 = spinBox2->value();

    textEdit->append( "Number 1 value: " + QString::number(value1) );
    textEdit->append( "Number 2 value: " + QString::number(value2) );

    state = checkBox->checkState();

    str = "Checkbox says: ";
    if ( state == Qt::Checked ) str += "yes";
    else str += "no";
    textEdit->append( str );

    textEdit->append( "ComboBox current text: " + comboBox->currentText() );
    textEdit->append( "ComboBox current item: " + QString::number(comboBox->currentIndex()) );
}

void myQtApp::clear()
{
    textEdit->clear();
}

void myQtApp::about()
{
    QMessageBox::about(this, "About myQtApp",
        "这是一个测试程序,用于入门./n"
        "Number 1 is: " + QString::number(spinBox1->value()) + "/n/n"
        "Bye./n");
}

然后我们在主程序中就可以调用我们这个类了:

#include

#include "myqtapp.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    myQtApp *dialog = new myQtApp;

    dialog->show();//窗体展现
    return app.exec();
}

QApplication --> 负责程序的初始、结束及处理事件( event )的循环等,并提供基本的窗口外观,这个外观与系统的桌面环境有关,例如标题栏的样式、窗口外观、系统功能键等,在不同的操作系统桌面环境下,会有各自不同的外观, QApplication 对象接受命令行自变量作为它的自变量,像是如果您没有设定窗口标题,且会使用执行文件的名称作为窗口标题名称,可以使用的自变量与其作用,可以查询 Qt 在线文件关于 QApplication 类别的说明。在最后一行,调用了QApplication的exec()方法,这将程序的控制权交给了QApplication,exec()方法会提供一个事件处理循环,窗口显示之后会不断倾听(listen)事件,像是键盘、鼠标等动作所引发的事件。

QObject类是所有Qt对象的基类。它是Qt对象模型的中心。这个模型的中心特征就是一种用于无缝对象通讯的被叫做信号和槽的非常强大的机制。

附1:

整个工程下载:http://cid-a0a0b50959052db4.skydrive.live.com/self.aspx/.Public/MyQTFirstApp.rar

附2:Linux编译步骤

1. qmake -project  -- 生成平台无关的project文件(hello.pro )

2. qmake hello.pro --生成平台有关的makefile

3. make                 --编译

附3:QT命令行程序

[cpp:nogutter] view plaincopyprint?

  1. #include <QTextStream>
  2. #include <QCoreApplication>
  3. int main (int argc, char* argv[]) { 
  4.     QCoreApplication app(argc, argv); 
  5.     QTextStream cout(stdout, QIODevice::WriteOnly); 
  6.     cout << QString("hello world!") << endl; 
  7. }  

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

原文地址:https://www.cnblogs.com/gnuhpc/p/2811960.html