Accessing Field in Close Event of QWizard
-
I have created a multi-page QWizard that exploits the feature of fields. On the entry page, I register a field "Location" based on a QLineEdit that stores a directory location. I would like to store and retrieve this with QSettings. To do so, I initialize the "Location" QLineEdit with the value given by QSettings::value. To store the latest changes to the "Location" that are made when visiting various pages of the QWizard, I update the "Location" field by means of QWizard::setField. All this works perfectly.
Now, when I exist the QWizard, I make sure that the closeEvent method is called. However, when I access the "Location" field in QWizard::closeEvent (by using QWizard::field), it does not contain the latest changes but instead it contains the initial value. How can I get the most recent value of a field in QWizard::closeEvent?
Full sources are available here: https://modeltech.org/download/sdf3wizard/sdf3wizard.zip where the 'problem' is on line 124 of wizard.cpp
-
Hi,
Your project is pretty big to go through. You should at least provide a valid sdf file to allow to go on with the wizard. You should also give a step by step instruction set to help people trigger your bug.
On a side note, in
ModelSelector::onUpdate
, why are you re-creating and re-opening a QFile object several times ? -
Good point. Wasn't thinking...
You don't need a valid sdf model file to trigger the issue, just an arbitrary .xml file will already do.
Procedure to hit the issue:
- Start the app and on the Model Selector page, click on the Browse button. Navigate to and select an arbitrary .xml file that is not in the location given by QStandardPaths::writableLocation(QStandardPaths::HomeLocation), i.e., not your home directory on linux
- The application will give some error that it is not a valid file (because ModelSelector::onUpdate checks validity of the file), but it has now triggered setting the "Location" field in ModelSelector::onBrowse to store the latest directory that I want to be stored in the QSettings (i.e., line 146 in modelselector.cpp). I have checked that this works correctly by adding QString Test = field("Location").toString(); after line 146 in modelselector.cpp and inspecting the Test variable in the debugger
- Click the Exit button to trigger the closeEvent on line 120 in wizard.cpp. I have added a similar piece of test code as in the previous bullet to see that the "Location" field does not contain the correct value here
- The settings file that is created (sdf3wizard.conf in directory ./config/modeltech in your home directory on linux) will contain a setting "Location" that should store the latest directory that you visited but it does not
I do not know right now exactly where the QSettings are stored on Windows and MacOS but could find out if needed
As for your remark on ModelSelector::onUpdate. Yes, each time a different .xml file is selected, a QFile is created and opened to check its validity. Is there a better way to do this? Are you suggesting that I can somehow reuse the QFile for different selected .xml files? What I can understand is that I could perhaps use the same QFile in ModelSelector::onBrowse and ModelSelector::onUpdate although I would need to think about the situation where the user just types text in the QLineEdit without hitting the Browse button (i.e., executing ModelSelector::onBrowse)
-
That's because you are doing it "too late". When going back or cancelling,
cleanupPage
is called hence the default value being back.What I meant is that you have something like:
if (!ModelField->text().isEmpty() && (QFileInfo(ModelField->text()).suffix() == "xml")) { QFile File(ModelField->text()); Valid = File.open(QIODevice::ReadOnly); } if (Valid) { QFile File(ModelField->text()); File.open(QIODevice::ReadOnly);
Why create two
File
object and open it twice ?