您可以創建一個輸出到QTextEdit的QIODevice。
class TextEditIoDevice : public QIODevice
{
Q_OBJECT
public:
TextEditIoDevice(QTextEdit *const textEdit, QObject *const parent)
: QIODevice(parent)
, textEdit(textEdit)
{
open(QIODevice::WriteOnly|QIODevice::Text);
}
//...
protected:
qint64 readData(char *data, qint64 maxSize) { return 0; }
qint64 writeData(const char *data, qint64 maxSize)
{
if(textEdit)
{
textEdit->append(data);
}
return maxSize;
}
private:
QPointer<QTextEdit> textEdit;
};
// In some dialogs constructor
QTextStream ss(new TextEditIoDevice(*ui.textEdit, this));
ss << "Print formatted text " <<hex << 12 ;
// ...
不錯。我也試圖這樣做,但我花了整整一天的時間試圖讓操作符鏈工作沒有任何效果。謝謝 – Moomin 2010-02-28 16:23:54
但是,仍然無法使用'QTextStream'選項(例如輸出格式) – Moomin 2010-02-28 16:31:48