英文:
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't do it anyway. QMessageBox::information works, and qDebug() outputs the correct content, but it just doesn'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't work. If you save `Ui-> tempText` as a variable, calling setText from the mainwindow won't work either.I tried update() and repaint() , and it still didn't work. And there'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 &. Because I need to update "save data" dynamically, I write a timer. I can't describe how I felt when I found the solution.
Like this:
connect(timer,SIGNAL(timeout()),this,SLOT(setTandHLabel()));
That's it.
</details>
# 答案1
**得分**: 0
原则没有被理解,但问题已解决:在我的计时器connect()中使用SIGNAL/SLOT而不是&。当我找到解决方案时,我无法描述我的感受。
<details>
<summary>英文:</summary>
The principle was not understood, but the problem was solved:use SIGNAL/SLOT in my timer connect() instead &.I can't describe how I felt when I found the solution.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论