【问题标题】:QLabel not giving updated txtQLabel 没有提供更新的 txt
【发布时间】:2018-07-18 19:01:06
【问题描述】:

我在我的小部件中添加了一个 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();
}

【问题讨论】:

  • 没有上下文。添加mcve
  • 我在您的代码中没有看到任何 setText 调用来修改标签中的文本。
  • @DmitrySazonov 激活标志Qt::TextEditable时,用户可以通过GUI直接编辑QLabel文本,不必使用setText()
  • 为什么不直接使用QLineEditQPlainTextEdit

标签: c++ qt qt5 qt4 qlabel


【解决方案1】:

似乎虽然文本是可编辑的,但文本中的数据没有更新,这可以通过 hack 解决,我们使用 findChild 查找激活标志时创建的 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___"
[...]

【讨论】:

  • toRawText() 不在 QTextDocument QTextDocument *td = m_ColorName->findChild(); if(td){ qDebug()toPlainText() }
  • @Sijith doc.qt.io/qt-5/qtextdocument.html#toRawText,你有什么版本的Qt?你也可以使用toPlainText()。
  • @Sijith 似乎 toRawText() 是在 Qt 5.9 中引入的,而您的版本低于此。
  • QT 5.4.1 但您提供的代码 sn-p 没有任何效果。我正在使用指针,看起来修改后的值不能被指针访问
  • @Sijith 我想念,它对我有用。试试我的代码示例,显然将 toRawText() 更改为 toPlainText()。
猜你喜欢
  • 1970-01-01
  • 2018-03-26
  • 2021-05-25
  • 2013-10-21
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多