Results 1 to 17 of 17

Thread: QMainWindow pointer

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QMainWindow pointer

    Hi,
    Is there any way to get the MainWindow pointer on any part of the code?

    I need to get the MainWindow pointer to make connections into other objects. These objects are created dinamically by user operation and I want to connect a SIGNAL from the object to the SLOT of the MainWindow.

    I need this because I have to make that the GUI Thread paint Images that the objects create.

    Thanks,
    Last edited by ^NyAw^; 18th November 2006 at 16:59.
    Òscar Llarch i Galán

  2. #2
    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: QMainWindow pointer

    There is no "main" window in Qt4. If your application has one main window, you can always add a static method that returns a pointer to it (read about singleton pattern).

    Where do you create those objects?

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow pointer

    Hi,
    There are objects that are created in the main window. Then, this objects have more objects inside them, ...
    for example:
    Qt Code:
    1. classA
    2. {
    3. classB* pB;
    4. }
    5.  
    6. classB
    7. {
    8. classC* pC;
    9. }
    10.  
    11. classC
    12. {
    13. classD* pD;
    14. }
    15.  
    16. classD
    17. {
    18. /*Here I want the pointer to the main window to connect a SIGNAL from classD and
    19.   a SLOT from main window because I need to paint Images and only the GUI Thread is
    20.   able to do it.
    21.   Do you remember the other thread that we are talking about Producer and Consumer
    22.   that my application hangs because another Thread tryies to paint Images.
    23.   */
    24. }
    To copy to clipboard, switch view to plain text mode 

    So, have I to pass the pointer to the widget along all the classes or there is a way to get the main widget pointer anywhere in the code?

    Thanks,
    Last edited by jacek; 18th November 2006 at 17:51. Reason: wrapped too long lines
    Òscar Llarch i Galán

  4. #4
    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: QMainWindow pointer

    Quote Originally Posted by ^NyAw^ View Post
    There are objects that are created in the main window. Then, this objects have more objects inside them, ...
    for example:
    It's a bit to abstract example to propose any other solutions, so either you will have to pass the pointer or, if you have only one main window instance, you can use the singleton pattern.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: QMainWindow pointer

    Ok,
    Will try it.

    Thanks for your time,
    Òscar Llarch i Galán

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

    Default Re: QMainWindow pointer

    If your objects are QObjects and are somehow attached to the application object, you can use QObject methods for finding children, but it is a bit complicated and slower than simply storing the pointer somewhere as Jacek already suggested.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMainWindow pointer

    Signal chaining (a signal connected to another signal) could also be one possibility.

    Qt Code:
    1. class MainWindow
    2. {
    3. public:
    4. MainWindow(...)
    5. {
    6. SomeClass* some = new SomeClass(...);
    7. connect(some, SIGNAL(somethingHappened()), this, SLOT(doSomething());
    8. }
    9.  
    10. private slots:
    11. void doSomething()
    12. {
    13. ...
    14. }
    15. }
    16.  
    17. class SomeClass
    18. {
    19. public:
    20. SomeClass(...)
    21. {
    22. AnotherClass* another = new AnotherClass(...);
    23. connect(another, SIGNAL(somethingHappened()), this, SIGNAL(somethingHappened()));
    24. }
    25.  
    26. signals:
    27. void somethingHappened();
    28. }
    29.  
    30. class AnotherClass
    31. {
    32. signals:
    33. void somethingHappened();
    34. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow pointer

    Hi,
    I'm having trouble with passing the pointer to othre classes. When I include the main window header in a class that is included in the main window header there are errors compiling. I think this is because one includes the other and the other includes the one, so ... there is a infinite bucle including that the compiler detects.

    mmm, is there anyway to force a class (main window) to response a SIGNAL without making the connections? I want to kwon if there is a way to force the main window to response the SIGNAL "X" with the SLOT "slotX" without having to tell who is the sender of the signal.

    Thanks,
    Òscar Llarch i Galán

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMainWindow pointer

    Quote Originally Posted by ^NyAw^ View Post
    I'm having trouble with passing the pointer to othre classes. When I include the main window header in a class that is included in the main window header there are errors compiling. I think this is because one includes the other and the other includes the one, so ... there is a infinite bucle including that the compiler detects.
    Circular dependency

    Quote Originally Posted by ^NyAw^ View Post
    mmm, is there anyway to force a class (main window) to response a SIGNAL without making the connections? I want to kwon if there is a way to force the main window to response the SIGNAL "X" with the SLOT "slotX" without having to tell who is the sender of the signal.
    You must pass a valid sender and a valid receiver to the connect-statement. Did you read the signal chaining example?
    J-P Nurmi

  10. #10
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow pointer

    Hi,
    Yes, I have readed it. I'm trying to put down it now. It's a complicated task because I have to emit 5 different signal types, and I have a lot of classes that have to emit this signals and response with the properly slots. Also I have some derived classes that are the first that emit these signals, so ... working on it.

    I have tryied to make the connections between 2 classes like your example and I have a question. I have readed about the 2 connection types "Qt:irectConnection" and "Qt::QueuedConnection". If I use the "Qt::QueuedConnection", the connection is not made.
    Òscar Llarch i Galán

  11. #11
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow pointer

    Hi,
    Now I'm reading about Circular Dependency and I think that it wolud be a little simple way to resolve it. I'm gonna try to use forward declarations.

    I will tell you if I solve this.

    Thanks,
    Òscar Llarch i Galán

  12. #12
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow pointer

    Hi,
    I'm having trouble.

    Now I'm able to make the connection of the main window and the class. "connect" returns true, but there is no response to the SIGNAL with the SLOT. In the connect call I had to convert the main window pointer to QObject pointer. I checked if the pointer points to the main window object, it's ok.

    The SIGNAL is emmited, but nothing happens in the SLOT, it is not called. The object that emmits the SIGNAL is in other Thread. I've tryied to use QueuedConnection but it returns me false. Using DirectConnection retruns me true but I think that I have to use QueuedConnection emmiting SIGNALS from another Thread to the Main Thread.

    I'm passing a non Qt class type with the SIGNAL and reciving it with the SLOT so I have called
    "qRegisterMetaType<HImage>("HImage");" from the main window constructor as the Mandelbrot example does. It returned me an integer (256).
    Òscar Llarch i Galán

  13. #13
    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: QMainWindow pointer

    Quote Originally Posted by ^NyAw^ View Post
    Now I'm able to make the connection of the main window and the class. "connect" returns true, but there is no response to the SIGNAL with the SLOT. In the connect call I had to convert the main window pointer to QObject pointer. I checked if the pointer points to the main window object, it's ok.
    That conversion shouldn't be necessary, unless you are missing #include directive.

    Are there any messages on the console? Remember that under windows you have to add CONFIG += console to your .pro file.
    Last edited by jacek; 19th November 2006 at 19:04. Reason: spelling error

  14. #14
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow pointer

    Hi,
    I'm thinking that I'm doing something wrong.
    The objects that connect with the main window are created from the main thread, then I created the Thread that would have a pointer to these objects. The code of the objects are called from this Thread, but the objects are created in the main thread.

    Could be this the error? So, if yes, I have to create the objects into the Thread?
    Òscar Llarch i Galán

  15. #15
    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: QMainWindow pointer

    Quote Originally Posted by ^NyAw^ View Post
    Could be this the error? So, if yes, I have to create the objects into the Thread?
    No, if you use queued connections.

  16. #16
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow pointer

    Hi,
    Ok, so I can pass a pointer of the Object to the Thread.
    The connect still returns me false with QueuedConnection.

    I'm calliing the connect function out of the Thread. When I create the object that would emmit the signal, the connection is done by this object, not by the Thread.

    Qt Code:
    1. classA
    2. {
    3. classB* pB;
    4. }
    5.  
    6. classB
    7. {
    8. classD* pD;
    9. pD = new classD(.....);
    10. classC* pCThread = new classC(pD); //The constructor of the classC has a pointer to the classD
    11.  
    12. }
    13.  
    14. classC : QThread
    15. {
    16. //Here it have a pointer to the classD object. This Thread will call the classD function to make jobs
    17. }
    18.  
    19. classD
    20. {
    21. //Here I have the pointer to the main window, so I try to connect a SLOT from it and a SIGNAL from this classD
    22. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if I explained it clearly

    Thanks,
    Last edited by ^NyAw^; 19th November 2006 at 19:22. Reason: updated contents
    Òscar Llarch i Galán

  17. #17
    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: QMainWindow pointer

    Quote Originally Posted by ^NyAw^ View Post
    The connect still returns me false with QueuedConnection.
    For the second time: Are there any messages on the console?

Similar Threads

  1. Disabling the Maximize button in QMainWindow ?
    By sunil.thaha in forum Qt Programming
    Replies: 17
    Last Post: 26th June 2017, 13:31
  2. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 01:15
  3. Insert separate QToolBar into QMainWindow
    By YuriyRusinov in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 10:37
  4. Signals destroys my pointer
    By Jojo in forum Qt Programming
    Replies: 12
    Last Post: 7th March 2006, 21:05
  5. Replies: 18
    Last Post: 22nd February 2006, 20:51

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.