Results 1 to 9 of 9

Thread: How to pass a pointer to a "QMainWindow" subclass in a function ?

  1. #1
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to pass a pointer to a "QMainWindow" subclass in a function ?

    Hello

    I want to access a function inside a custom QMainWindow (subclass that inherits QMainWindow), I called the class "BMW" (basic main window).

    public :
    void AccessFunction(BMW*);

    when I try to pass the pointer to the BMW object, I get an error (BMW has not been declared).
    note that the class which (accessFunction(BMW) ) method is in, is a foreign class, but an object of the class is inside BMW as an instance.

    Strange enough, this method works with other classes ; thus it can communicate easily & trigger foreign functions smoothly.

    Any help ? :/

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    As a precursor I will just say that you have poor design and you shouldn't need to write code like that!!

    To deal with your actual problem that has nothing to do with qt (and so this is the wrong forum for your question...) :

    In your foreign class cpp file you need to #include "bmw.h" or whatever header contains your BMW class.
    In your foreign class hpp file you need to forward declare the class

    e.g.
    Qt Code:
    1. class BMW;
    2.  
    3. class foreignclass
    4. {
    5. public:
    6. void use_bmw(bmw* ptr);
    7. };
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    AtlasS (22nd December 2012)

  4. #3
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    Quote Originally Posted by amleto View Post
    As a precursor I will just say that you have poor design and you shouldn't need to write code like that!!

    To deal with your actual problem that has nothing to do with qt (and so this is the wrong forum for your question...) :

    In your foreign class cpp file you need to #include "bmw.h" or whatever header contains your BMW class.
    In your foreign class hpp file you need to forward declare the class

    e.g.
    Qt Code:
    1. class BMW;
    2.  
    3. class foreignclass
    4. {
    5. public:
    6. void use_bmw(bmw* ptr);
    7. };
    To copy to clipboard, switch view to plain text mode 
    Thank you very much for your comment, It was helpful.
    Excuse me for rejecting your judgement on my design, but I simply forgot to declare the class BMW in my foreign class, but that's still not the problem.

    The class won't take BMW* as a parameter. Here are the points in the code where the problem is


    InterfaceHndler.h >>Foreign class
    Qt Code:
    1. #ifndef INTERFACEHNDLER_H
    2. #define INTERFACEHNDLER_H
    3.  
    4. #include <bmw.h>
    5. #include <KnightsComponent.h>
    6. #include <scene.h>
    7. #include <scenemainmenue.h>
    8. #include <QGraphicsView>
    9.  
    10. class InterfaceHndler
    11. {
    12. class BMW;
    13. public:
    14. InterfaceHndler();
    15.  
    16. QGraphicsView *boardHndler;
    17. Scene *chessBoard;
    18.  
    19. SceneMainMenue *MW;
    20. QImage *Logo;
    21. QGraphicsView *display;
    22. QGraphicsScene *displayScene;
    23. KnightsComponent *button_ng;
    24.  
    25.  
    26.  
    27. void buildMm(BMW*);
    28. void buildGame();
    29. };
    30.  
    31. #endif // INTERFACEHNDLER_H
    To copy to clipboard, switch view to plain text mode 

    BMW.h file

    Qt Code:
    1. #ifndef BMW_H
    2. #define BMW_H
    3. #include <QMainWindow>
    4. #include <QGraphicsView>
    5. #include <QGraphicsScene>
    6. #include <interfacehndler.h>
    7.  
    8.  
    9. class BMW : public QMainWindow
    10. {
    11. //Member variables
    12.  
    13. public:
    14. InterfaceHndler Ih;
    15.  
    16.  
    17.  
    18. public: BMW();
    19.  
    20.  
    21.  
    22.  
    23. };
    24.  
    25. #endif // BMW_H
    To copy to clipboard, switch view to plain text mode 


    I followed this methodology of design to allow my mouse event inside my (SceneMainMenue *MW) variable to respond to custom button clicking, and build other scenes in my main window class. I don't think that my design is poor, it's only a matter of making the BMW ptr works as a parameter....

    I didn't modify the code, I just encapsulated the components into the interfaceHandler class. All I need is to know how to pass my BMW ptr as a parameter into the interface handler class, so I can manage button clicking..

    the problem is not my design. The problem isn't Qt. The problem is simply that QGraphicsScene's mouse event is local.. I'm just trying to escape that locality by simple object communication.

    It worked with simpler classes, but failed with QMainWindow subclasses (Any subclass from QMainWindow).


    Added after 4 minutes:


    I edited the following :

    1-Functions are no longer reside in BMW ; they are defined in the interface handler.
    2-The interfaceHndler class can manage the adding & deleting of elements from/ to the main window by receiving a pointer to to that window as a parameter in its native functions


    Added after 5 minutes:


    BTW, I don't want a simple solution like (adding a main QGraphicsScene) to the main window first, and implementing the mouse event in it, because all I want is simply allowing different objects in different classes to communicate by adding pointers the the destination class through a function.

    I just want to know why I can't pass a QMainWindow as a pointer..

    lol, sorry for talking too much, but I'm a student, and I need to learn..
    Last edited by AtlasS; 22nd December 2012 at 17:38.

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    I have not stated that 'the problem' is your design, but you have a circular reference which is nearly always poor design.

    The class won't take BMW* as a parameter. Here are the points in the code where the problem is.
    The class does take BMW*, look
    Qt Code:
    1. void buildMm(BMW*);
    To copy to clipboard, switch view to plain text mode 


    Please explain what the problem is.

    I just want to know why I can't pass a QMainWindow as a pointer..
    You can.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #5
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    Quote Originally Posted by amleto View Post
    I have not stated that 'the problem' is your design, but it certainly is poor - you have a circular reference.


    The class does take BMW*, look
    Qt Code:
    1. void buildMm(BMW*);
    To copy to clipboard, switch view to plain text mode 


    Please explain what the problem is.

    You can.
    The error still appears whenever I call
    Qt Code:
    1. Ih.buildMm(this);
    To copy to clipboard, switch view to plain text mode 
    inside BMW.

  7. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    didn't I mention circular reference already? you cannot have this line

    #include <bmw.h>

    in your interface class header.


    And another point - when using <> brackets on includes, you are telling the compiler to look in implementation (compiler implementation) defined paths first - this is not correct! for your own source code files you should use normal quotes e.g. "myfile.cpp"
    Last edited by amleto; 22nd December 2012 at 18:15.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    AtlasS (23rd December 2012)

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

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    Graphics Scene events are not "local". As every event the event is meant to be delivered to only one object. If the object rejects it, the event gets delivered to an item under the original item (and so on). Furthermore in Graphics View architecture you have a possibility to catch the event in any of three places:
    1. the item the event is meant to be delivered to,
    2. the scene containing the item,
    3. the view the event (e.g. pressing the mouse button) was triggered on.

    So if you catch the event in the scene, there is no "locality" here, you can access any item in the scene you want or handle the event globally.

    The design you have is poor. It's very C-ish and breaks some OOP principles (like encapsulation). I don't know what you are trying to do but there is a number of design patterns (e.g. the Builder or Factory or maybe even Strategy) that you could use.

    E.g. instead of void "buildMw(BMW*)" you could have a "SomeType* buildMw() const" method.
    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.


  10. The following user says thank you to wysota for this useful post:

    AtlasS (23rd December 2012)

  11. #8
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    amleto

    Thanks again for your patience : ), I have fixed the mistakes you pointed at in the including of files. I have to admit that the design IS POOR as you have stated, I'm going to start things up from scratch (not the whole code, only the interface part). I need to study the mechanisms of the qt classes more, because obviously I'm confused a little..well, maybe a lot ..

    wysota

    I'm trying to design a main screen for my chess program. I have already finished the games (full 2 player chess game), defined over 3 classes :

    class ChessElement ->QGraphicsItem
    class board ->nothing
    class Scene ->QGraphicsScene

    all the ChessElement instances are added to "Scene *x".

    now, having this scene set & ready, I need one main window, starts with a scene added to it (representing the main menu), then I have buttons, which are from a subclass inherits QGraphicsItem (same like chessElement).

    All I need now to present the full program is to build the interface -_-", kinda frustrating since I thought it's the easiest part. The whole problem is in the custom buttons, if I used the QPushButton I would've ended this suffering, but it looks ugly :/ I'm not using that

    I will try to implement what you have wrote in your comment, joining it with what amleto said.

    But please, if there is anything I can read (outside the qt documentation) which might solve my problem, post it here. Since the final exams in my academy are up, I don't have a lot of time to read for my personal projects..One of the main reasons while my design is poor + lacks for concentration.

    best regards

  12. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to pass a pointer to a "QMainWindow" subclass in a function ?

    Your problem *still* boils down to a C++ problem. The code you posted:

    Qt Code:
    1. class InterfaceHndler
    2. {
    3. class BMW;
    4. public:
    5. //...
    To copy to clipboard, switch view to plain text mode 

    is declaring that there is a class, called "BMW" that is an embedded class defined within the scope of the class InterfaceHndler. This means that the name of that class, as far as C++ is concerned, is InterfaceHndler::BMW. And as far as C++ is concerned, that class is totally different from a class named BMW that might be declared in your bmw.h header file as existing at global scope. Therefore, within InterfaceHndler, the declaration of the embedded class BMW overrides any declaration of a globally scoped class BMW, which means the method you have defined is expecting to receive a pointer to an InterfaceHndler::BMW * instance, not a BMW * instance. You are obviously trying to pass a BMW * instance, which the compiler is telling you is not acceptable.

    The solution is easy; move the class BMW forward declaration:

    Qt Code:
    1. class BMW;
    2.  
    3. class InterfaceHndler
    4. {
    5. public:
    6. //...
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 10th August 2012, 19:41
  2. Replies: 5
    Last Post: 2nd April 2010, 21:26
  3. Replies: 4
    Last Post: 19th February 2009, 16:56
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. Convert Windows "HWND" to QWidget* pointer?
    By gerome69 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2006, 13:03

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.