PDA

View Full Version : The drag and moving problem??



Ray
26th September 2006, 12:29
I am planning to build a tool bar like Dr.eye,when using the Qt's example D&D ,I set set the program like this:


MainWidget::MainWidget(QWidget *parent) : QLabel(parent)
{
setWindowState(this->windowState() ^ Qt::WindowFullScreen);
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
setAcceptDrops(true);

toolbar = new ToolWidget(this);
toolbar->move(50, 50);
toolbar->show();
toolbar->setAttribute(Qt::WA_DeleteOnClose);
}

void MainWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("MyToolBar"))
{
if (event->source() == this)
{
event->setDropAction(Qt::MoveAction);
event->accept();
} else
{
event->acceptProposedAction();
}
}
else
{
event->ignore();
}
}

void MainWidget::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat("MyToolBar"))
{
if (children().contains(event->source()))
{
event->setDropAction(Qt::MoveAction);
event->accept();
}
else
{
event->acceptProposedAction();
}
printf("this is the test\n");
}
else
{
event->ignore();
}
}

void MainWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("MyToolBar"))
{
QByteArray itemData = event->mimeData()->data("MyToolBar");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);

QPixmap pixmap;
QPoint offset;
dataStream >> pixmap >> offset;

ToolWidget *Transfer = new ToolWidget(this);
Transfer->setPixmap(pixmap);
Transfer->move(event->pos() - offset);
Transfer->show();
Transfer->setAttribute(Qt::WA_DeleteOnClose);

if (event->source() == this)
{
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
}
else
{
event->ignore();
}
}

void MainWidget::mousePressEvent(QMouseEvent *event)
{
if(Qt::LeftButton == event->button ())
{
ToolWidget *child = static_cast<ToolWidget*>(childAt(event->pos()));
if (!child)
return;

QPixmap pixmap = *child->pixmap(); //get the pixmap on the label


QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << pixmap << QPoint(event->pos() - child->pos());

QMimeData *mimeData = new QMimeData;
mimeData->setData("MyToolBar", itemData);
child->hide();

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(event->pos() - child->pos());

if (drag->start(Qt::MoveAction) == Qt::MoveAction)
child->close();
else
child->show();
}
}

this is following the example,but I create a class ToolWidget,
it contains several button's。
the question is,when i drag and move the toolbar, all of the object except toolbar'r 's icon
are invisiable
is it possible make the label visible,and how to do it??

wysota
28th September 2006, 07:18
What does ToolWidget::pixmap() return? You set the resulting pixmap as the drag indicator. Maybe the pixmap doesn't contain what you want?

Ray
28th September 2006, 08:04
I found it is useful to use the: QPixmap pixmap = QPixmap::grabWidget(toolbar,toolbar->frameRect());
and i got the pixmap to all of the toolbar,include all of the objects in the toolbar.
so, when i move the toolbar it present all of the images in toll bar but not part of the toolbar.

wysota
28th September 2006, 08:11
But is the pixmap grabbed correct or not? Can you show a picture of the pixmap and a picture of the toolbar which you want to achieve?

Ray
28th September 2006, 09:29
the toolbar that i need is like the Dr.eye's toolbar,i found the page :
http://www.twinbridge.com/Products/Dictionary/DrEye/Dr70_chn.htm

there is another question,is it possible to set the toolbar widegt on the Z-plane
because the toolbar is not allowed to be coverd by other objects,thx.

jpn
28th September 2006, 09:32
there is another question,is it possible to set the toolbar widegt on the Z-plane
because the toolbar is not allowed to be coverd by other objects,thx.
QWidget::raise()

wysota
28th September 2006, 09:43
Still, what exactly you managed to achieve up to this point?

Ray
28th September 2006, 10:32
QWidget::raise()


is doesn't work!!
my tool bar is not any other widegt's child,and almost all of the object are indepednant,
is there any other way to achieve the goal??

wysota
28th September 2006, 10:37
You probably want to use Qt::WindowStaysOnTopHint window flag

jpn
28th September 2006, 10:43
Sorry, I haven't been following the thread too closely, but are you using drag'n'drop just for moving the toolbar?

Ray
28th September 2006, 10:57
You probably want to use Qt::WindowStaysOnTopHint window flag

toolbar->setWindowFlags(Qt::WindowStaysOnTopHint);

but it doesn't work, other widgets still cover the toolbar@@
does the script right??
or any other suggustion??