Qt标准输入输出问题

序:

在Qt应用编程的时候,常常通过打印信息来调试程序。一般在Qt中使用qdebug();非常方便,既可以当成c++里面的cout来用,也可以当成printf();来用,而且自动换行。虽然qdebug有如此多的好处,但是有时候还是需要调用标准输入输出函数和流在终端来显示信息,比如在调试图像程序的时候,有时候希望把一些图像矩阵打印出来,这时候用qdebug就非常的不方便,在VC的win32程序中可以直接调用printf();或者cout在终端打印矩阵信息,在Qt中也一样是可以的。

Qt控制台程序:

新建一个Qt控制台的程序


然后通过printf在终端打印一个hello world !

#include <QtCore/QCoreApplication>
#include <stdio.h>

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

    printf("hello world !\r\n");

    return a.exec();
}
运行结果:


说明在Qt控制台程序中printf跟win32一样可以直接使用。

那么cout呢?在win32中添加#include  <iostream.h>在后再程序中添加cout<<"hello world !"<<endl;就可以打印出hello world !了。

在Qt中是不行的,编译时候会报错,(error: 'cout' was not declared in this scope),这是因为Qt使用的是GNU的c++库,标准输入输出流被定义在了std的命名空间中,在使用的时候必须加上std::或者在开头加上usingnamespacestd;如下所示:

#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <iostream>

using namespace std;

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

    printf("hello world !\r\n");
    cout<<"<<hello world !"<<endl;

    return a.exec();
}
或者:
#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <iostream>

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

    printf("hello world !\r\n");
    std::cout<<"<<hello world !"<<std::endl;

    return a.exec();
}
打印结果都是:


还有一种方法就是使用
QTextStream类来打印信息。如下所示:

#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <QTextStream>

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

    QTextStream cout(stdout);
    printf("hello world !\r\n");
    cout<<"<<hello world !"<<endl;

    return a.exec();
}
打印结果跟上面的一致。

当然qdebug还是可以用的,并且依然是可以自动换行的,如下所示:

#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <QTextStream>
#include <QDebug>

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

    QTextStream cout(stdout);
    printf("hello world !\r\n");
    cout<<"<<hello world !"<<endl;
    qDebug()<<"hello world !";

    return a.exec();
}

 打印结果: 

opencv应用:

需要注意的是QTextStreamqdebug不能直接打印Mat矩阵,所以只能用标准输出流cout来打印。并且在包含了opencv的头文件后,不需要再加using namespace std;就可以直接用cout 。如下所示:

#include <QtCore/QCoreApplication>
#include <stdio.h>
//#include <QTextStream>
#include <QDebug>
#include <opencv2/opencv.hpp>

using namespace cv;

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

    Mat srcIMG=imread("liying.jpg");

    //QTextStream cout(stdout);//不能用来打印Mat矩阵
    printf("hello world !\r\n");
    cout<<"<<hello world !"<<endl;
    qDebug()<<"hello world !";//不能用来打印Mat矩阵

    Mat roi(srcIMG,Rect(0,0,5,5));//定义一个ROI区域
    cout<<roi<<endl;//打印ROI区域

    imshow("liling",srcIMG);
    waitKey(0);

    return a.exec();
}
运行结果:

定义的ROI为5*5,图像为BGR3通道,所以打印出的矩阵大小为:5*15


**********最后在移植win32到Qt的时候常见的一个语法不兼容的如下***************

for(int i=0;i<100;i++){

}
for(i=0;i<100;i++)
{

}
这样的代码,在VC的win32里是没问题的,但是在Qt中编译时会报错!Qt中应该把int i 定义在外面,或者在两个for中分别定义如下:

for(int i=0;i<100;i++){

}
for(int i=0;i<100;i++)
{

}









评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶落西湘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值