Results 1 to 3 of 3

Thread: Required a pointer "this" to QWidget inside a component class.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Required a pointer "this" to QWidget inside a component class.

    Hello,

    I have recently found a problem when I wanted to use a Qt dialog window to show information or ask for a file path. The problem is that I need to do it inside a constructor of another class. If I had an arbitrary code below, what would I need put in the line 43 (inside a constructor of class A) to open QMessageBox that requires a pointer "this" to my window class? If I inherited from MyWindow, would something like &(static_cast<MyWindow&>(*this)) work?

    Thanks

    Qt Code:
    1. #include <QApplication>
    2. #include <QMessageBox>
    3. #include <QWidget>
    4.  
    5. ////////////////////////////////////////
    6. class MyWindow : public QWidget {
    7. Q_OBJECT
    8. public:
    9. MyWindow(QWidget* parent = 0);
    10.  
    11. public slots:
    12. signals:
    13. };
    14.  
    15. class A {
    16. int a;
    17. double b;
    18. public:
    19. A(int _a = 0, double _b = 0.0);
    20. };
    21. ////////////////////////////////////////
    22.  
    23. ///// ===== main function ====
    24. int main(int argc, char* argv[]) {
    25. QApplication app(argc,argv);
    26.  
    27. return app.exec();
    28. }
    29. ///// ===== end of main ======
    30.  
    31. ///// MyWindow //////////////////////////
    32. MyWindow::MyWindow(QWidget* parent)
    33. : QWidget(parent) {
    34. // Create Layout, actions, menus, etc.
    35. A object_A(10,20.5);
    36. }
    37. ///// End of MyWindow ///////////////////
    38.  
    39. ///// A /////////////////////////////////
    40. A::A(int _a, double _b) : a(_a), b(_b) {
    41. // Some problems here and information need to be shown:
    42. QMessageBox::information(
    43. this,
    44. tr("Title"),
    45. tr("Information to be shown"));
    46. }
    47. ///// End of A //////////////////////////
    To copy to clipboard, switch view to plain text mode 

    PS. I did not really run this code. It is only to show the problem.
    Last edited by ZikO; 10th August 2012 at 16:09.

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

    Default Re: Required a pointer "this" to QWidget inside a component class.

    why not just use NULL?

    Otherwise change A like
    Qt Code:
    1. class A {
    2. int a;
    3. double b;
    4. public:
    5. A(int _a = 0, double _b = 0.0, QWidget* parentForMsgBox = 0);
    6. };
    7.  
    8. ...
    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:

    ZikO (13th August 2012)

  4. #3
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Required a pointer "this" to QWidget inside a component class.

    Hello,

    OK. Thanks for this advice. I usually do so providing a full code. I did think my code was too long and it would take the whole page. Also, the code was separated into several headers / source files due to a few classes / structures I have implemented. I just assumed the problem would be to trivial but I will follow this rule next time.

    As to the problem, it works now. I came here because I am still a newbie programmer and I did expect the problem would be solved in a very easy way. I really should have found it by myself :/ somehow still not experienced enough to realise that extra argument would solve the problem.

    Thank you.

Similar Threads

  1. Replies: 0
    Last Post: 16th June 2011, 21:48
  2. How to "sleep" inside QRunnable-derived class?
    By TorAn in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2009, 22:03
  3. Replies: 2
    Last Post: 7th September 2009, 21:13
  4. Replies: 2
    Last Post: 11th May 2009, 16:50
  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.