PDA

View Full Version : Event filter with mouse events



felo188
20th July 2011, 14:37
Hi everyone.
Firstly sorry for my english.

I have some problem with qt so i decidied to ask You for help.

I'writting a program. I want to do somethink like this:
- when i move mouse on button, for example, should show table with data like name.
The main difficult to me is that the button is on MainWindow and if i want to show new table when the mouse is on button i must create new class or somethink like this.

I rode a lot about QMouseEvent and event filter but i stuck i don't know what i should do next.

1. Firts way i used event filter. Some code:.cpp
bool MainWindow::eventFilter(QObject *object, QEvent *ev)
{
if (object == pushButton)
{
if(ev->type() == QEvent::Enter)
{
QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
return true;
}
else
{
return false;
}
}
else
{
QApplication::restoreOverrideCursor();
return false;
}
}
...
...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pushButton->installEventFilter(this);
}
I set a type of cursor because i wanted to see it is working.
.h
...
protected:
bool eventFilter(QObject *object, QEvent *ev);
...
This method isn't working. When I press Run button the application crash.

2.The second way is use EnterEvent and LeaveEvent. For that i create a new .h file and called him FlatButton when i declared:
.h
...
protected:
void enterEvent(QEvent *ev);
void leaveEvent(QEvent *ev);
...
In cpp file:

...
void FlatButton::enterEvent(QEvent *ev)
{
QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
}
void FlatButton::leaveEvent(QEvent *ev)
{
QApplication::restoreOverrideCursor();
}
...
When i replace FlatButton:: on MainWindow:: it's working but the cursor change type when enter or live MainWindow but i want to change him only on button. For this reason i created FlatButton.

I don't know where i do wrong. I would be grateful for help.

Furkan
20th July 2011, 15:17
Why don't you try this?

In enterEvent

this->enterEvent(ev);

In leaveEvent

this->leaveEvent(ev);

high_flyer
20th July 2011, 15:32
When I press Run button the application crash.
Where does it crash? on what line?

felo188
20th July 2011, 20:22
Why don't you try this?

In enterEvent

this->enterEvent(ev);

In leaveEvent

this->leaveEvent(ev);
Where should i paste it? I paste it
void FlatButton::enterEvent(QEvent *ev)
{
this->enterEvent(ev);
QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
}
void FlatButton::leaveEvent(QEvent *ev)
{
this->leaveEvent(ev);
QApplication::restoreOverrideCursor();
}But it doesn't works.

Where does it crash? on what line?
When i press Run the program compile with any errors or warnings but shows a Windows message box somethink like this one http://edu.i-lo.tarnow.pl/inf/hist/002_gui/images/win7crash.png. I think it's depend on
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pushButton->installEventFilter(this);

}When the pushButton->installEventFilter(this) is not enabled the crash doesn't appear.

high_flyer
21st July 2011, 09:24
but shows a Windows message box somethink like this one
The windows manager crashes when you start debugging??
Then something is very wrong with your system!

felo188
21st July 2011, 09:45
The windows manager crashes when you start debugging??
Then something is very wrong with your system!
It's only crash when the pushButton->installEventFilter(this) is enabled. After crash system working normally. Only crash MyProgramInQT.exe not a Windows.

high_flyer
21st July 2011, 09:52
Only crash MyProgramInQT.exe not a Windows.
The screenshot you posted says your window manager crashed, so either its not the message you get, or, as I said, your windows managers is crashing.

felo188
21st July 2011, 10:34
shows a Windows message box somethink like this I mean message box only looking like this one. My message says that MyProgramInQT.exe stops working not a system Windows :)

high_flyer
21st July 2011, 10:55
I mean message box only looking like this one. My message says that MyProgramInQT.exe stops working not a system Windows
What is the point of not giving the exact information? do you want help or not?:mad:
Sending people on the wrong track will not help you!
Are you sure your project is in debug mode?
It sounds like you are starting a release mode project.
Make sure you are in debug mode, and start the debugger, it will then stop on the line that is crashing.
Post that line here, and then we can start seeing where the problem is.

felo188
21st July 2011, 12:03
Sorry for misleading.

When i click Start Debugging (F5) debug stops at this (http://imageshack.us/photo/my-images/696/windowq.png/)
The code from Compile Output:

Running build steps for project Mysz...
Configuration unchanged, skipping qmake step.
Starting: "E:\Programy\QtSDK\mingw\bin\mingw32-make.exe"
E:/Programy/QtSDK/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Users/Lukasz/Desktop/Mysz-build-desktop'
mingw32-make[1]: Nothing to be done for `first'.
mingw32-make[1]: Leaving directory `C:/Users/Lukasz/Desktop/Mysz-build-desktop'
The process "E:\Programy\QtSDK\mingw\bin\mingw32-make.exe" exited normally.

high_flyer
21st July 2011, 12:49
please stop post external links with screenshots.
Attach the screenshots to the post, since after some time the external links stop being valid, and the post loses information, which might be important for people reading this in later time.
Also, for posting a section of code you can simple cope the code and paste it here with [code] tags.

Where is 'pushButton' defined, and where do you initialize it?

felo188
22nd July 2011, 09:12
It's a screenshot 6703.

I didn't have a defined pushButton so i added it:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *pushButton = new QPushButton("button1",this);
pushButton->installEventFilter(this);
}Now the program stops crashing.
I'm wondering why the event filter don't working.
bool MainWindow::eventFilter(QObject *object, QEvent *ev)
{
if ((object == pushButton) && ev->type() == QEvent::Enter)
{
QApplication::setOverrideCursor(QCursor(Qt::CrossC ursor));
return true;
}
if ((object == pushButton) && ev->type() == QEvent::Leave)
{
QApplication::restoreOverrideCursor();
return true;
}
else
return false;
}When the cursor is on button he should change a type to cross but he don't change.

high_flyer
22nd July 2011, 10:00
I didn't have a defined pushButton so i added it:
Thats is not true.
If there was not a defined 'pushButton' you would have gotten a compile error.
Which is also the reason your event handler "is not working".
You have a defined a local 'pushButton' in your constructor, which is being initialized, but the 'pushButton' in your evnetFilter is declared somewhere else, probably in your header in class scope, but it is not initialized, hence it never matcher your if() statements.
It seems you have no idea how classes in C++ work, and what are members.
Hence, this is becoming off topic for this forum.

felo188
22nd July 2011, 10:57
I solved my problem.
.h
...
QPushButton *pushButton;
....and .cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pushButton = new QPushButton("button1",this);
pushButton->setGeometry(50,45,60,25);
pushButton->installEventFilter(this);
}high_flyer anyway thanks for help.