In my project I give the user a button that allows him to dynamically drop-add textfields on an area. Each textfield can be moved around via a drag started with a moveButton, also contains a minimizeButton. My solution works fine if there is one textfield. Both buttons work. If there are multiple textfields, each time I move one, the others disappear and it also causes the minimizeButton`s redraw to be drawn on top of the textfield without deleting the old drawing.
the textfield is called WholeField: inherits from QWidget, contains QTextEdit and a moveButton(custom,inherits QAbstractButton) to move the WholeField around via drag and a minmizeButton that hides the QTextEdit and causes a redraw to happen
the area: called IdeaPlane: inherits QWidget, serves as a background, accepts drops
{
if (event->mimeData()->hasFormat("moveWholeField"))
{
qDebug("dropEvent-moveWholeField entered");
event->setDropAction(Qt::MoveAction);
event->accept();
//variable to store the drop`s position
dropPos = event->pos(); //dropPos is QPoint, a public member of IdeaPlane
emit drag_move_done();
}
else
{
event->ignore();
}
}
void IdeaPlane::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("moveWholeField"))
{
qDebug("dropEvent-moveWholeField entered");
event->setDropAction(Qt::MoveAction);
event->accept();
//variable to store the drop`s position
dropPos = event->pos(); //dropPos is QPoint, a public member of IdeaPlane
emit drag_move_done();
}
else
{
event->ignore();
}
}
To copy to clipboard, switch view to plain text mode
the signal drag_move_done() is connected to a slot in WholeField`s constructor:
WholeField
::WholeField(QWidget *parent
) :{
.....
IdeaFieldButton *minimizeButton = new IdeaFieldButton(this,IdeaFieldButton::minimizeButton);
IdeaFieldButton *moveButton = new IdeaFieldButton(this,IdeaFieldButton::moveButton);
connect(this->parentWidget(),SIGNAL(drag_move_done()),this,SLOT(drag_move() ) );
.....
}
WholeField::WholeField(QWidget *parent) :
QWidget(parent),minimized(false)
{
.....
IdeaFieldButton *minimizeButton = new IdeaFieldButton(this,IdeaFieldButton::minimizeButton);
IdeaFieldButton *moveButton = new IdeaFieldButton(this,IdeaFieldButton::moveButton);
connect(this->parentWidget(),SIGNAL(drag_move_done()),this,SLOT(drag_move() ) );
.....
}
To copy to clipboard, switch view to plain text mode
that slot finds the pointer to the IdeaPlane via a call to parentWidget, casts it to the IdeaPlane type and then accesses the drop position to move the corresponding WholeField accordingly.
void WholeField::drag_move()
{
IdeaPlane *plane = qobject_cast<IdeaPlane *>(this->parentWidget() );
this->move(plane->dropPos);
}
{
IdeaPlane *plane = qobject_cast<IdeaPlane *>(this->parentWidget() );
this->move(plane->dropPos);
}
To copy to clipboard, switch view to plain text mode
Why would this not work with multiple WholeFields added but work with one? How do I fix this?
I have been stuck with this for a day...
Bookmarks