Results 1 to 9 of 9

Thread: accessing my main application window widget

  1. #1
    Join Date
    Feb 2006
    Posts
    26
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    MacOS X Windows

    Default accessing my main application window widget

    Hi,

    How can I access the main window widget from a child widget N levels down?

    e.g. in main.cpp I have
    Qt Code:
    1. MainAppUI w;
    2. w.show();
    To copy to clipboard, switch view to plain text mode 
    Then within w I create widgets so eventually I have
    w->aa->bb->cc->...>xx->yy->zz->bottomwidget
    where I never know exactly how many parents there will be.

    How then, in bottomwidget, can I access w? In bottomwidget I declare w as a friend class, but buttomwidget needs to access functions and variables in w. How, without writing a function to explicitly pass the pointer to bottomwidget, can I do this?

    Thanks!
    Jay

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: accessing my main application window widget

    Well... basicly you can call parent() as long as it returns something other than null. The last non-null result returned will be the main window.

    Alternatively you can just call qApp->mainWidget() (just remember to #include <qapplication.h> and to set the main widget in main() ).

  3. #3
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: accessing my main application window widget

    Did you figure it out? trying to do almost the same, and after a couple of hours now and x number of crashes... forum is probably the hope for me...

    I'm having trouble doing several parent(). results in crashes...

    Quote Originally Posted by jayw710
    Hi,

    How can I access the main window widget from a child widget N levels down?

    e.g. in main.cpp I have
    Qt Code:
    1. MainAppUI w;
    2. w.show();
    To copy to clipboard, switch view to plain text mode 
    Then within w I create widgets so eventually I have
    w->aa->bb->cc->...>xx->yy->zz->bottomwidget
    where I never know exactly how many parents there will be.

    How then, in bottomwidget, can I access w? In bottomwidget I declare w as a friend class, but buttomwidget needs to access functions and variables in w. How, without writing a function to explicitly pass the pointer to bottomwidget, can I do this?

    Thanks!
    Jay

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: accessing my main application window widget

    Doing more than one parent() call is a bad design in most cases. In reality, you should have methods in your parent objects to return you a proper pointer of an object you want. Then you'll be sure the pointer return is valid and of proper type.

  5. #5
    Join Date
    Oct 2007
    Posts
    11
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Question

    Hi everyone!

    My problem is seems to be similar as jayw710's so I didn't opened a new topic.

    I'm designing an application what have a main window.
    This was designed in the QT Designer.
    I'm subclassed it with the QDevelop IDE to the mainwindowimpl.cpp , .h
    It generated the MainWindowImpl class.

    I was designed an other dialog whit the designer, subclassed it in the same way.
    This is created the smenu.cpp and the smenu.h header file with the settingsmenu class.
    I have inculded the smenu.h into the mainwindow.h.
    In the class definition of the MainWindowImpl, I created a pointer to the dialog.

    Qt Code:
    1. class MainWindowImpl : public QMainWindow, public Ui::FoAblak
    2. {
    3.  
    4. Q_OBJECT
    5. public:
    6. MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
    7. ~MainWindowImpl();
    8.  
    9. Ui::FoAblak ui;
    10.  
    11. smenu *settingsMenu;
    12. int volume;
    To copy to clipboard, switch view to plain text mode 


    In the constructor of the MainWindowImpl I create an object from it.

    Qt Code:
    1. MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
    2. : QMainWindow(parent, f)
    3. {
    4. _mixerfd = open("/dev/mixer", O_RDWR);
    5. ui.setupUi(this);
    6. smenu = new settingsMenu(this);
    7.  
    8. ...
    9. void doSomething()
    10. {
    11. qWarning() << "it work's";
    12. };
    To copy to clipboard, switch view to plain text mode 

    When I press a button in the main window I call the smenu->show()
    The dialog appears, and everything is fine.

    But -- and there is my big problem -- I would like to acces from this dialog the MainWindow 's functions, variables, etc.
    I know that I have to do something with the constructor's first argument the parent pointer.
    For example I can access the mainwindow's width with the parent->width(), but I can not do it with the parent->doSomething().
    I would like to ask it from you how can I do it?

  6. #6
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing my main application window widget

    Quote Originally Posted by wysota View Post
    Well... basicly you can call parent() as long as it returns something other than null. The last non-null result returned will be the main window.

    Alternatively you can just call qApp->mainWidget() (just remember to #include <qapplication.h> and to set the main widget in main() ).
    I tried setting the main widget in main() like so:

    Qt Code:
    1. #include <QApplication>
    2. #include "mymainwidg.h"
    3. int main(int argc, char** argv)
    4. {
    5. QApplication app(argc, argv);
    6. MyMainWidg widg;
    7. app.setMainWidget(&widg);
    8. widg.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    My compiler whines that setMainWidget is undeclared. What have i missed? Was setMainWidget perhaps not available in version 4.2.2?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing my main application window widget

    Quote Originally Posted by TheRonin View Post
    My compiler whines that setMainWidget is undeclared. What have i missed? Was setMainWidget perhaps not available in version 4.2.2?
    It's available only as a part of Qt3 Support module.

  8. The following user says thank you to jacek for this useful post:

    TheRonin (1st November 2007)

  9. #8
    Join Date
    Oct 2007
    Posts
    11
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Thumbs up Re: accessing my main application window widget

    Quote Originally Posted by martonmiklos View Post
    Hi everyone!

    My problem is seems to be similar as jayw710's so I didn't opened a new topic.

    I'm designing an application what have a main window.
    This was designed in the QT Designer.
    I'm subclassed it with the QDevelop IDE to the mainwindowimpl.cpp , .h
    It generated the MainWindowImpl class.

    I was designed an other dialog whit the designer, subclassed it in the same way.
    This is created the smenu.cpp and the smenu.h header file with the settingsmenu class.
    I have inculded the smenu.h into the mainwindow.h.
    In the class definition of the MainWindowImpl, I created a pointer to the dialog.
    ....
    Jepp...
    I find the soulution for my problem. I share it with you probably it could be useful for somebody in the future.

    So if you passed the this pointer in the argument list you can use it with this syntax in your child class:

    ((MainWindowImpl *)parent)->something

  10. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing my main application window widget

    Quote Originally Posted by martonmiklos View Post
    Jepp...
    I find the soulution for my problem. I share it with you probably it could be useful for somebody in the future.

    So if you passed the this pointer in the argument list you can use it with this syntax in your child class:

    ((MainWindowImpl *)parent)->something
    It's better to make the MakeWindowImpl a singleton in these cases.

Similar Threads

  1. move parent window to the front.
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2007, 08:41
  2. closing dialog in hidden main Widget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2006, 10:35
  3. Replies: 5
    Last Post: 4th August 2006, 23:44
  4. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 10:21
  5. Main window
    By Michiel in forum Qt Tools
    Replies: 1
    Last Post: 20th March 2006, 23:54

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.