cannot call member function without object
I get the following error when trying to connect two classes together:
Code:
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:
Code:
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:
Code:
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.
Re: cannot call member function without object
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:
Code:
QApp *app = qobject_cast<QApp>(qApp);
app->mouseCoords(...);
BTW: Is that mouseCoord() or mouseCoords() ? You aren't consistent.
Re: cannot call member function without object
If I do this:
Code:
QApp *app = qobject_cast<QApp>(qApp);
app->mouseCoords(...); // what is this exactly? Am I not supposed to use connect()?
Then what happens to this?
Code:
QApp a(argc, argv);
Thanks for the help.
Re: cannot call member function without object
I would ask first why did you make your QApp::eventFilter() method static?
Re: cannot call member function without object
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:
Code:
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*)
Re: cannot call member function without object
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?
Re: cannot call member function without object
I need QAbstractEventDispatcher to be able to get WM_INPUT messages because winEvenFilter won't do it.
Quote:
EDIT:
if I remove "static" from eventFilter I get this error message:
Code:
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*)
Re: cannot call member function without object
Can we see the exact code?
Re: cannot call member function without object
Yes. Here it is:
main.cpp:
Code:
#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:
Code:
extern "C"{
#include "raw_mouse.h"
}
#include "qapp.h"
#include <QDebug>
#include <QAbstractEventDispatcher>
#define WM_INPUT 0x00FF
QApp::QApp(int & argc, char ** argv) :
{
qDebug() << init_raw_mouse(1, 0, 1);
qDebug() << raw_mouse_count();
}
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:
Code:
#ifndef QAPP_H
#define QAPP_H
#include <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
Re: cannot call member function without object
Why not do it this way?
Code:
bool myInputEventFilter(void*);
Q_OBJECT
friend bool myInputEventFilter(void *);
public:
init_raw_mouse(1, 0, 1);
}
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;
}
Re: cannot call member function without object
Great! Working perfectly now. Thanks Wysota.
What do you mean with "pollute the namespace"?
Re: cannot call member function without object
Quote:
Originally Posted by
been_1990
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.