问题描述:

I do have a question. I'm currently developing an app and this app is reporting the available space on a device.

The display is done in my Window class of my file mainwindow.cpp

void MainWindow::createInfoSection()

{

uint64_t space;

CreateInfoSection = new QWidget();

CreateInfoSection->setFixedHeight(40);

QGridLayout *CreateInfoLayout = new QGridLayout(CreateInfoSection);

NbOfItemsLabel = 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));

CreateInfoLayout->addWidget(NbOfItemsLabel, 0,0);

CreateInfoLayout->addWidget(SpaceAvailLabel, 0,1);

CreateInfoLayout->setAlignment(Qt::AlignCenter);

}

And I had a second method to just update the section when I want

void MainWindow::RefreshInfoSection()

{

uint64_t space;

NbOfItemsLabel = 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));

}

the space available is get from a method declared in another class that I'm accessing using m_device. getAvailableFreeSpaceInBytes is defined as below in my mtp_wrapper class (in mtp_wrapper.cpp)

uint64_t mtp_wrapper::getAvailableFreeSpaceInBytes() {

return PulsDeviceMngr->device->storage->FreeSpaceInBytes;

}

The PulsDeviceMngr is a structure which contain multiple elements.

What I'm looking for is a way to detect the change/update the value of FreeSpaceInBytes and update the section defined in my MainWindow using RefreshInfoSection as RefreshInfoSection due have a way to update the section and QLabel.

I'm pretty sure that it can be done by CONNECT/SLOT/SIGNAL

Any idea/examples.

网友答案:

What is m_device? It is the responsibily of m_device to notify others of changes. If it is possible, add a signal ChangeInBytes to m_device and then connect interested parties to that signal using roughly the following:

connect(SIGNAL(m_device->ChangeInBytes), SLOT(interested_party->slot))

If you can't modify m_device, you should probably create a wrapper-class, checking for changes in an appropriate way.

网友答案:

You cannot monitor a variable, you need either a setter for the variable, that as suggested, raise a signal, or some sort of pooling on the variable itself to detect changes but this may be overkill.

相关阅读:
Top