简易的QT输出调试器2
我们升级一下调试器,让它变成单例模式,所有调试都打印在一个窗口上:
#pragma once
#ifndef _DEBUGWIDGET_H__
#define _DEBUGWIDGET_H__
#include <QWidget>
#include <qtextedit.h>
#include <qlayout.h>
class DebugWidget : public QWidget {
Q_OBJECT
public:
~DebugWidget();
void addContents(const QString& s1);
static DebugWidget* getDebugWidget();
private:
QTextEdit *ShowDebugArea;
QHBoxLayout *qlayout;
DebugWidget(QWidget * parent = Q_NULLPTR);
};
extern DebugWidget *dw;
#endif
#include "debugwidget.hpp"
#include <iostream>
#include <vector>
#include <string>
#include <ostream>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::streambuf;
using std::string;
using std::vector;
static DebugWidget *dw = NULL;
DebugWidget::DebugWidget(QWidget * parent) : QWidget(parent) {
setWindowFlags(Qt::WindowStaysOnTopHint);
resize(1100,800);
qlayout = new QHBoxLayout(this);
ShowDebugArea = new QTextEdit(this);
ShowDebugArea->setFontPointSize(20);
qlayout->setAlignment(Qt::AlignCenter);
qlayout->addWidget(ShowDebugArea);
//ShowDebugArea->set
setAttribute(Qt::WA_DeleteOnClose, true);
show();
}
DebugWidget::~DebugWidget() {
}
void DebugWidget::addContents(const QString& s1)
{
ShowDebugArea->append(s1);
}
DebugWidget* DebugWidget::getDebugWidget() {
if(dw == NULL)
dw = new DebugWidget;
return dw;
}
大部分内容和上一个调试器相同,只是在头文件中私有了构造函数,并在构造了静态的对象,在使用时初始化。
注意一开始一定要用static DebugWidget *dw = NULL;,如果不等于NULL,则还没有进行初始化,初始值可能为任何值,这时候如果判断第一次是不是为NULL就会出错:
if(dw == NULL)
dw = new DebugWidget;
return dw;
会直接返回没有任何实体对象的指针。