Results 1 to 6 of 6

Thread: Drag the Widget

  1. #1
    Join Date
    Aug 2006
    Location
    Chennai, India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Drag the Widget

    Hi,

    I have QWidget. In that I set setWindowFlags ( Qt::FramelessWindowHint).
    I have Image which has designed like the QWidget ie) it has "Maxmize", "Minimize" buttons and Title bar also. I set this Image as my Background.
    Now I want to drag or move the Widget using Images Title bar. How can I do it?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag the Widget

    Reimplement the mouse events( press, move, release ) in your widget.
    You detect the drag and just use QWidget::move to move the widget. Be careful, you must use QMouseEvent::globalPos() when calculating the new position in order to avoid flicker.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag the Widget

    Something like this:
    Qt Code:
    1. void FramelessWidget::mousePressEvent( QMouseEvent* e )
    2. {
    3. if( pressed in title bar )
    4. {
    5. mOrigin = e->globalPos(); //mOrigin is a QPoint member
    6. }
    7. }
    8.  
    9.  
    10. void FramelessWidget::mouseMoveEvent( QMouseEvent* e )
    11. {
    12. if( e->buttons() & Qt::LeftButton ) //dragging
    13. {
    14. move( pos() + ( e->globslPos() - mOrigin ) );
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure this is going to work, so you may have to modify it a bit.

  4. The following user says thank you to marcel for this useful post:

    suresh (26th May 2007)

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag the Widget

    Of course, to avoid all this, you could have created your own style, in which you override everything related to title bars, derived from one of the existing ones.

    This way, you wouldn't have had to worry about moving, resizing, how it will behave on other platforms, etc.

  6. #5
    Join Date
    Aug 2006
    Location
    Chennai, India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag the Widget

    Thanks for your reply. I will try this

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag the Widget

    Quote Originally Posted by marcel View Post
    Of course, to avoid all this, you could have created your own style, in which you override everything related to title bars, derived from one of the existing ones.
    The title bar of a window is not handled by Qt but the underlying window system. Unfortunately you cannot alter the looks of a title bar with a custom style except for mdi/workspace child windows.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    marcel (26th May 2007)

Similar Threads

  1. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  2. Controlling which widget on top layer?
    By JonathanForQT4 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2007, 14:27
  3. Drag Drop between Different Views
    By aamer4yu in forum Qt Programming
    Replies: 13
    Last Post: 8th December 2006, 04:29
  4. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.