2

tl;博士:

QPlainTextEdit::appendPlainText(QString)将换行符附加到我的文本小部件。 QPlainTextEdit::insertPlainText(QString) 似乎不受setCurrentCharFormat().

QTextCharFormat 有没有办法在不插入换行符的情况下在收听当前时附加文本

细节

我有一个“终端”样式的小部件,它从子进程的标准输出中获取文本并将其显示在QPlainTextEdit.

在我有颜色内容之前,我可以简单地这样做:

void ProcessWidget::appendText(QString text)
{
    m_textedit->appendPlainText(text);
}

'\033'颜色使用转义字符后跟颜色出现在文本中。我可以检测颜色并适当地设置调色板:

void ProcessWidget::appendText(QString text)
{
    Qt::GlobalColor colour = GetColour(text);

    QTextCharFormat tf = m_textedit->currentCharFormat();
    tf.setForeground(QBrush(colour));
    m_textedit->setCurrentCharFormat(tf);

    m_textedit->appendPlainText(text);
}

如果每行只有一种颜色,则此方法有效,但如果我的颜色在每行中途发生变化,那么我需要更疯狂一点:

std::map<QString, Qt::GlobalColor>   m_colours;
QPlainTextEdit*                      m_textedit;
...

void ProcessWidget::AppendText(QString text)
{
    while(true)
    {
        int iColour = text.indexOf('\033');

        if (iColour == -1)
            break;

        QString pretext = text.mid(0, iColour);

        if (!pretext.isEmpty())
        {
            m_textedit->appendPlainText(pretext);
        }

        text.remove(0, iColour);

        for (auto pair : m_colours)
        {
            if ( text.startsWith(pair.first) )
            {
                QTextCharFormat tf = m_textedit->currentCharFormat();
                tf.setForeground(QBrush(pair.second));
                m_textedit->setCurrentCharFormat(tf);
                text.remove(0, pair.first.size());
                break;
            }
        }
    }

    if (!text.isEmpty())
    {
        m_textedit->appendPlainText(text);
    }
}

但是,因为我使用appendPlainText(),所以找到的每种新颜色都会给我一个新行。

我尝试替换appendPlainText()为:

m_textedit->moveCursor (QTextCursor::End);
m_textedit->insertPlainText(text);
m_textedit->moveCursor (QTextCursor::End);

然后'\n'在最后添加。但在那种情况下,我不再有任何颜色了。我也试过appendHtml(),但这似乎没有什么不同。

4

1 回答 1

3

对于这些情况,最简单的方法是使用 HTML 并插入标签:<font color = "..."> </ font>

例子:

#include <QApplication>
#include <QDateTime>
#include <QPlainTextEdit>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>

const std::map<QString, QColor> m_colours {{"red", QColor(Qt::red)},
                                           {"blue", QColor(Qt::blue)},
                                           {"green", QColor(Qt::green)}
                                          };

class ProcessWidget: public QWidget{
    Q_OBJECT
public:
    ProcessWidget(QWidget *parent=nullptr):
        QWidget(parent),
        lay{this}
    {
        m_textedit.setReadOnly(true);
        lay.addWidget(&m_textedit);
    }
public slots:
    void appendText(const QString & text){
        QString html{text};
        int j = 0;
        bool start = true;
        QString textColor;

        while ((j = html.indexOf(QChar('\033'), j)) != -1) {
            html.remove(j, 1);
            QColor color;
            for(auto & pair : m_colours){
                if(html.mid(j).startsWith(pair.first)){
                    color = pair.second;
                    html.remove(j, pair.first.length());
                }
            }
            if(start){
                textColor = QString("<font color=\"%1\">").arg(color.name());
                start = false;
            }
            else
                textColor = QString("</font><font color=\"%1\">").arg(color.name());
            html.insert(j, textColor);
            j += 1+textColor.length();
        }
        html.append("</font>");
        m_textedit.appendHtml(html);
    }
private:
    QPlainTextEdit m_textedit;
    QVBoxLayout lay;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    ProcessWidget w;
    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, [&w](){
        QString text = QString("\033redDateTime: %1 \033blueDate:%2 \033greenTime:%3")
                .arg(QDateTime::currentDateTime().toString())
                .arg(QDate().currentDate().toString())
                .arg(QTime::currentTime().toString());
        w.appendText(text);
    });
    timer.start(1000);
    w.show();
    return a.exec();
}

#include "main.moc"

在此处输入图像描述

于 2018-08-29T11:10:39.403 回答