QLabel not giving updated txt

混江龙づ霸主 提交于 2020-06-23 18:04:10

问题


I added a QLabel inside my widget and it is editable through UI, able to set new text also, but not able to retrieve the updated text using function text();

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

In UI i am able to edit to new text but by calling function

m_ColorName->text();

Its giving default txt _________

Code H file

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;

};

Source File

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();
}

回答1:


It seems that although the text is editable, the data is not updated in the text, that can be solved with a hack, we use findChild to find the QTextDocument that is created when the flags are activated and get the text of that element:

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

Example:

#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();
}

Output:

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


来源:https://stackoverflow.com/questions/48681580/qlabel-not-giving-updated-txt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!