PDA

View Full Version : trouble when moving dynamically created custom Widgets



chocolateSteak
15th September 2013, 18:34
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

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();
}

}

the signal drag_move_done() is connected to a slot in WholeField`s constructor:

WholeField::WholeField(QWidget *parent) :
QWidget(parent),minimized(false)
{
.....
IdeaFieldButton *minimizeButton = new IdeaFieldButton(this,IdeaFieldButton::minimizeButt on);
IdeaFieldButton *moveButton = new IdeaFieldButton(this,IdeaFieldButton::moveButton);
connect(this->parentWidget(),SIGNAL(drag_move_done()),this,SLOT( drag_move() ) );
.....
}

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);
}

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...

amleto
16th September 2013, 00:38
read my sig.