Qt之hello world

 

本人使用的是Qt5.7版本的,请读者自主下载安装。

 

今天首先来进行Qt入门的第一个程序,也是很经典的一个例子。这是在很多的变成语言中都会用到的例子,就是输出helloworld这个信息。Qt中使用的变成语言是C++语言,如果读者对C++这变成语言不太熟悉的话,可以自行查看相应的C++学习教程,这里不多收。

具体的步骤:

  1. 创建Qt项目
  2. 在main.cpp中编写输出的内容
  3. #include "helloworld.h"
    #include <QApplication>
    
    #include <QDebug>  //添加这个头文件,用来输出信息
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    //    helloworld w;  //注释下面两条语句,我们直接在main中进行输出helloworld
    //    w.show();
    
        qDebug()<<"hello world";
    
        return a.exec();
    }

      

 

当然上述的例子只是一个简单的例子,很多人都会,因为在C语言或者C++中都在main函数中进行这个helloworld的输出,但是,现在采用一种Qt的C++方法进行输出。

请看例子:

   在项目的helloworl.cpp的构造函数中,添加以下代码

  

#include "helloworld.h"

#include <QDebug>  //添加这个头文件,用来输出信息

helloworld::helloworld(QWidget *parent)
    : QMainWindow(parent)
{
    qDebug() << "hello world";
}

helloworld::~helloworld()
{

}

  同时修改main.cpp中的main函数的内容,

#include "helloworld.h"
#include <QApplication>



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    helloworld w;
    //w.show();  /*注释这个话,不然的话,就会出现弹幕*/


    return a.exec();
}

  

同样输出的结果如下:

 

posted on 2017-02-15 21:19  Psrion  阅读(824)  评论(0编辑  收藏  举报

导航