Results 1 to 6 of 6

Thread: built in dragging

  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question built in dragging

    Hi,
    I'm using Qt creator 4.7.
    I have a QWidget with some buttons and labels and a QtableWidget that I would like to be able to move around with the mouse.
    In the QTableWidget's properties menu I see some mouse and drag and drop related settings - (dragEnabled, dragDropMode etc'), so far changing their settings does not enable dragging.
    reading through the forums and tutorials I see that mostly the way of doing this is by manually creating a QDrag object, and reimplementing dragMoveEvent() ,dropEvent() and others (http://doc.qt.nokia.com/4.7-snapshot/dnd.html)

    My question - is there no built in way for this? what are the Widget's drag related properties for?
    Do I implement this on my own?
    thanks,
    Ido.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: built in dragging

    Those are the built-in drag and drop facilities. They control whether data can be dragged from one part of the program and dropped on other widgets, what data is available, and what is accepted (also to and from the program). This has nothing to do with moving widgets about on screen, which I expect you would have to do yourself with QWidget::mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), and explicit calls to position the widgets. If you have used the layout mechanisms then dragging widgets about to arbitrary positions is counter to the nature of layouts.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: built in dragging

    Note that QToolBar provides limited ability to reposition elements.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: built in dragging

    ... and QDockWidget also.

  5. #5
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Thumbs up Re: built in dragging

    Thanks for the swift and helpful answers.
    Ido.

  6. #6
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: built in dragging

    Just wanted to close this off with the two different solutions I eventually came up with:
    First option - use EvevntFilter()
    The code I used is based on the following reference:
    qt-fluid.zip

    Second option was to subclass a widget and rewrite its mouse event functions:
    Qt Code:
    1. class dispFrame : public QFrame
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit dispFrame(QWidget* parent = 0, Qt::WindowFlags f = 0);
    7. ~dispFrame(){}
    8.  
    9. protected:
    10. /* PIP frame*/
    11. int MouseX;
    12. int MouseY;
    13.  
    14. private:
    15. void mousePressEvent(QMouseEvent *);
    16. void mouseMoveEvent(QMouseEvent *);
    17. };
    18.  
    19. void dispFrame::mousePressEvent(QMouseEvent *evt)
    20. {
    21. MouseX = evt->x();
    22. MouseY = evt->y();
    23. }
    24.  
    25.  
    26. void dispFrame::mouseMoveEvent(QMouseEvent *evt)
    27. {
    28. int newX = this->x() - (MouseX - evt->x());
    29. int newY = this->y() - (MouseY - evt->y());
    30.  
    31. this->setGeometry(newX, newY, this->width(), this->height());
    32. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Painting during dragging
    By inktomi in forum Qt Programming
    Replies: 13
    Last Post: 2nd March 2011, 12:52
  2. Replies: 3
    Last Post: 11th May 2010, 05:50
  3. Widget dragging in MDI app
    By zeeeend in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2007, 22:11
  4. dragging
    By mickey in forum Newbie
    Replies: 1
    Last Post: 25th July 2006, 15:05
  5. dragging
    By mickey in forum Newbie
    Replies: 5
    Last Post: 11th March 2006, 01:26

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.