[英]Refresh QLabel in a QWidget
I have created an app which is used to display folders and files in c++ and Qt. 我创建了一个应用程序,用于在c ++和Qt中显示文件夹和文件。
I have added an info section which is supposed to display the number of files and folders. 我添加了一个信息部分,该部分应显示文件和文件夹的数量。
To do saw, I have created a layout and widgets as below: 为了看到,我创建了一个布局和小部件,如下所示:
void MainWindow::createInfoSection()
{
uint64_t space;
CreateInfoSection = new QWidget();
CreateInfoSection->setFixedHeight(40);
//QGridLayout *CreateInfoLayout = new QGridLayout(CreateInfoSection);
CreateInfoLayout = new QGridLayout(CreateInfoSection);
NbOfFolderLabel = new QLabel(tr("%1 Folders").arg(m_device.getNbOfFolder()));
NbOfFilesLabel = new QLabel(tr("%1 Files").arg(m_device.getNbOfFiles()));
space = m_device.getAvailableFreeSpaceInBytes();
if(space < MEGABYTE)
SpaceAvailLabel = new QLabel(tr("%1 KB Remaining").arg(space/KILOBYTE));
else if (space < GIGABYTE)
SpaceAvailLabel = new QLabel(tr("%1 MB Remaining").arg(space/MEGABYTE));
else
SpaceAvailLabel = new QLabel(tr("%1 GB Remaining").arg(space/GIGABYTE));
NbOfFolderLabel->updatesEnabled();
NbOfFilesLabel->updatesEnabled();
SpaceAvailLabel->updatesEnabled();
CreateInfoLayout->addWidget(NbOfFolderLabel, 0,0);
CreateInfoLayout->addWidget(NbOfFilesLabel, 0,1);
CreateInfoLayout->addWidget(SpaceAvailLabel, 0,2);
CreateInfoLayout->setAlignment(Qt::AlignCenter);
}
And when an action is done such as adding a Folder, I'm calling a Refresh method as below: 当完成添加文件夹之类的操作后,我将调用Refresh方法,如下所示:
void MainWindow::RefreshInfoSection()
{
uint64_t space;
NbOfFolderLabel = new QLabel(tr("%1 Files").arg(m_device.getNbOfFolder()));
NbOfFilesLabel = new QLabel(tr("%1 Files").arg(m_device.getNbOfFiles()));
space = m_device.getAvailableFreeSpaceInBytes();
if(space < MEGABYTE)
SpaceAvailLabel = new QLabel(tr("%1 KB Available").arg(space/KILOBYTE));
else if (space < GIGABYTE)
SpaceAvailLabel = new QLabel(tr("%1 MB Available").arg(space/MEGABYTE));
else
SpaceAvailLabel = new QLabel(tr("%1 GB Available").arg(space/GIGABYTE));
NbOfFolderLabel->update();
NbOfFilesLabel->update();
SpaceAvailLabel->update();
CreateInfoLayout->update();
}
But it never works even if the 3 labels are updated. 但是,即使更新了3个标签,它也永远不会起作用。
Any idea ? 任何想法 ?
Using QLabel->update() is fine, but you should also run 'processEvents()' to enforce these updates. 使用QLabel-> update()很好,但是您也应该运行'processEvents()'来实施这些更新。 Can you run qApp->processEvents();
你可以运行qApp->processEvents();
or emit a signal to it? 或发出信号吗?
See QT Repaint/Redraw/Update/Do Something! 请参阅QT重涂/重绘/更新/执行某些操作! and maybe have a look at http://wiki.qt.io/Progress-bar . 也许看看http://wiki.qt.io/Progress-bar 。
Me too, I am currently just learning about this, so I might make a mistake, but please let me know how it is going? 我也是,我目前只是在学习这件事,所以我可能会犯一个错误,但是请让我知道这是怎么回事?
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.