PDA

View Full Version : built in dragging



idoroth
11th September 2011, 16:29
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.

ChrisW67
12th September 2011, 00:12
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.

SixDegrees
12th September 2011, 00:16
Note that QToolBar provides limited ability to reposition elements.

ChrisW67
12th September 2011, 01:15
... and QDockWidget also.

idoroth
12th September 2011, 11:57
Thanks for the swift and helpful answers.
Ido.

idoroth
21st September 2011, 09:37
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:
6870

Second option was to subclass a widget and rewrite its mouse event functions:


class dispFrame : public QFrame
{
Q_OBJECT

public:
explicit dispFrame(QWidget* parent = 0, Qt::WindowFlags f = 0);
~dispFrame(){}

protected:
/* PIP frame*/
int MouseX;
int MouseY;

private:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
};

void dispFrame::mousePressEvent(QMouseEvent *evt)
{
MouseX = evt->x();
MouseY = evt->y();
}


void dispFrame::mouseMoveEvent(QMouseEvent *evt)
{
int newX = this->x() - (MouseX - evt->x());
int newY = this->y() - (MouseY - evt->y());

this->setGeometry(newX, newY, this->width(), this->height());
}