PDA

View Full Version : do something while a button is pressed



saman_artorious
10th November 2013, 12:09
I am willing to regularly call an instruction while the left mouse is pressed on a button. I tried to do this with pressed or clicked and taking care of the mouse left button by defining a QMouseEvent inside of the slot.


if (event->buttons() & Qt::LeftButton)
{

there is also no way to pass the qmouseevent as a parameter of clicked or pressed. how can I solve this?

anda_skoa
10th November 2013, 14:30
If you need to check the event itself, you'll have to overwrite mousePressEvent()

Cheers,
_

blue_sky
10th November 2013, 17:42
You have to implement mouse press event as well as click slot.If you want the same functionality, then call the same function from both.

i.e=

slot_btn_clkd()
{
taskMethod();
}
keyPressEvent(QKeyEvent* event)
{
if( event->key() == Qt::Key_Up ){ taskMethod(); }
else event->accept();
}

taskMethod()
{

//Do stuffs here
}

saman_artorious
11th November 2013, 09:24
You have to implement mouse press event as well as click slot.If you want the same functionality, then call the same function from both.

i.e=

slot_btn_clkd()
{
taskMethod();
}
keyPressEvent(QKeyEvent* event)
{
if( event->key() == Qt::Key_Up ){ taskMethod(); }
else event->accept();
}

taskMethod()
{

//Do stuffs here
}

Negative. Think about where you saying wrong.

Added after 6 minutes:


If you need to check the event itself, you'll have to overwrite mousePressEvent()

Cheers,
_

Yes, I need to overwrite it. But, I need to tell it a scenario in which mouse left button is pressed and also the very button is also clicked. I do not know how to mention button click from inside mousepressevent. However, there are some ways to do that, but I don't consider them the main solutions.

anda_skoa
11th November 2013, 11:21
I am not sure what the problem is, all mouse events got through the mouse event handler methods.

Maybe you could describe what happens, i.e. what is the order of user interactions you need to discover.

Cheers,
_

saman_artorious
11th November 2013, 12:06
I am not sure what the problem is, all mouse events got through the mouse event handler methods.

Maybe you could describe what happens, i.e. what is the order of user interactions you need to discover.

Cheers,
_

imagine I have a few button. How would you identify which button was clicked inside mousePressEvent? otherwise, if I click any mouse event the event would trigger for any buttons clicked!

anda_skoa
11th November 2013, 19:21
imagine I have a few button. How would you identify which button was clicked inside mousePressEvent?

I am afraid I still don't understand what you are trying to do.
Each button has its own mousePressEvent handler, so it is obvious which one received the mouse event.

Also, each button has its own clicked signal, so you can determined which one was clicked even without custom event handling.

Cheers,
_

saman_artorious
11th November 2013, 19:51
I am afraid I still don't understand what you are trying to do.
Each button has its own mousePressEvent handler, so it is obvious which one received the mouse event.

Also, each button has its own clicked signal, so you can determined which one was clicked even without custom event handling.

Cheers,
_

but there is only one mousePressEvent() and that is for my MainWindow class. If I write my function inside mousePressEvent(), it is triggered each time user hold the left mouse button. But, this event occurs for every click on the ui! I want it to be only for one button. while that button is clicked and the mouse left button is held, do the task.
blue_sky suggested to call the task function both in the click signal SLOT() and also in the mousePressEvent. But this way is wrong, because you have no idea which button was clicked and for every mouse left button event the task is executed.

stampede
11th November 2013, 20:29
I don't know if I understand correctly, but since by default you can press the QPushButton only with left mouse button, try:


connect(btn, SIGNAL(pressed()), obj, SLOT(startWork()));
connect(btn, SIGNAL(released()), obj, SLOT(stopWork()));

in order to protect yourself from the scenario:
- user clicked left, but not yet released
- mouse pointer left the widgets boundaries while user is holding the btn down
you can reimplement button's leaveEvent and stop the work there too.
I don't really remember if the released() wouldn't be called anyway if mouse pointer is outside the button, but that's quite easy to test.

anda_skoa
12th November 2013, 12:00
but there is only one mousePressEvent() and that is for my MainWindow class.

No.
mousePressEvent() is a virtual method of QWidget, not just of QMainWindow. All widgets have that one. How do you think QPushButton is implementing mouse clicks?



If I write my function inside mousePressEvent(), it is triggered each time user hold the left mouse button. But, this event occurs for every click on the ui! I want it to be only for one button.


Might make sense to implement the mousePressEvent of the widget where you want to detect it, no?

Cheers,
_

saman_artorious
12th November 2013, 12:32
so, for that I need to first define a class inheriting a qpushbutton, and declare my qpushbutton of that type, then re implement the qMouseEvent inside my class for the button, right?

anda_skoa
12th November 2013, 16:39
Yes, that would be the cleanest option.

Cheers,
_