Results 1 to 5 of 5

Thread: Moving a QButton around the form using the mouse

  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Moving a QButton around the form using the mouse

    I posted this in the newbies section as thats what I am, but since theres no replies there I'm assuming maybe its a more advanced question or maybe just people don't understand what I'm asking.

    I'm a Windows programmer, so I'm used to working with Windows messages and MFC.

    What I want to do, basically: An single window app with a QPushButton on it. You run the app and you can move the button around the form by clicking the left mouse button on the QPushButton and moving the mouse around. Once you release the mouse button, the QPushButton no longer cares about the mouse position.

    I don't expect someone to write this for me, just some hints or advice is fine.

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving a QButton around the form using the mouse

    Hm, you could reimplement the QPushButtons mouseMoveEvent and call setGeometry or move with the events position.
    But I never tried anything like that, it's just an idea.

    Another possibility would be to reimplement mousePressEvent, start a drag with a pixmap of the button, hide the button and reimplement dropEvent to move it to the dropEvents position.

  3. The following user says thank you to ChiliPalmer for this useful post:

    squidge (29th September 2009)

  4. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving a QButton around the form using the mouse

    Widgets / buttons are usually handled by the layout.
    But since you need control over it, you shouldnt set it in layout.
    Next step would be to override drag events and set the position of the widget/button according to the mouse moved.
    Look more into the drag events, or how to start a drag for a widget.

    You may even refer code of designer !

  5. #4
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving a QButton around the form using the mouse

    Once upon a time I was interested in creating just what you're describing. I've got ready code on #qt IRC channel which you can find attached to this post. Good luck!
    Attached Files Attached Files

  6. The following 2 users say thank you to piotr.dobrogost for this useful post:

    George Neil (1st October 2009), squidge (29th September 2009)

  7. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving a QButton around the form using the mouse

    Thanks, you described it how I had done it before using just Windows code, and then piotr.dobrogost posted example code using the same method. Perfect!

Similar Threads

  1. Replies: 6
    Last Post: 2nd March 2018, 04:39
  2. Displaying the Icons name,while moving mouse on Icon
    By yuvaraj.yadav in forum Qt Programming
    Replies: 3
    Last Post: 22nd April 2009, 20:17
  3. Moving plot marker with the mouse
    By viridis in forum Qwt
    Replies: 2
    Last Post: 23rd September 2008, 20:39
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13

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.