牛骨文教育服务平台(让学习变的简单)
博文笔记

Qt标准输入输出问题

创建时间:2015-10-30 投稿人: 浏览次数:5987

序:

在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 !
");

    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 !
");
    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 !
");
    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 !
");
    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 !
");
    cout<<"<<hello world !"<<endl;
    qDebug()<<"hello world !";

    return a.exec();
}


打印结果:

opencv应用:

需要注意的是QTextStream和qdebug不能直接打印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 !
");
    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++)
{

}









声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。