2012-04-25 8 views
3

Qtアプリケーション開発が初めてで、私はあなたの助けに感謝します。これは私がQTextEditstd::coutをリダイレクトしようとしていると私は見て、次のリンクで提供されている例をテストしようとした redirect std::cout to QTextEditstd :: coutをQTextEditにリダイレクト

の再投稿です。

参考リンク1:リファレンスリンク1.

untiled1.pro

SOURCES += \ 
main.cpp 
HEADERS += \ 
qdebugstream.h 

qdebugstream.h

#ifndef QDEBUGSTREAM_H 
#define QDEBUGSTREAM_H 

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

#include "qtextedit.h" 

class QDebugStream : public std::basic_streambuf<char> 
{ 
public: 
QDebugStream(std::ostream &stream, QTextEdit* 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->append(m_string.c_str()); 

    m_stream.rdbuf(m_old_buf); 
} 

protected: 
virtual int_type overflow(int_type v) 
{ 
    if (v == '\n') 
    { 
    log_window->append(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->append(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; 


    QTextEdit* log_window; 
    }; 

#endif 
の例をテストするためのQt Creatorの2.4.1を使用し http://lists.trolltech.com/qt-interest/2005-06/thread00166-0.html

main.cpp

#include "qdebugstream.h" 
#include "qtextedit.h" 
#include <QtGui> 

int main(int argc, char **argv) 
{ 
    QApplication application(argc, argv); 
     application.connect(&application, SIGNAL(lastWindowClosed()), 
    &application, SLOT(quit())); 

     QMainWindow* mainWindow = new QMainWindow(); 

     QTextEdit* myTextEdit = new QTextEdit(mainWindow, "myTextEdit"); 
     myTextEdit->setTextFormat(Qt::LogText); 
     QDebugStream qout(std::cout, myTextEdit); 

     mainWindow->show(); 
     std::cout << "Send this to the Text Edit!" << std::endl; 

     return application.exec(); 
} 

私は次のエラーメッセージが出ます:

C:\Documents and Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug..\untitled1\main.cpp:13: error: C2664: 'QTextEdit::QTextEdit(const QString &,QWidget *)' : cannot convert parameter 1 from 'QMainWindow *' to 'const QString &' Reason: cannot convert from 'QMainWindow *' to 'const QString' No constructor could take the source type, or constructor overload resolution was ambiguous

C:\Documents and Settings\Administrator\untitled1-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Debug..\untitled1\main.cpp:14: error: C2039: 'setTextFormat' : is not a member of 'QTextEdit'

c:\qtsdk\desktop\qt\4.8.1\msvc2010\include\qtgui\qtextedit.h:70: see declaration of 'QTextEdit'

答えて

1

documentationを見ると、QTextEdit

を変更してみてください2つのコンストラクタ、

QTextEdit::QTextEdit (QWidget * parent = 0) 

QTextEdit::QTextEdit (const QString & text, QWidget * parent = 0) 

を持っています

QTextEdit* myTextEdit = new QTextEdit(mainWindow, "myTextEdit"); 

古いQT3のAPIを使用する場所がまだあるので、これは、すべての問題を解決しません

QTextEdit* myTextEdit = new QTextEdit(mainWindow); 

へ。必要に応じて変更する必要があります。

+0

ありがとうございました。私はそれを試すことができたし、それは動作します。 –

+0

@NinjaJoey問題ありません。あなたが新しいので、回答を受け入れる方法について簡単に説明します:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –

関連する問題