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
{
if (object == pushButton)
{
if(ev
->type
() == QEvent::Enter) {
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
...
...
MainWindow::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
pushButton->installEventFilter(this);
}
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);
}
To copy to clipboard, switch view to plain text mode
I set a type of cursor because i wanted to see it is working.
.h
...
protected:
...
...
protected:
bool eventFilter(QObject *object, QEvent *ev);
...
To copy to clipboard, switch view to plain text mode
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:
...
...
protected:
void enterEvent(QEvent *ev);
void leaveEvent(QEvent *ev);
...
To copy to clipboard, switch view to plain text mode
In cpp file:
...
void FlatButton
::enterEvent(QEvent *ev
) {
}
void FlatButton
::leaveEvent(QEvent *ev
) {
}
...
...
void FlatButton::enterEvent(QEvent *ev)
{
QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
}
void FlatButton::leaveEvent(QEvent *ev)
{
QApplication::restoreOverrideCursor();
}
...
To copy to clipboard, switch view to plain text mode
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.
Bookmarks