Results 1 to 7 of 7

Thread: Best way to acces MainWindow instance from separate class

  1. #1
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Best way to acces MainWindow instance from separate class

    Hello,

    What is the best way to access MainWindow instance from a separate class.

    Main.cpp
    Qt Code:
    1. ...
    2. int main(int argc, char *argv[])
    3. {
    4. QApplication a(argc, argv);
    5. MainWindow w;
    6. w.show();
    7.  
    8. return a.exec();
    9. }
    10. ...
    To copy to clipboard, switch view to plain text mode 

    How can I do this?

    Thank you have a good day!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best way to acces MainWindow instance from separate class

    Pass a pointer to the window instance to the other class instance or use signals and slots.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Best way to acces MainWindow instance from separate class

    Quote Originally Posted by wysota View Post
    Pass a pointer to the window instance to the other class instance or use signals and slots.
    Hello,

    How can you use signals and slots to accomplish this?

    Thank you

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Best way to acces MainWindow instance from separate class

    You connect signals from the window instance to the other object and vice-versa. All depending on what functionality you want to implement.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Best way to acces MainWindow instance from separate class

    MainWindow class contains a QTableWidget, I am trying to access the tables data from another class. It has to be accessing the current instance of the class.

    Thank you

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Best way to acces MainWindow instance from separate class

    The answer hasn't changed.

    You use generic C++ mechanisms to achieve this. Expose the data through a public interface and give the other class instance a pointer/reference to the source instance.
    OR
    When something happens in the table widget that the other class needs to hear about you emit a signal which is, in turn, connected to a slot in the other class. The signal can carry relevant data as parameters.

    If the entire content of the QTableWidget needs to be shared between views then you should be using QTableView and a separate shared model.

    Exactly how you do it is strongly dependent on what you need to achieve.

  7. #7
    Join Date
    May 2013
    Posts
    45
    Thanks
    6
    Thanked 6 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Best way to acces MainWindow instance from separate class

    Thank you I understand now.

Similar Threads

  1. How to Have a class run in a separate thread.
    By sona1111 in forum Newbie
    Replies: 7
    Last Post: 29th August 2013, 07:44
  2. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 17:15
  3. Replies: 4
    Last Post: 23rd August 2011, 22:53
  4. separate class using an interface
    By qt_gotcha in forum Newbie
    Replies: 7
    Last Post: 3rd March 2010, 22:54
  5. separate Qt class diagram
    By juanrb in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2008, 20:55

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.