Qt学习笔记之Hello World

用Qt的第一个程序必然是怎样输出Hello World

1、在命令行中显示Hello World

首先建立一个控制台应用程序,其他都是默认设置balabala。。。

新建完成后,在main.cpp中添加显示输出

具体代码如下:

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "hello world!";
    return a.exec();
}
代码中添加了这两行

显示输出Hello World!

这里遇到一个小问题,如下::-1: error: cannot open output file debug\hello.exe: Permission denied


原因是当前运行的程序还在后台运行,没有terminate,所以要打开任务管理器 去结束进程。


代码稍作改动,可以如下表示:

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString myStr = "hello world!";
    qDebug() << myStr;
    return a.exec();
}
当然了,输出结果多了一对双引号

原因是 引号没有被加进QString,引号是你用qDebug打印的时候由qDebug添加的
qDebug打印的时候,<<""这么写就是输出引号内的内容,也就是字符串,<<str这样输出字符串变量的时候是加上引号的,为了区分这两种情况


还有这种情况

用QLabel

#include <QCoreApplication>
#include <QLabel>
#include <QApplication>
int main(int argc, char *argv[])
{
    //QCoreApplication a(argc, argv);
    QApplication a(argc,argv);
    QLabel *label = new QLabel("Hello World!");
    label->show();
    return a.exec();
}
这里要添加上面彩色标注代码

显示结果为创建对话框的Hello World!

HMTL源码形式使字体换色,具体如下

#include <QCoreApplication>
#include <QLabel>
#include <QApplication>
int main(int argc, char *argv[])
{
    //QCoreApplication a(argc, argv);
    QApplication a(argc,argv);
    QLabel *label = new QLabel("<h2><i>Hello</i>""<font color = red>Qt!</font></h2>");
    label->show();
    return a.exec();
}
结果:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值