PDA

View Full Version : Is there a way to add a mouse event to a thread?



AO1114
27th May 2018, 15:12
I tried the following after defining Clicked as a QMouseEvent in the header file:
[
Clicked = new QMouseEvent(this);
connect(Clicked, SIGNAL(mousePressEvent()), this, SLOT(MouseClicked()));
timer->moveToThread(&MouseThread);
MouseThread.start();
]

I defined mousePressEvent() n the header under signals and MouseClicked() under slots. I also wrote the MouseClicked as a void function:

[
void Game::MouseClicked()
{
lbl->setText("Works");
}

]

A label was defined to give me feedback if the thread worked.
But I get errors when implementing this as it would seem as if you can't use QMouseEvent in connect

d_stranz
27th May 2018, 20:57
There are so many things wrong here, it's hard to know where to start.

First, mousePressEvent() is not a signal, it is a protected virtual member function of QWidget that is called whenever the user presses a mouse button while inside the widget. If you derive from (or use a class derived from) QWidget, you will likely override the QWidget implementation with your own method if you need to handle this event in a custom way.

Second, the QMouseEvent class inherits from the QEvent base class, which -does not- inherit from QObject, so it can't emit signals in the first place. So defining a "Clicked" instance of QMouseEvent and trying to connect to a non-existent "mousePressEvent" signal of the QMouseEvent class will never work.

Third, I don't think you understand the basics of how signals and slots work.

Fourth, I have no idea what you are trying to do with a timer and whatever MouseThread is, or why you think you even need a thread to handle mouse clicks in your game.

Why don't you start by reading the Qt Signals and Slots tutorial (https://doc.qt.io/qt-5/signalsandslots.html), then move on to the Threading Basics tutorial (https://doc.qt.io/archives/qt-5.10/thread-basics.html), and read Maya Posch's excellent blog post (https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/) for dessert.