In Qt C++, QLabel::setText 不起作用,但其他功能可以使用。

huangapple go评论116阅读模式
英文:

In Qt c++, QLabel::setText does not work ,but other fun can work

问题

我想在标签中显示指定的内容(Client::saveData,一个静态变量),但无论如何我都做不到。QMessageBox::information可以工作,qDebug()可以输出正确的内容,但它就是不显示在标签上。

```cpp
// network.h
public:
    static QByteArray saveData;

// network.cpp
QByteArray Client::saveData = "TEST";

void S8524Widget::setTandHLabel()
{
    QString tempdata(Client::saveData.left(2));
    ui->tempText->setText(tempdata);
}

Client::saveData正确打印。如果我将setText放在构造函数中,它可以正常工作,但我需要动态更新文本。在这个函数中,甚至setText("Tempdata")都不起作用。如果将ui->tempText保存为一个变量,在主窗口中调用setText也不起作用。我尝试过update()和repaint(),但仍然不起作用。并且没有其他代码控制这个标签。

我创建了一个新项目并尝试复制它,但在这个项目中成功实现了它。

// network.h
public:
    static QByteArray saveData;

// network.cpp
QByteArray udpNetwork::saveData = "TEST";

// mainwindow.cpp
void MainWindow::setdata()
{
    ui->label->setText(QString(udpNetwork::saveData.right(2)));
}

原理不太明白,但问题解决了:在我的定时器connect()中使用SIGNAL/SLOT而不是&。因为我需要动态更新“保存数据”,所以我写了一个定时器。当我找到解决方案时,我无法描述我有多么兴奋。

像这样:

connect(timer, SIGNAL(timeout()), this, SLOT(setTandHLabel()));

就是这样。


<details>
<summary>英文:</summary>

I wanted to display the specified content in the label (Client::saveData, a static variable),but I couldn&#39;t do it anyway. QMessageBox::information works, and qDebug() outputs the correct content, but it just doesn&#39;t show up on the label.

//network.h
public:
static QByteArray saveData;

//network.cpp
QByteArray Client::saveData = "TEST";

void S8524Widget::setTandHLabel()
{
QString tempdata(Client::saveData.left(2));
ui->tempText->setText(tempdata);
}




Client::saveData prints correctly. If I put settext in the constructor, it works fine, but I need it to update the text dynamically. In this function, even `setText (“Tempdata”)` doesn&#39;t work. If you save `Ui-&gt; tempText` as a variable, calling setText from the mainwindow won&#39;t work either.I tried update() and repaint() , and it still didn&#39;t work. And there&#39;s no other code that controls this label.



I created a new project and tried to replicate it, but it was successfully implemented in this project.



//network.h
public:
static QByteArray saveData;

//network.cpp
QByteArray udpNetwork::saveData = "TEST";

//mainwoindow.cpp
void MainWindow::setdata()
{
ui->label->setText(QString(udpNetwork::saveData.right(2)));
}


The principle was not understood, but the problem was solved: use SIGNAL/SLOT in my timer connect() instead &amp;. Because I need to update &quot;save data&quot; dynamically, I write a timer. I can&#39;t describe how I felt when I found the solution. 

Like this:

    connect(timer,SIGNAL(timeout()),this,SLOT(setTandHLabel()));

That&#39;s it.

</details>


# 答案1
**得分**: 0

原则没有被理解,但问题已解决:在我的计时器connect()中使用SIGNAL/SLOT而不是&amp;。当我找到解决方案时,我无法描述我的感受。

<details>
<summary>英文:</summary>

The principle was not understood, but the problem was solved:use SIGNAL/SLOT in my timer connect() instead &amp;.I can&#39;t describe how I felt when I found the solution.

</details>



huangapple
  • 本文由 发表于 2023年6月29日 15:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定