PDA

View Full Version : cannot call member function without object



been_1990
23rd October 2010, 03:07
I get the following error when trying to connect two classes together:

qapp.cpp: In static member function 'static bool QApp::eventFilter(void*)':
qapp.cpp:32: error: cannot call member function 'void QApp::mouseCoord(int, int, int, int, int)' without object

main.cpp:

int main(int argc, char *argv[])
{
QApp a(argc, argv);
Widget w;
w.show();

QObject::connect(&a,SIGNAL(mouseCoords(int, int, int, int, int)),
&w,SLOT(mouseCoords(int, int, int, int, int)));

return a.exec();
}

qapp.cpp:

emit mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i), // this is line 32...
is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));

I have searched the forum and found several issues quite like mine, but not enough to help me out.

ChrisW67
23rd October 2010, 04:54
As the compiler error message says, you cannot call a member function, which requires a "this" pointer, from a static method that does not have a "this" pointer to give. This might do it:

QApp *app = qobject_cast<QApp>(qApp);
app->mouseCoords(...);


BTW: Is that mouseCoord() or mouseCoords() ? You aren't consistent.

been_1990
23rd October 2010, 12:01
If I do this:

QApp *app = qobject_cast<QApp>(qApp);
app->mouseCoords(...); // what is this exactly? Am I not supposed to use connect()?
Then what happens to this?

QApp a(argc, argv);

Thanks for the help.

wysota
23rd October 2010, 12:23
I would ask first why did you make your QApp::eventFilter() method static?

been_1990
23rd October 2010, 14:18
I made it static because that's the only way it would work with QAbstractEventDispatcher(). I was experiencing some difficulties with setting QAbstractEventDispatcher() filter and a thread here in QtCentre Forum said that I should make eventFilter() static, I did and it worked. Only for that reason. But if there is a better way I would really like to know.

EDIT:
if I remove "static" from eventFilter I get this error message:

qapp.cpp: In constructor 'QApp::QApp(int&, char**)':

qapp.cpp:14: error: no matching function for call to 'QAbstractEventDispatcher::setEventFilter(<unresolved overloaded function type>)'

c:\Qt\2010.05\qt\include\QtCore/../../src/corelib/kernel/qabstracteventdispatcher.h:91: note: candidates are: bool (* QAbstractEventDispatcher::setEventFilter(bool (*)(void*)))(void*)

wysota
23rd October 2010, 14:30
But this method shouldn't be static. And if you make it static then you can't call non-static methods from it. What do you need QAbstractEventDispatcher for and what is that you can't make work without making eventFilter() static?

been_1990
23rd October 2010, 14:33
I need QAbstractEventDispatcher to be able to get WM_INPUT messages because winEvenFilter won't do it.


EDIT:
if I remove "static" from eventFilter I get this error message:



qapp.cpp: In constructor 'QApp::QApp(int&, char**)':

qapp.cpp:14: error: no matching function for call to 'QAbstractEventDispatcher::setEventFilter(<unresolved overloaded function type>)'

c:\Qt\2010.05\qt\include\QtCore/../../src/corelib/kernel/qabstracteventdispatcher.h:91: note: candidates are: bool (* QAbstractEventDispatcher::setEventFilter(bool (*)(void*)))(void*)

wysota
23rd October 2010, 14:52
Can we see the exact code?

been_1990
23rd October 2010, 15:09
Yes. Here it is:
main.cpp:

#include <QtGui/QApplication>
#include <QObject>
#include "widget.h"
#include "qapp.h"


int main(int argc, char *argv[])
{
//QApp *app = qobject_cast<QApp>(qApp);
QApp a(argc, argv);
Widget w;
w.show();

QObject::connect(&a,SIGNAL(mouseCoord(int, int, int, int, int)),
&w,SLOT(mouseCoord(int, int, int, int, int)));

return a.exec();
}

qapp.cpp:

extern "C"{
#include "raw_mouse.h"
}
#include "qapp.h"
#include <QDebug>
#include <QAbstractEventDispatcher>
#define WM_INPUT 0x00FF

QApp::QApp(int & argc, char ** argv) :
QApplication(argc,argv)
{
qDebug() << init_raw_mouse(1, 0, 1);
qDebug() << raw_mouse_count();
QAbstractEventDispatcher::instance(0)->setEventFilter(eventFilter);



}


bool QApp::eventFilter(void *msg)
{
MSG *mess;

mess = (MSG*)msg;
//qDebug() << mess->message;
switch(mess->message){
case WM_INPUT:
{
add_to_raw_mouse_x_and_y((HANDLE)mess->lParam);
for (int i = 0; i < raw_mouse_count(); i++){
emit mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i),
is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
}

}
return true;
}
return false;
}


app.h:

#ifndef QAPP_H
#define QAPP_H

#include <QApplication>

class QApp : public QApplication
{
Q_OBJECT
public:
explicit QApp(int & argc, char ** argv );
//bool winEventFilter(MSG *message, long *result);
static bool eventFilter(void *msg);
private:

signals:
void mouseCoord(int x, int y, int btn1, int btn2, int btn3);

public slots:

};

#endif // QAPP_H

wysota
23rd October 2010, 16:01
Why not do it this way?


bool myInputEventFilter(void*);

class QApp : public QApplication {
Q_OBJECT
friend bool myInputEventFilter(void *);
public:
QApp(int argc, char **argv) : QApplication(argc, argv){
init_raw_mouse(1, 0, 1);
QAbstractEventDispatcher::instance(0)->setEventFilter(myInputEventFilter);
}
signals:
void mouseCoord(int x, int y, int btn1, int btn2, int btn3);
};

// the following function may probably be made static so that it doesn't pollute the namespace
bool myInputEventFilter(void *msg) {
MSG *mess = (MSG*)msg;
QApp *app = (QApp*)qApp;
switch(mess->message){
case WM_INPUT:
{
add_to_raw_mouse_x_and_y((HANDLE)mess->lParam);
for (int i = 0; i < raw_mouse_count(); i++){
emit app->mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i),
is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
}

}
return true;
}
return false;
}

been_1990
23rd October 2010, 17:07
Great! Working perfectly now. Thanks Wysota.
What do you mean with "pollute the namespace"?

wysota
23rd October 2010, 17:12
What do you mean with "pollute the namespace"?
Try defining (in a different cpp file) a function called myInputEventFilter and taking a void* as input and you'll see for yourself.