2012-10-19 2 views
1

textedit의 첫 번째 출력은 숫자 3이며, 그 숫자가 Qt :: LogText의 형식이되는 이유는 모르겠다. 이 질문은 내가 물어 본 이전 질문을 기반으로합니다. 아래 링크에서 동일한 qdebugstream 헤더 파일을 사용하고 있습니다. qdebugstream 및 qtextedit을 사용하여 알 수없는 출력

Redirect std::cout to a QTextEdit

아래 새로운 프로젝트

는 텍스트 편집기로 cout을 리디렉션하는 QT GUI 응용 프로그램입니다. 또한 settextformat()은 더 이상 QTextEdit의 멤버가 아니므로 Qt :: LogText를 문자열로 변환했습니다.

이것은 다른 게시물을 기반으로했지만 해결책을 이해하지 못했습니다. QTextEdit::setTextFormat(Qt::LogText) does not exist anymore, what else can I use to log?. 누군가 더 자세한 정보를 제공 할 수 있습니까?

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 



MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 


    ui->textEdit->setReadOnly(true); 
    ui->textEdit->setText(QString("%1").arg(Qt::LogText)); 

    QDebugStream qout(std::cout, ui->textEdit); 

    cout << "Send this to the Text Edit!" << endl; 

} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

Mainwindow.h

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include "qdebugstream.h" 
#include "stdio.h" 
#include "iostream" 

using namespace std; 

namespace Ui { 
class MainWindow; 
} 

class QtextEdit; 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private: 
    Ui::MainWindow *ui; 
}; 

#endif // MAINWINDOW_H 

답변

2

당신은 Qt는 3 동등한 사용 Q3TextEdit (은 QTextEdit 것 같다 Qt는 4.x의에서 QPlainTextEdit을 사용해야합니다. x)를 텍스트 형식 Qt :: LogText로 변환합니다.

QDebugStream의 QPlainTextEdit 버전은 다음과 같을 수 있습니다 (append() mem func의 이름이 addPlainText()이며 이름이 비올라 임). 희망이 도움이됩니다.

#ifndef QDEBUGSTREAM_H 
#define QDEBUGSTREAM_H 

#include <iostream> 
#include <streambuf> 
#include <string> 
#include <QPlainTextEdit> 

class QDebugStream : public std::basic_streambuf<char> 
{ 
public: 
    QDebugStream(std::ostream &stream, QPlainTextEdit* text_edit) 
     : m_stream(stream) 
    { 
     log_window = text_edit; 
     m_old_buf = stream.rdbuf(); 
     stream.rdbuf(this); 
    } 
    ~QDebugStream() 
    { 
     // output anything that is left 
     if (!m_string.empty()) 
      log_window->appendPlainText(m_string.c_str()); 

     m_stream.rdbuf(m_old_buf); 
    } 

protected: 
    virtual int_type overflow(int_type v) 
    { 
     if (v == '\n') 
     { 
      log_window->appendPlainText(m_string.c_str()); 
      m_string.erase(m_string.begin(), m_string.end()); 
     } 
     else 
      m_string += v; 

     return v; 
    } 

    virtual std::streamsize xsputn(const char *p, std::streamsize n) 
    { 
     m_string.append(p, p + n); 

     int pos = 0; 
     while (pos != std::string::npos) 
     { 
      pos = m_string.find('\n'); 
      if (pos != std::string::npos) 
      { 
       std::string tmp(m_string.begin(), m_string.begin() + pos); 
       log_window->appendPlainText(tmp.c_str()); 
       m_string.erase(m_string.begin(), m_string.begin() + pos + 1); 
      } 
     } 

     return n; 
    } 

private: 
    std::ostream &m_stream; 
    std::streambuf *m_old_buf; 
    std::string m_string; 


    QPlainTextEdit* log_window; 
}; 

#endif