PDA

View Full Version : Drop QGraphicsView/QGraphicsScene big doubt



vcp
14th April 2010, 21:17
Hi,

When I do the first drop inside the QGraphicsView everything occurs correctly, but if I tried do another drop is not more accepted. Why this happens?

I think that the problem is in the "scene" but I don't know the concept behind this.

Thanks a lot.

See the code:



GraphicsView::GraphicsView( QWidget * parent )
: QGraphicsView(parent)
{
setAcceptDrops(true);
scene = new GraphicsScene;
}

GraphicsView::~GraphicsView()
{
delete scene;
}

void GraphicsView::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
}

void GraphicsView::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("plain/text")) {

QByteArray pieceData = event->mimeData()->data("plain/text");
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
QString string;
dataStream >> string;

qDebug() << "drop" << string;

setScene(scene);
QFont sansFont("Helvetica [Cronyx]", 12);
scene->addSimpleText(string,sansFont);


event->setDropAction(Qt::MoveAction);
event->accept();
} else {

event->ignore();
}
}

wysota
14th April 2010, 21:44
Why are you setting a new scene in the drop event?

vcp
15th April 2010, 12:21
Hi,

If I don't use setScene () the text doesn't appear in the view (QGraphicsView).
As I said, I think that I didn't understood very well the concept yet.

wysota
15th April 2010, 12:37
Did you set the scene on the view as part of setting up the view? From the code you posted I see you haven't. You should do so right after creating the scene. Your problem might be that you are dropping the item outside scene bounds (if you don't explicitly set the scene size it will stretch to encompass all items you place on it) but it's hard to say without knowing more. You should probably be handling the drops in the scene and not in the view, by the way.

vcp
15th April 2010, 12:51
Hi

I'm going do some tests and I give a feedback so soon be possible.

Thanks for while.

vcp
15th April 2010, 13:43
Please Wysota,

Compile my testing code for you to understand better my problem. and perhaps indicate the best way to solve it.

Download the sources from :ftp.consultecnica.com.br (ftp://ftp.consultecnica.com.br) the file dragdrop.zip

vcp
15th April 2010, 20:35
You should probably be handling the drops in the scene and not in the view, by the way.

Yes, this was the solution to do drag&drop works.

Now I understand how QListView and QGraphicsView+QGraphicsScene should work together to that the drag&drop work fine.