Beginner Question
-
I know; this is NOT an uncommon title (and, probably, question).
The questions I found either had dead links or did not answer my question (I also have bought the latest book for QT6, but this still hasn't helped. Gone through the online documentation as best as I can.That's all just in case you think I have come straight here, without spending some time searching. :)
Okay, so I have built the beginnings of an interface using the designer mode (beautifully simple; just like old Visual Studio). However, I now want to read some data in from a file and assign it to defined text boxes/spinners.
I have the basic code:
# This Python file uses the following encoding: utf-8 import os from pathlib import Path import sys from PySide2.QtWidgets import QApplication, QWidget from PySide2.QtCore import QFile from PySide2.QtUiTools import QUiLoader class Widget(QWidget): def __init__(self): super(Widget, self).__init__() self.load_ui() def load_ui(self): loader = QUiLoader() path = os.fspath(Path(__file__).resolve().parent / "form.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) loader.load(ui_file, self) ui_file.close() if __name__ == "__main__": app = QApplication([]) widget = Widget() widget.show() sys.exit(app.exec_())And a file - modules.txt.
I have a collection of spinners and textboxes, but let's assume a textbox called var1 and a spinner called var2.Where do I add the Python code to read in the text file and assign the values to the input box and spinner?
I don't need the code to read in nor assign (I don't think; hahaha!) but where does this go within this code?
-
depends what you want to do with it. it its just to prime beginning values in Widget then do it under load_ui().
-
If this data load should happen when the widget is constructed then the last line of
__init__might a be good place to call a function that does the file access.
If it should happen in response to a user, for example, clicking a button in the UI then this code should be called from a slot reacting to that event. -
If this data load should happen when the widget is constructed then the last line of
__init__might a be good place to call a function that does the file access.
If it should happen in response to a user, for example, clicking a button in the UI then this code should be called from a slot reacting to that event.@ChrisW67
Thank you.
One of the issues when posting is to ensure you capture the full intent, and I clearly failed in that attempt.Yes, the intention is to have this occur on load. Though a future development might be to add the ability to use an explorer/finder window to locate the input file, that is not truly relevant or necessary at present.
I did comprehend, as with VS, that I could attach the instructions to any object; so the user may carry out the update (this is the next step after this one, but a step I feel confident I can work out). My biggest issue, on opening the system up for the first time, was where I placed the initialisation of the controls (textboxes, spinners, etc) - and you have answered that.
Again, thank you. :)