PDA

View Full Version : Perform an action when the cursor comes over a push button, WITHOUT CLICKING IT



learner_qt
4th February 2013, 06:29
I am working on openCV with Qt.. I am using my hand to move the cursor instead of the mouse. Can you pls help me with the following doubt:
I want to perform an action when the cursor comes over a push button BUT WITHOUT CLICKING IT. How do I do it?
Thanks in advance. :)

Santosh Reddy
4th February 2013, 06:50
Create a custom QPushButton, implement mouseMoveEvent ( QMouseEvent * event ), and also enable mouse tracking

Lykurg
4th February 2013, 08:02
As a small side note: Since mouseMoveEvent is triggered every time the mouse moves inside a widget I would prefer enterEvent in this case.

learner_qt
4th February 2013, 13:41
Thank you! :)

learner_qt
5th February 2013, 12:09
As a small side note: Since mouseMoveEvent is triggered every time the mouse moves inside a widget I would prefer enterEvent in this case.

Can you help me with the enterEvent implementation??
I am unable to proceed. :(
I dont know where to start.. :(

Lykurg
5th February 2013, 15:11
Subclass QPushButton and reimplement the enterEvent where you emit the clicked signal.

Look at the samples of the documentation, there you will find similar problems, where Qt classes where subclassed and event handlers reimplemented. If you have problems, show us what you have tried and we will go on.

learner_qt
7th February 2013, 07:52
Subclass QPushButton and reimplement the enterEvent where you emit the clicked signal.

Look at the samples of the documentation, there you will find similar problems, where Qt classes where subclassed and event handlers reimplemented. If you have problems, show us what you have tried and we will go on.

Thanks. Here is my code.
custom.h


#ifndef CUSTOM_H
#define CUSTOM_H

#include <QWidget>
#include <QPushButton>

namespace Ui {
class custom;
}

class Custom : public QPushButton
{
Q_OBJECT

public:
explicit Custom(QWidget *parent = 0);
~Custom();
void enterEvent(QEvent * );
void display();

private:
Ui::custom *ui;

public slots:
void display();
};

#endif // CUSTOM_H



custom.cpp


#include "custom.h"
#include "ui_custom.h"
#include <QMessageBox>
#include<QPushButton>

Custom::Custom(QWidget *parent) :
QPushButton (parent),
ui(new Ui::custom)
{
ui->setupUi(this);
}

void Custom::enterEvent(QEvent *)
{
Q_emit clicked();
}

void Custom::display()
{
QMessageBox msg;
msg.setText("CLICKED");
msg.exec();
}

Custom::~Custom()
{
delete ui;
}



It works when i click that push button. How do i do that event occurs when cursor comes over that particular push button (but without click)????
Also, i am able to do that message box is displayed when cursor comes over widget, but not over push button.
Where am i wrong??

Lykurg
7th February 2013, 08:22
It is Q_EMIT and you should leave enterEvent protected. And why the heck do you have a ui file?

class Custom : public QPushButton
{
Q_OBJECT

public:
explicit Custom(QWidget* parent = 0) : QPushButton(parent)
{
connect(this, SIGNAL(clicked()), this, SLOT(display()));
}
~Custom() {}

protected:
void enterEvent(QEvent*)
{
Q_EMIT clicked();
}

public slots:
void display()
{
setText("clicked");
}
};

learner_qt
7th February 2013, 09:57
I am taking input from camera, moving the mouse cursor through hand gesture, and have to perform a click like function when i keep my mouse cursor over the push button for 1500 ms. For now i am trying to see whether i can call an event (without clicking) when cursor comes over push button.
i need ui for this.

Lykurg
7th February 2013, 10:15
Yes, but you don't need it in a subclassed QPushButton!

learner_qt
7th February 2013, 10:50
Thank you for your quick replies!! :)
i did it and its working.! :)
the scenario is that the user sees multiple push buttons on the screen together with a live video feed from the camera.
and what i want to do is when he moves the cursor (through the gesture which i am tracking) to the button a specific event occurs.
pls help me through this.

learner_qt
8th February 2013, 13:59
Done. Thanks..