QLabel 没有提供更新的 txt

QLabel not giving updated txt

我在我的小部件中添加了一个 QLabel,它可以通过 UI 进行编辑,也可以设置新文本,但无法使用函数 text();

检索更新的文本
 QLabel *m_ColorName = new QLabel("_________");
m_ColorName->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);

在 UI 中,我可以编辑新文本,但需要调用函数

m_ColorName->text();

它给出默认的 txt _________

代码 H档

class CascadeColorHighlightWidgetItem : public QWidget
{
    Q_OBJECT
public:
    CascadeColorHighlightWidgetItem(int jobID, QColor selectedColor, QWidget *parent = 0);

    int getJobID();
    QString getSelectedColor();
    QString getColorText();

private:
    QString m_titleName;
    QRectF m_textRect;
    QVBoxLayout *m_mainLyt;

    QLineEdit *m_pTitleEditor;
    QLabel *m_ColorName;
    QColor m_SelectedColor;

};

源文件

CascadeColorHighlightWidgetItem::CascadeColorHighlightWidgetItem(QColor selectedColor, QWidget *parent)
    : QWidget(parent),m_titleName("______"),m_SelectedColor(selectedColor)
{
    setFixedHeight(40);
    setContentsMargins(0,0,0,0);
    setFocusPolicy(Qt::StrongFocus);

    m_pTitleEditor = new QLineEdit();

    m_ColorName = new QLabel("_________");
    m_ColorName->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);

    QFont font( "Calibri" );
    font.setPixelSize(14);
    m_ColorName->setFont(font);

    QPixmap pixmap;
    pixmap.fill(QColor("transparent"));
    QWidget *pixMap = new QWidget;
    pixMap->resize(100,100);

    QString styl = QString("background-color: rgb(%1, %2, %3)").arg(QString::number(m_SelectedColor.red()),
                                         QString::number(m_SelectedColor.green()),
                                         QString::number(m_SelectedColor.blue()));
    pixMap->setStyleSheet(styl);

    m_ColorToStringMap.insert(m_ColorName->text(),m_SelectedColor);


    QHBoxLayout * mnLyt = new QHBoxLayout(this);
    mnLyt->addWidget(pixMap);
    mnLyt->addWidget(m_ColorName);
    mnLyt->addSpacerItem(new QSpacerItem(30, 0, QSizePolicy::Minimum, QSizePolicy::Minimum));


    int width = pixMap->width();
    int height = pixMap->height();

    int side = qMin(width, height);
    QRegion maskedRegion(width/2- side/2, height/2- side/2, 20,
                    20, QRegion::Ellipse);
    pixMap->setMask(maskedRegion);
}

QString CascadeColorHighlightWidgetItem::getColorText()
{
    qDebug() << "!!!CascadeColorHighlightWidgetItem::getColorText" << m_ColorName->text(); // returns "_________"
    return m_ColorName->text();
}

看来虽然文本是可编辑的,但是文本中的数据没有更新,可以通过hack来解决,我们使用findChild找到flags激活时创建的QTextDocument,得到文本该元素的:

QTextDocument *td = m_ColorName->findChild<QTextDocument *>();
if(td){
    QString text = td->toRawText();  // >= Qt 5.9
    // td->toPlainText();
    // td->toHtml();
} 

示例:

#include <QApplication>
#include <QLabel>
#include <QTextDocument>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>

#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget widget;
    QLabel label("_________");
    label.setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextEditable);
    widget.setLayout(new QVBoxLayout);
    widget.layout()->addWidget(&label);

    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, [&label](){
        QTextDocument *td = label.findChild<QTextDocument*>();
        if(td){
            qDebug()<<td->toRawText();
            //qDebug()<<td->toPlainText();
            //qDebug()<<td->toHtml();
        }
    });
    timer.start(100);
    widget.show();
    return a.exec();
}

输出:

[...]
"________"
"________"
"_______"
"____f___"
"____f___"
"____ff___"
"____fff___"
"____fff___"
"____ffff___"
"____fffff___"
"____fffff___"
[...]