Results 1 to 2 of 2

Thread: Is there a way to add a mouse event to a thread?

  1. #1
    Join Date
    May 2018
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Is there a way to add a mouse event to a thread?

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,997
    Thanks
    232
    Thanked 632 Times in 622 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Is there a way to add a mouse event to a thread?

    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, then move on to the Threading Basics tutorial, and read Maya Posch's excellent blog post for dessert.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Event Filter Mouse Press/Release Event not called
    By Alundra in forum Qt Programming
    Replies: 3
    Last Post: 22nd September 2016, 00:59
  2. Ogre+qt mouse event (add object with mouse problem)
    By rimie23 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2012, 10:49
  3. Replies: 14
    Last Post: 17th January 2012, 09:01
  4. Replies: 3
    Last Post: 7th January 2012, 08:38
  5. Mouse Event - source of event?
    By PowerPlate in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2011, 09:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.