using Qt I have a QStackedWidget with several QPushButton buttons. The parent widget of the widget is the QMainWindow. Inside the QStackedWidget I also have some QLineEdit. What I want to do is simple: validate the form that contains the textboxes and if it’s all fine call a function of a custom class that will process the data. The instance of the class is created inside my QMainWindow class. What would be the best way to call the methods of that class? Should I use the signals/slots mechanism of Qt connecting my custom class to the instances of the widget inside the QStackedWidget?
using Qt I have a QStackedWidget with several QPushButton buttons. The parent widget of
Share
You can derive off of QValidator as many times as necessary and install a validator on each field that requires validation. This way, you are ensured that the field is either empty or else contains a valid entry.
Before processing your data, make sure that all of the appropriate fields have data, and you’re guaranteed then that you have a valid form.
The typical way to pass data from a form to some consumer of form data is to create a middle-man class to hold the data. An instance of the class is populated after the form is validated, and the instance is then passed to the consumer. This way, if there are changes in the form, your consumer is isolated from these changes, provided the same data is being collected.
The general rule is to work against object interfaces, not their implementation. It doesn’t matter whether you do sig/slot or a direct function call so long as you keep some things in mind:
Sig/slot does not guarantee call order, and emitting signals essentially means that anybody who can see this object can connect to its signal and pick this change up. if you must have a guaranteed order of processing of your form data by sub-components, then why even give a maintenance programmer the option to mess up?
Secondly, it should not matter where the objects reside. Some day when your data consumers become very complicated and you want to snap a different one in based upon some scenario (maybe user privs, or today’s date, or the current temperature, I don’t know!), then you’re going to want to do a re-arch. If your logic for getting the data to the consumers is highly dependent upon the objects residing in a certain way in your form, then you’re doing it wrong.