Results 1 to 6 of 6

Thread: qt5/c++ - trying to access a QmainWindow control from another class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 4 Times in 4 Posts

    Default Re: qt5/c++ - trying to access a QmainWindow control from another class

    Hello d_stranz,

    Thanks for your reply.

    In the main() method of your app, you should have something like this, right?
    Yes.

    You don't explain what "class2" is
    Class2 is a set of functions (only) to do with I/O and disk/file manipulation.

    You've given me somewhere to start. If the worst comes to the worst, I can revert to placing all these functions back into class1.
    They were only separated to clean things up a little.

    Regards

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: qt5/c++ - trying to access a QmainWindow control from another class

    Class2 is a set of functions (only) to do with I/O and disk/file manipulation.
    OK, so they need to know something about which file to open or something like that based on a choice made in a combo box on the main window form? And presumably you are creating a class2 instance from within your mainwindow class somewhere?

    Then I would derive class2 from QObject (making sure not to forget to add the Q_OBJECT macro at the top of the class definition). When you create it, you can choose to make the mainwindow instance the parent or not. In any case, if you need only one of them, be sure you implement the creation in such a way that you don't end up creating a new one on every button click or whatever. It's basically the same as the last code snippet I posted, changing the base class for class2:

    Qt Code:
    1. // class2.h
    2.  
    3. #include <QObject>
    4. #include <QString>
    5.  
    6. class class2 : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. class2( QObject * parent ) : QObject( parent ) {}
    12.  
    13. public slots:
    14. void setCurrentText( const QString & text ) { mCurrentText = text; }
    15.  
    16. private:
    17. QString mCurrentText;
    18. }
    19.  
    20. // mainwindow.cpp
    21. class2 * mainwindow::createClass2()
    22. {
    23. class2 * pClass2 = new class2( this );
    24. connect ( myCombo, SIGNAL( currentTextChanged( const QString & ) ), pClass2, SLOT( setCurrentText( const QString & ) ) );
    25.  
    26. // If you want the class2 text to be updated any time the combobox is clicked, not just when the selection changes, then:
    27. connect ( myCombo, SIGNAL( activated( const QString & ) ), pClass2, SLOT( setCurrentText( const QString & ) ) );
    28.  
    29. return pClass2;
    30. }
    To copy to clipboard, switch view to plain text mode 

    You still retain the encapsulation of your I/O methods in class2, plus you add some encapsulation and separation of the mainwindow UI objects. All mainwindow needs to know about class2 is that it has a setCurrentText slot, and class2 doesn't know anything at all about mainwindow. If class2 needs to notify mainwindow when it has finished some I/O operation, then give it a signal to pass information that mainwindow can also connect up to .

    This one of the great things about Qt's signals and slots mechanism. You could completely replace the internals of class2 while retaining the signals and slots, and mainwindow wouldn't have a clue (or need to know). Likewise, *anything* that has a signal with a const QString & argument could be connected to class2's slot, and it wouldn't know or care.

Similar Threads

  1. Replies: 4
    Last Post: 12th November 2015, 13:00
  2. Replies: 4
    Last Post: 2nd April 2013, 09:13
  3. Replies: 1
    Last Post: 29th May 2011, 08:00
  4. how to access and control QLabel by ObjectName?
    By lamp in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2010, 09:31
  5. Replies: 4
    Last Post: 29th May 2010, 12:56

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
  •  
Qt is a trademark of The Qt Company.