Update mainWindow / Reloop constructor
-
Hello guys,
i would like to update my mainWindow. My mainWindow searches through a directory and uses those file names for the creation of the buttons. My GUI also is able to create new Files in the descripted directory. If i close and open my GUI again, then the buttons are shown. But i would like to update the shown buttons, if a new file is added during the running of my GUI. The problem here is, that this process is only done once at the constructor of the class. So if im not wrong, myApp->update() does not work here.
Here is the my code:
This is the constructor of my mainWindow, which creates the amount of buttons in dependecy to the amount of files in the directory:
mainWindow::mainWindow(const QString savedSettingsHeader, const QString subjectSettingsHeader, const QString objectSettingsHeader) { window = new QVBoxLayout(this); layoutLoadButtons = new QHBoxLayout(); // Load Settings Buttons labelLoadButtons = new QLabel(savedSettingsHeader, this); layoutLoadButtons->addWidget(labelLoadButtons); savedSettingsNames = getSavedSettingsNames(); // Load as many buttons as files have already been saved for (int i = 0; i < savedSettingsNames.size(); i++) { QString buttonName = savedSettingsNames.at(i)[0].c_str(); myLoadButtons.append(new QPushButton(buttonName, this)); layoutLoadButtons->addWidget(myLoadButtons.at(i)); } window->addLayout(layoutLoadButtons); }
This is the function for creating a new file, which would mean also creating a new button:
void mainWindow::saveAsFile() { // Check if force value has been set string expText = qExpText.toStdString() + "-" + qSubText.toStdString() + "-" + currentDateTime(); saveSettings(expText, qObjText.toStdString(), savedForceSettings); }
What is the best way to solve this problem? I hope u guys can help me here
-
@LoveAndMercy If I understood your question correctly you need this: https://doc.qt.io/qt-6/qfilesystemwatcher.html
Connect a slot to https://doc.qt.io/qt-6/qfilesystemwatcher.html#directoryChanged signal and in that slot add a new button for the new file. -
Also, you can move the code that actually creates/adds the buttons to a separate class method and then call it from both the constructor and when you get a signal that new files were added. I am not sure what 'reloop constructor' means, but you do not want to try to call the constructor again.