PDA

View Full Version : pass "this" as parameter



incapacitant
23rd April 2006, 17:49
I have this code as exemple that uses "this" :



ApplicationWindow::ApplicationWindow()
: QMainWindow( 0 )
{

// set background and size it correctly
( this )->resize( 569, 458 );



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" ?

jpn
23rd April 2006, 18:09
The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself.

jacek
23rd April 2006, 18:11
ApplicationWindow::ApplicationWindow()
: QMainWindow( 0 )
{

// set background and size it correctly
( this )->resize( 569, 458 );

You don't have to use "this" in above code. You can simply write:

ApplicationWindow::ApplicationWindow()
: QMainWindow( 0 )
{

// set background and size it correctly
resize( 569, 458 );



What is the the type of "this" ?
In this case "this" is this: ApplicationWindow*

incapacitant
23rd April 2006, 18:34
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

jacek
23rd April 2006, 19:09
sorry to be a pain, I understand now that the object is ApplicationWindow.
No, ApplicationWindow is a class, i.e. a kind of type.


But in order to declare my function I need the type of ApplicationWindow !

void aFunction ( ApplicationWindow *appWindow, type2 param )
{
// some code to define bool ok
if ( !ok ) QMessageBox::warning( appWindow, param );
}


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.

incapacitant
23rd April 2006, 19:34
slowly getting somewhere thanks, but :

function call:


s = qLoadIniStr( ApplicationWindow, "TxtDateEnvoi" ); // line 27



function definition


QString qLoadIniStr( ApplicationWindow *appWindow, const QString sParam );



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

jpn
23rd April 2006, 20:02
function call:


s = qLoadIniStr( ApplicationWindow, "TxtDateEnvoi" ); // line 27


Where do you have this function call?

- if inside ApplicationWindow:

s = qLoadIniStr( this, "TxtDateEnvoi" );
- if outside ApplicationWindow, you need to one way or another pass a pointer to the application window:


#include "applicationwindow.h"
...
ApplicationWindow* appWindow = ...
...
s = qLoadIniStr( appWindow, "TxtDateEnvoi" );

incapacitant
23rd April 2006, 20:11
I am making the call outside of Application window so yes I need to pointer to the class.

Like you propose:


ApplicationWindow* appwindow = ...


What do you mean with "..." ?

jpn
23rd April 2006, 20:29
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..

incapacitant
23rd April 2006, 21:07
thanks, i give up. will try on a simpler piece of code that I can post later...

fullmetalcoder
24th April 2006, 21:13
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...:rolleyes:

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.


int x;
QSomeClass y;

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.


{

...

QSomeClass *class = new QSomeClass;

class->someFunction();

...

}

void QSomeClass::someFunction()
{
...
}

This C++ code performs something like that (C equivalent, QSomeClass being a structure in that case) :



{

...

QSomeClass *class = new QSomeClass;

someFunction(class);

...

}

void someFunction(QSomeClass *this)
{
...
}


Does that help??? If not you may consider having a look there : What are your favorite C++ book (http://www.qtcentre.org/forum/showthread.php?t=29)s?
I hope it'll help you learning that wonderful programming language C++ is!

incapacitant
23rd May 2006, 09:22
ok, i don't give up...

let me summarize the problem, I have defined a simple procedure outside classes as follows qloadini.cpp :


#include <QtCore>
#include <QtGui>

//************************************************** **************
QStringList qLoadIniVecStr( const QString sParam )
{
QString s;
QStringList param;
QStringList ret;
QString fileName = "efrei.ini";

QFile file( fileName );
if (file.open(QIODevice::ReadOnly)) {
QTextStream is(&file);
while (!is.atEnd()) {
s = is.readLine();
if (s[0]!=QChar('#'))
{
param = s.split( "=" );
if ( param[0] == sParam ) ret.push_back( param[1] );
}
}
}
if (ret.size()==0) ret.push_back( "Error loading: " + param[0] );
return ret;
file.close();
}

//************************************************** **************
QString qLoadIniStr( const QString sParam )
{
QString s;
QStringList param;
QString fileName = "efrei.ini";

QFile file( fileName );
if (file.open(QIODevice::ReadOnly)) {
QTextStream is(&file);
while (!is.atEnd()) {
s = is.readLine();
if (s[0]!=QChar('#'))
{
param = s.split( "=" );
if ( param[0] == sParam )
{
file.close();
return param[1];
}
}
}
}
file.close();
return "Error loading: " + sParam;
}



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


Then I have have the qloadini.h :


#ifndef QLOADINI_H
#define QLOADINI_H

#include <QStringList>

QString qLoadIniStr( const QString sParam );
QStringList qLoadIniVecStr( const QString sParam );

#endif


Finally I call this procedure from various GUI classes with :


TxtError = qLoadIniStr( "Unknown Error" );
ErrHttp = qLoadIniStr( "ErrHttp" );
QMessageBox::warning( this, ErrHttp, TxtError );
// here for instance i don't even bother to check what is returned, the QMessageBox
// displays the default error parameter if the retrieve filed.


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.

wysota
23rd May 2006, 09:57
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.

incapacitant
23rd May 2006, 15:26
Thanks for the hint which as it stands sounds chinese.
So am banned from newbie.

Are there plans for new newbies ? :)

Thanks wysota.

wysota
23rd May 2006, 15:56
Thanks for the hint which as it stands sounds chinese.
So am banned from newbie.

Are there plans for new newbies ? :)

Learn C++ :)

incapacitant
23rd May 2006, 16:05
I've got the books. Just started reading. I'll be back. ;)

incapacitant
25th May 2006, 08:37
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.


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

wysota
25th May 2006, 09:41
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.