QT中进行debug输出和使用cout cin等的问题

来源:互联网 发布:笔记本声音端口号 编辑:程序博客网 时间:2024/05/30 04:53

在qt中大家都知道不能像纯C++那样使用cout等进行debug, 因为输出的内容会保留到程序退出的时候才进行输出。

 

Warning and Debugging Messages

Qt includes four global functions for writing out warning and debug text. You can use them for the following purposes:

Qt中包含了四个全局方法, 用于输出警告和调试信息, 列举如下:

  • qDebug () is used for writing custom debug output.用于输出调试信息
  • qWarning () is used to report warnings and recoverable errors in your application.警告信息
  • qCritical () is used for writing critical error mesages and reporting system errors.严重错误信息
  • qFatal () is used for writing fatal error messages shortly before exiting.很严重的错误, 输出后退出程序

If you include the <QtDebug> header file, the qDebug() function can also be used as an output stream. For example:

如果你include了<QtDebug>头文件, 上述的方法都可以当输出流进行使用, 例如:

 qDebug() << "Widget" << widget << "at position" << widget->pos();

 

这时可以把上面一条语句添加到定义的主窗口的不同位置进行调试。

 

如果想要使用cout, 应使用QTextStream重载

Cpp代码  收藏代码
  1. #include <QApplication>    
  2. #include <QTextStream>    
  3.     
  4. int main(int argc, char *argv[])    
  5. {    
  6.     QApplication app(argc, argv);    
  7.     
  8.     QTextStream out(stdout);  //重载  
  9.     
  10.     out << "is QTextStream out " << endl;    
  11.     
  12.     return app.exec();    
  13. }  


另外一种解释,这个要清晰一点:

QSignalMapper我们可以将它理解为一个信号翻译器或者说是哥信号转发器。它的主要应用在于可以实现一个函数相应不同按钮的实现功能。比如我们 实现一个计算器的时候,很多不同的按钮都有着差不多的功能,数字0~9的按钮,功能都是输入数字,只是输入的数值不一样而已,如果不使用 QSignalMapper,我们不得不写10个函数作为各自按钮的响应事件。这里我就通过这个简单的实例讲解下QSignalMapper的用法。
    类内的成员:
    QPushButton *numButt[10];
    QLabel *info;
    QLineEdit *inputEdit;
    QHBoxLayout *hLay;
    QVBoxLayout *vLay;
    QGridLayout *gLay;
    QSignalMapper *sigMap;


myinput::myinput(QWidget *parent) :
    QWidget(parent)
{
    hLay = new QHBoxLayout;
    vLay = new QVBoxLayout;
    gLay = new QGridLayout;
    vLay->addLayout(hLay);
    vLay->addLayout(gLay);
    info = new QLabel(tr("the power: "), this);
    inputEdit = new QLineEdit;
   
    hLay->addWidget(info);
    hLay->addWidget(inputEdit);


   
    sigMap = new QSignalMapper(this); //创建QSignalMapper对象
    for (int i = 0; i < 10; ++i) {
        numButt[i] = new QPushButton(tr(QString::number(i).toStdString().c_str()), this);
        //创建0~9按钮对象,按钮显示的text内容就是数值0~9
        gLay->addWidget(numButt[i], i/4, i%4);  //加入布局
        connect(numButt[i], SIGNAL(clicked()), sigMap, SLOT(map())); 
        //将原始信号传递给QSignalMapper对象
        sigMap->setMapping(numButt[i], i);
        //设置转发规则,转发为参数为int型的信号,并将i作为实参传递
    }


    connect(sigMap, SIGNAL(mapped(int)), this, SLOT(sl_push(int))); 
    //将转发信号连接到对应的槽函数
    this->setLayout(vLay);
}
0 0
原创粉丝点击