Results 1 to 5 of 5

Thread: [Question] Is it possible to access a class member from another class?

  1. #1
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question [Question] Is it possible to access a class member from another class?

    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.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: [Question] Is it possible to access a class member from another class?

    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.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Question] Is it possible to access a class member from another class?

    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.

  4. #4
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Question] Is it possible to access a class member from another class?

    Thank you for your replies, I'm going to find some literature about this and study it in-depth.

  5. #5
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Question] Is it possible to access a class member from another class?

    I've completed my application successfully thanks to your help! I appreciate it very very much!

Similar Threads

  1. Using a static member in a class
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 29th April 2010, 10:58
  2. Class member reference
    By waynew in forum Newbie
    Replies: 2
    Last Post: 20th December 2009, 01:38
  3. A class member has ran away?!
    By MIH1406 in forum Newbie
    Replies: 4
    Last Post: 24th August 2009, 23:21
  4. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  5. Replies: 5
    Last Post: 14th July 2006, 22:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.