Results 1 to 18 of 18

Thread: pass "this" as parameter

  1. #1
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    I have this code as exemple that uses "this" :

    Qt Code:
    1. ApplicationWindow::ApplicationWindow()
    2. : QMainWindow( 0 )
    3. {
    4.  
    5. // set background and size it correctly
    6. ( this )->resize( 569, 458 );
    To copy to clipboard, switch view to plain text mode 


    I would need to call a procedure with "this" as parameter so that I can display stuff in "this" from the procedure. Like for instance resize "this" inside another procedure to make my main proc shorter and cleaner.

    How do I declare the function ? What is the the type of "this" ?

  2. #2
    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: pass "this" as parameter

    The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself.
    J-P Nurmi

  3. #3
    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: pass "this" as parameter

    Quote Originally Posted by incapacitant
    Qt Code:
    1. ApplicationWindow::ApplicationWindow()
    2. : QMainWindow( 0 )
    3. {
    4.  
    5. // set background and size it correctly
    6. ( this )->resize( 569, 458 );
    To copy to clipboard, switch view to plain text mode 
    You don't have to use "this" in above code. You can simply write:
    Qt Code:
    1. ApplicationWindow::ApplicationWindow()
    2. : QMainWindow( 0 )
    3. {
    4.  
    5. // set background and size it correctly
    6. resize( 569, 458 );
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by incapacitant
    What is the the type of "this" ?
    In this case "this" is this: ApplicationWindow*

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

    incapacitant (23rd April 2006)

  5. #4
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    sorry to be a pain, I understand now that the object is ApplicationWindow.
    But in order to declare my function I need the type of ApplicationWindow !

    have some trouble to declare it :
    void aFunction ( type??? ApplicationWindow, type2 param )
    {
    // some code to define bool ok
    if ( !ok ) QMessageBox::warning( ApplicationWindow, param );
    }

    Also in the function body instead of "this" I will just say ApplicationWindow, right ?

    PS: why I am defined as Intermediate User, all this ranking should be based on the number of thanks people get. I never got any thanks, how could I ? But this is another story...
    Thanks

  6. #5
    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: pass "this" as parameter

    Quote Originally Posted by incapacitant
    sorry to be a pain, I understand now that the object is ApplicationWindow.
    No, ApplicationWindow is a class, i.e. a kind of type.

    Quote Originally Posted by incapacitant
    But in order to declare my function I need the type of ApplicationWindow !
    Qt Code:
    1. void aFunction ( ApplicationWindow *appWindow, type2 param )
    2. {
    3. // some code to define bool ok
    4. if ( !ok ) QMessageBox::warning( appWindow, param );
    5. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by incapacitant
    why I am defined as Intermediate User, all this ranking should be based on the number of thanks people get. I never got any thanks, how could I ?
    Because title is based on the number of posts. The "thanks" mechanism was added, so that people could say "Thank you" without interfering with the discussion.

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

    incapacitant (23rd April 2006)

  8. #6
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    slowly getting somewhere thanks, but :

    function call:
    Qt Code:
    1. s = qLoadIniStr( ApplicationWindow, "TxtDateEnvoi" ); // line 27
    To copy to clipboard, switch view to plain text mode 


    function definition
    Qt Code:
    1. QString qLoadIniStr( ApplicationWindow *appWindow, const QString sParam );
    To copy to clipboard, switch view to plain text mode 


    compile error:
    27 C:\Qt\test\sms\archiv.cpp expected primary-expression before ',' token

  9. #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: pass "this" as parameter

    Quote Originally Posted by incapacitant
    function call:
    Qt Code:
    1. s = qLoadIniStr( ApplicationWindow, "TxtDateEnvoi" ); // line 27
    To copy to clipboard, switch view to plain text mode 
    Where do you have this function call?

    - if inside ApplicationWindow:
    Qt Code:
    1. s = qLoadIniStr( this, "TxtDateEnvoi" );
    To copy to clipboard, switch view to plain text mode 
    - if outside ApplicationWindow, you need to one way or another pass a pointer to the application window:
    Qt Code:
    1. #include "applicationwindow.h"
    2. ...
    3. ApplicationWindow* appWindow = ...
    4. ...
    5. s = qLoadIniStr( appWindow, "TxtDateEnvoi" );
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. #8
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    I am making the call outside of Application window so yes I need to pointer to the class.

    Like you propose:
    Qt Code:
    1. ApplicationWindow* appwindow = ...
    To copy to clipboard, switch view to plain text mode 

    What do you mean with "..." ?

  11. #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: pass "this" as parameter

    I meant that you should fill in the gaps.
    Hard, if not impossible, to advise you more without knowing a little more about your code..
    J-P Nurmi

  12. #10
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: pass "this" as parameter

    thanks, i give up. will try on a simpler piece of code that I can post later...

  13. #11
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pass "this" as parameter

    Quote Originally Posted by incapacitant
    thanks, i give up. will try on a simpler piece of code that I can post later...
    Why the hell would you give up??? It is the simplest piece of code possible! Are you sure you learnt C++, if I remember well, a guy here as a nice signature that may help you understand where lies your problem...

    A class, such as your mainWindow, is kinda perticular type, with its own data and structure while an object is an instance of a class.

    To simplify, "class QSomeClass" would be much like "int", the only difference being that "QSomeClass" is an user-defined type while "int" is a built-in one.
    Qt Code:
    1. int x;
    2. QSomeClass y;
    To copy to clipboard, switch view to plain text mode 
    x and y are both objects, i.e. instance of a given type

    To access an object or give it as parameter to a function you need a pointer or a reference to it. "this" is a perticular keyword that points to the instance of the object used for a function.
    Qt Code:
    1. {
    2.  
    3. ...
    4.  
    5. QSomeClass *class = new QSomeClass;
    6.  
    7. class->someFunction();
    8.  
    9. ...
    10.  
    11. }
    12.  
    13. void QSomeClass::someFunction()
    14. {
    15. ...
    16. }
    To copy to clipboard, switch view to plain text mode 
    This C++ code performs something like that (C equivalent, QSomeClass being a structure in that case) :

    Qt Code:
    1. {
    2.  
    3. ...
    4.  
    5. QSomeClass *class = new QSomeClass;
    6.  
    7. someFunction(class);
    8.  
    9. ...
    10.  
    11. }
    12.  
    13. void someFunction(QSomeClass *this)
    14. {
    15. ...
    16. }
    To copy to clipboard, switch view to plain text mode 

    Does that help??? If not you may consider having a look there : What are your favorite C++ books?
    I hope it'll help you learning that wonderful programming language C++ is!
    Current Qt projects : QCodeEdit, RotiDeCode

  14. The following user says thank you to fullmetalcoder for this useful post:

    incapacitant (23rd May 2006)

  15. #12
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    ok, i don't give up...

    let me summarize the problem, I have defined a simple procedure outside classes as follows qloadini.cpp :
    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. //****************************************************************
    5. QStringList qLoadIniVecStr( const QString sParam )
    6. {
    7. QStringList param;
    8. QString fileName = "efrei.ini";
    9.  
    10. QFile file( fileName );
    11. if (file.open(QIODevice::ReadOnly)) {
    12. QTextStream is(&file);
    13. while (!is.atEnd()) {
    14. s = is.readLine();
    15. if (s[0]!=QChar('#'))
    16. {
    17. param = s.split( "=" );
    18. if ( param[0] == sParam ) ret.push_back( param[1] );
    19. }
    20. }
    21. }
    22. if (ret.size()==0) ret.push_back( "Error loading: " + param[0] );
    23. return ret;
    24. file.close();
    25. }
    26.  
    27. //****************************************************************
    28. QString qLoadIniStr( const QString sParam )
    29. {
    30. QStringList param;
    31. QString fileName = "efrei.ini";
    32.  
    33. QFile file( fileName );
    34. if (file.open(QIODevice::ReadOnly)) {
    35. QTextStream is(&file);
    36. while (!is.atEnd()) {
    37. s = is.readLine();
    38. if (s[0]!=QChar('#'))
    39. {
    40. param = s.split( "=" );
    41. if ( param[0] == sParam )
    42. {
    43. file.close();
    44. return param[1];
    45. }
    46. }
    47. }
    48. }
    49. file.close();
    50. return "Error loading: " + sParam;
    51. }
    To copy to clipboard, switch view to plain text mode 

    The idea is to externalize constants and load them from an ini file when I need them.


    Then I have have the qloadini.h :
    Qt Code:
    1. #ifndef QLOADINI_H
    2. #define QLOADINI_H
    3.  
    4. #include <QStringList>
    5.  
    6. QString qLoadIniStr( const QString sParam );
    7. QStringList qLoadIniVecStr( const QString sParam );
    8.  
    9. #endif
    To copy to clipboard, switch view to plain text mode 

    Finally I call this procedure from various GUI classes with :
    Qt Code:
    1. TxtError = qLoadIniStr( "Unknown Error" );
    2. ErrHttp = qLoadIniStr( "ErrHttp" );
    3. QMessageBox::warning( this, ErrHttp, TxtError );
    4. // here for instance i don't even bother to check what is returned, the QMessageBox
    5. // displays the default error parameter if the retrieve filed.
    To copy to clipboard, switch view to plain text mode 

    As the qloadini procs are defined they return some message that tells me whether I was able to retrieve the constant or not. The problem is that I then have to handle the errors in the calling procedure which is cluttering the code. What I would have liked is to be able to display a QMessageBox inside the qloadini procedures themselves.
    It would be a generic message telling that a constant parameter is not retrieved and that the ini file has something wrong with it.

    But I don't know how to pass this "this" parameter so that the qloadini QMessageBox can address the current window. I could dupplicate qloadini in all classes that call it but that would hardly be an improvement.

  16. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: pass "this" as parameter

    Declare the function parameter as a base class of all Qt window classes. As you're learning C++ right now ( ), I won't tell you which is it and I suggest others do the same. Find what is the base class of all Qt window classes and why does it work when you pass a pointer to the base class and then call the function with a subclass as a parameter.

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

    incapacitant (23rd May 2006)

  18. #14
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    Thanks for the hint which as it stands sounds chinese.
    So am banned from newbie.

    Are there plans for new newbies ?

    Thanks wysota.

  19. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: pass "this" as parameter

    Quote Originally Posted by incapacitant
    Thanks for the hint which as it stands sounds chinese.
    So am banned from newbie.

    Are there plans for new newbies ?
    Learn C++

  20. #16
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    I've got the books. Just started reading. I'll be back.

  21. #17
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default pass "this" as parameter

    Qt Code:
    1. Declare the function parameter as a base class of all Qt window classes. As you're learning C++ right now (:cool: ), I won't tell you which is it and I suggest others do the same. Find what is the base class of all Qt window classes and why does it work when you pass a pointer to the base class and then call the function with a subclass as a parameter.
    To copy to clipboard, switch view to plain text mode 

    Nice hint that I store carefully in a text file, however given my poor knowledge could you say the same thing in more words, in newbie terms ?
    Thanks

  22. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: pass "this" as parameter

    It's as newbie as it can get. Just learn about classes, base classes (superclasses) and subclasses and look into Qt documentation and find the base class of all Qt top level windows.

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

    incapacitant (25th May 2006)

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Error: BadMatch --> what's it?
    By mattia in forum Newbie
    Replies: 4
    Last Post: 9th June 2008, 12:20

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.