[solved]Drag and drop between two QGraphicViews in single widget
-
Did you add the QGraphicsPixmapItem to the scene ?
Did you move it to a visible position ? -
Hi,
Yeah i have actually promoted the class to the QGraphicsView created using QTCreator.@
#include "gview.h"
#include<QGraphicsPixmapItem>
#include <QtGui>gview::gview(QWidget *parent)
: QGraphicsView(parent)
{
setMinimumSize(200, 200);
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
setAlignment(Qt::AlignCenter);
setAcceptDrops(true);
setAutoFillBackground(true);
clear();}
void gview::dragEnterEvent(QDragEnterEvent *event)
{
//s setText(tr("<drop content>"));
setBackgroundRole(QPalette::Highlight);
//const QMimeData *mimeData = event->mimeData();
//if (mimeData->hasImage()) {
// event->acceptProposedAction();
// update();
// } else {
event->setAccepted(true);
}//}
void gview::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}void gview::dropEvent(QDropEvent *event)
{
const QMimeData *mimeData=event->mimeData();
if(mimeData->hasImage()){
QGraphicsView *v = new QGraphicsView();
QGraphicsScene *scn = new QGraphicsScene(v);
// QPixmap pixmap= QPixmap::fromImage(qvariant_cast<QImage>(mimeData->imageData()));
// item->setPixmap(QPixmap::fromImage(qvariant_cast<QImage>(mimeData->imageData())));
item->setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
scn->addItem(item);
v->setScene(scn);
v->show();
// }
//else if (mimeData->hasHtml()) {
//s setText(mimeData->html());
//s setTextFormat(Qt::RichText);
// else if (mimeData->hasText()) {
//s setText(mimeData->text());
//s setTextFormat(Qt::PlainText);
// else if (mimeData->hasUrls()) {
// QList<QUrl> urlList = mimeData->urls();
//QString text;
//for (int i = 0; i < urlList.size() && i < 32; ++i) {
// QString url = urlList.at(i).path();
//text += url + QString("\n");
//return(text);
}
//} else {
//label->setText(tr("Cannot display data"));
//event->setAccepted(false);
//}
//event->acceptProposedAction();//}
void gview::dragLeaveEvent(QDragLeaveEvent *event)
{
// clear();
event->accept();
}void gview::clear()
{
// setText(tr("<drop content>"));
// setBackgroundRole(QPalette::Dark);emit changed();}
@
Please do guide me whats going wrong here.
-
You should not create a new scene and a new view each time you drop.
Just set a scene on your view after you created it and add your new item to that scene. -
Hi again,
But how to access the graphicsView which is the object of the @Widget::ui->graphicsView@ This cannot be accessed directly as its from a non- static member. It would be great if you could provide me the code how to use it in another class gview, for setting the scene in this view @ graphicsView and graphicsView2 @ Both are QGraphicView created using QTDesigner
Thanks and regards
Venkatesh Padmanabhan -
Create the scenes in Widget's constructor and set them your graphics views using setScene and then access them in your drop event using the scene() function
-
Hi,
Thanks for your valuable response. I have a problem, how to access the scenes declared in the constructor of WIDGET using scene() in drop event. I couldn't find a method to access those @ scn1 and scn @
declared in Widget using @ scene() @ -
You have to call it on the views you are implementing
-
I have created an object of class Widget in class gview to call upon @graphicsView1
graphicsView2
@ and created new scenes to accommodate it in gview, but it gets terminated when application starts to run.As you guided, how can I call upon the class using any other method?
I have tried @
Widget *wid = new Widget();
wid->ui->scn1->additem(item);
wid->ui->scn2->additem(item);
update();@
The item is the qgraphicspixmapitem, declared in gview as a pointer. I am not sure whether declaration is correct too@ QGraphicsPixmapItem *item; @
And used it for the variant_cast. I am not sure what's the mistake I am doing, as the application terminates as soon as I drop the image into the view.
-
Hi SGaint,
Atlast i was able to drag and drop the images into the qgraphicsview.I used
@ scene()->setPixmap(pixmap); @Thank you so much for your wonderful guidance.
Regards,
Venkatesh Padmanabhan. -
You're welcome
Don't forget to update the thread's title prepending solved so other forum users may know that a solution has been found :)
-
Hi,
I would like to ask one more query related to the original post. I am able to drag and drop images into the view now. But it doesn't work with Url's from local file, if i want to drag an image from the desktop into QGraphicsView it doesn't work as i think it is passed as Url not as an image i hope. It would be great help if you could suggest how to cast an Url into QPixmap... Awaiting your response. -
I'd rather answer on your other "post":http://qt-project.org/forums/viewthread/31764/