通过 QTextStream 流式传输到 QTextEdit

发布于 2024-08-22 21:22:42 字数 357 浏览 5 评论 0原文

我经常想使用 QTextEdit 作为显示正在编写的内容的快速方法 到一条溪流。也就是说,我想要的不是写入 QTextStream out(stdout) 做类似的事情:

 QTextEdit qte; 
 QTextStream out(qte);  

如果我在写入 QTextStream 后发出信号,我可以做类似的事情 附加到 QString。
问题是我希望界面是 与我流式传输到 stdout 等相同:

out << some data << endl;

关于如何实现此目的有什么想法吗?

提前致谢。

I have often wanted to use QTextEdit as a quick means of displaying what is being written
to a stream. That is, rather than writing to QTextStream out(stdout), I want
to do something like:

 QTextEdit qte; 
 QTextStream out(qte);  

I could do something similar if I emit a signal after writing to a QTextStream
attached to a QString.
The problem is that I want the interface to be the
same as it would if I were streaming to stdout etc.:

out << some data << endl;

Any ideas on how I might accomplish this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-08-29 21:22:42

您可以创建一个输出到 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 ;
// ...

You can create a QIODevice that outputs to QTextEdit.

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 ;
// ...
栖竹 2024-08-29 21:22:42

您可以子类化QTextEdit并实现<<运算符以赋予其您想要的行为;像这样的东西:

class TextEdit : public QTextEdit {
    .../...
    TextEdit & operator<< (QString const &str) {
        append(str);

        return *this;
    }
};

You can subclass the QTextEdit and implement the << operator to give it the behaviour you want ; something like:

class TextEdit : public QTextEdit {
    .../...
    TextEdit & operator<< (QString const &str) {
        append(str);

        return *this;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文