PDA

View Full Version : [Question] Is it possible to access a class member from another class?



Axtroz
28th May 2010, 21:32
Hello everyone! First and on top of all I am very very sorry for my poor English skills and I am very very sorry if I mislead someone with my explanations.

I am writing a Qt4 Application to launch an external application with different parameters. with the following layout:

4 Classes, 1 is just a container with a QTabWidget in it (this is class0).
Other 3 classes are the actual tabs.
class1 has a QComboBox with parameteres for the external application and a QPushButton, connected to a slot, whose functionality is described below.

class3 has a QPushButton that calls a QFileDialog to choose the external application and a QLineEdit with ReadOnly enabled. When file is selected, the QLineEdit displays the path to the program.

The slot in class1 does the following:
gets the QString from the QLineEdit and launches the external application with the parameters chosen from the QComboBox.

My question is is it possible to access the public QLineEdit in class3 from class1 and get the QString from it to use it in the QProcess and if it is, how?

Thanks for your time and again, I'm sorry for my poor English skills.

SixDegrees
28th May 2010, 22:09
You can access any public member of a class from any other class.

It would probably be better, though, to pass the filename around via signals and slots. This is somewhat "cleaner" than making direct function calls, which can rapidly multiply into a messy tangle of intertwined calls between classes. The same can happen with signals and slots, of course, but the simplicity of the interface makes maintenance easier.

squidge
28th May 2010, 23:19
You need to learn about encapsulation. Even though class 1 could access class 3's QLineEdit, it shouldn't. Class 3's QLineEdit should be local to and accessible by Class 3 only.

Instead, what should happen is that when you press the button, Class 3 will put the text into its QLineEdit, and then notify Class 1 that it has a filename via a signal. Class 1 will then do it's job.

Axtroz
29th May 2010, 00:56
Thank you for your replies, I'm going to find some literature about this and study it in-depth.

Axtroz
29th May 2010, 12:56
I've completed my application successfully thanks to your help! I appreciate it very very much! :):):)