2

I have created an app which is used to display folders and files in c++ and 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:

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.

Any idea ?

Seb
  • 2,929
  • 4
  • 30
  • 73
  • 2
    I don't think you should be new'ing the labels again, instead you should be able to do something like *NbOfFolderLabel->setText(...);* Which would mean making them members of your class. – James Adkison Jan 31 '15 at 03:18
  • 1
    @seb Are you able to see the NbOfFolderLabel and other label text after RefreshInfoSection() ?? – Balu Jan 31 '15 at 05:44

1 Answers1

0

Using QLabel->update() is fine, but you should also run 'processEvents()' to enforce these updates. Can you run qApp->processEvents(); or emit a signal to it?

See QT Repaint/Redraw/Update/Do Something! and maybe have a look at 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!

Community
  • 1
  • 1
Wim
  • 507
  • 1
  • 8
  • 16