Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting problem in drag and drop of thumbnail image inside the image view?



  • I want to use drag and drop event inside my project. Where I have a list widget as( Thumbnails). and I want to drag an image from the thumbnail and drop inside the grid layout as (QVTKOpenGlWidget). I will make a file QOpenGLWidgetDrag.cpp and inherit this QOpenGlWidget.cpp.

    QOpenGLWidgetDrag.h

    class QvtkOpenGLWidgetdrag : public QOpenGLWidget
    {
    	Q_OBJECT
    public:
    	explicit QvtkOpenGLWidgetdrag(QWidget *parent = nullptr);
    
    protected:
    	void dropEvent(QDropEvent *event) override ;
    	//QvtkOpenGLWidgetdrag * New();
    	void dragEnterEvent(QDragEnterEvent *event) override;
    	void paintGL() override;
    
    private:
    	QPixmap m_pixmap;
    };
    

    QOpenGLWidgetDrag.cpp

    void QvtkOpenGLWidgetdrag::dragEnterEvent(QDragEnterEvent *event)
    {
    	QOpenGLWidget::dragEnterEvent(event);
    	event->accept();
    }
    
    void QvtkOpenGLWidgetdrag::dropEvent(QDropEvent *event)
    {
    	const QMimeData *mimeData = event->mimeData();
    
    	if (mimeData->hasFormat("application/x-qabstractitemmodeldatalist")) {
    		QByteArray encoded = mimeData->data("application/x-qabstractitemmodeldatalist");
    		QDataStream stream(&encoded, QIODevice::ReadOnly);
    
    		while (!stream.atEnd()) {
    			int row;
    			int col;
    			QMap<int, QVariant> roleDataMap;
    
    
    			stream >> row >> col >> roleDataMap;
    
    			// QString name = roleDataMap.value(0).toString();
    			QIcon icon = roleDataMap.value(1).value<QIcon>();
    
    			m_pixmap = icon.pixmap(icon.availableSizes().first());
    
    			update();
    		}
    	}
    	else {
    		event->ignore();
    	}
    }
    
    void QvtkOpenGLWidgetdrag::paintGL()
    {
    	QPainter painter(this);
    
    	painter.drawPixmap(500, 250, m_pixmap);
    }
    

    I will ask the same question in stack overflow also. link:-https://stackoverflow.com/questions/51966584/qt-drag-and-drop-image-from-thumbnail-to-gridlayout-qvtkopenglwidget

    But this one code not reflecting anything inside my project.

    Please help me

    Thanks in advance



  • I think you can get idea from this sample code. its works on drag and drop...

    QListWidget *listWidget = new QListWidget;
    listWidget->setAcceptDrops(true);
    listWidget->setDragEnabled(true);
    listWidget->setDragDropMode(QAbstractItemView::InternalMove);
    listWidget->setDropIndicatorShown(true);
    listWidget->setUpdatesEnabled(true);

    QStringList img_list; // list of path of all images
    img_list<<":/images/red.jpg";
    img_list<<":/images/green.jpg";
    
    for(int i=0;i<img_list.size();i++)
    {
        QListWidgetItem *item=new QListWidgetItem;
        item->setIcon(QIcon(img_list.at(i)));
        listWidget->insertItem(i,item);
        listWidget->setCurrentRow(i);
    }
    listWidget->show();


  • @anil_arise Sorry I m not getting this one code



  • @amarism you checked it or not this Git code



  • @anil_arise this one working inside the QT but when the same thing i will apply inside my project it will not working.
    So, Now my question is can i write this one code inside the my mainwindow.cpp file insist of making new file .



  • @amarism your code is working just change this line
    painter.drawPixmap(500, 250, m_pixmap); beccause out of range
    use in widget range
    painter.drawPixmap(100, 100, m_pixmap);



  • @anil_arise still this one not working



  • @amarism can you show your widget structure. where are you design.?



  • @anil_arise I have added new file as QvtkOpenGLWidgetDrag.cpp/.h and inherit the QOpenGLWidget and i will displaying everything through mainwindiw.cpp. So, I will add the drag class inside the mainwindow.h. But were i will calling this file QvtkOpenGLWidgetDrag.cpp/.h throung mainwindow.cpp



  • @amarism when your are create and initialize your widget in mainwindow.cpp

    example
    auto *viewTopLeft = new OpenGlWidget(this);
    layoutGrid->addWidget(viewTopLeft, 0, 0);



  • @anil_arise I will directly create view port through Qt. statistically creating port not dynamically



  • @amarism @amarism it should be create dynamically. and add to layout



  • @anil_arise everything i am take as a static inside the QT so how i am going to create dynamically on view port. inside the view port i am added slider also



  • @amarism Again I said ,create and initialization of these 4 widgets you can use dynamically in your project. you can also add slider dynamically.



  • @anil_arise Can i do the same thing in statically or not



  • @amarism No , How can you connect your statically created OpenGlWidget to dynamically created OpenGlWidget class..
    try this mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <openglwidget.h>
    #include <QSlider>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    auto *viewTopLeft = new OpenGlWidget(this);
    auto *viewTopRight = new OpenGlWidget(this);
    auto *viewBottomLeft = new OpenGlWidget(this);
    auto *viewBottomRight = new OpenGlWidget(this);

    ui->listWidget->addItem(new QListWidgetItem(QIcon(":/images/red.jpg"), tr("Item 1")));
    ui->listWidget->addItem(new QListWidgetItem(QIcon(":/images/green.jpg"), tr("Item 2")));
    ui->listWidget->addItem(new QListWidgetItem(QIcon(":/images/stop.png"), tr("Item 3")));
    ui->listWidget->setViewMode(QListWidget::IconMode);
    ui->listWidget->setIconSize(QSize(50, 50));
    ui->listWidget->setGridSize(QSize(65, 65));
    ui->listWidget->setMaximumWidth(100); //already set by statically
    
    
    QSlider *slider1=new QSlider;
    slider1->setRange(0,100);
    slider1 ->setOrientation(Qt::Horizontal);
    QSlider *slider2=new QSlider;
    slider2->setRange(0,100);
    slider2 ->setOrientation(Qt::Horizontal);
    QSlider *slider3=new QSlider;
    slider3->setRange(0,100);
    slider3 ->setOrientation(Qt::Horizontal);
    QSlider *slider4=new QSlider;
    slider4->setRange(0,100);
    slider4 ->setOrientation(Qt::Horizontal);
    
    ui->gridLayout->addWidget(viewTopLeft, 0, 0);
    ui->gridLayout->addWidget(viewTopRight, 0, 1);
    ui->gridLayout->addWidget(slider1, 1, 0);
    ui->gridLayout->addWidget(slider2, 1, 1);
    ui->gridLayout->addWidget(viewBottomLeft, 2, 0);
    ui->gridLayout->addWidget(viewBottomRight, 2, 1);
    ui->gridLayout->addWidget(slider3, 3, 0);
    ui->gridLayout->addWidget(slider4, 3, 1);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }



  • @anil_arise This one all are there in my ui just can i write only drag n drop event



  • @amarism just replace your static openGLWidget with thisss

    auto *viewTopLeft = new OpenGlWidget(this);
    auto *viewTopRight = new OpenGlWidget(this);
    auto *viewBottomLeft = new OpenGlWidget(this);
    auto *viewBottomRight = new OpenGlWidget(this);



  • @anil_arise But how i will set the position for every view port



  • @amarism by using LAYOUT



  • @anil_arise Sir i am try to change . Not able place properly OpenGLWidget and slider. This is my layout viewer.

    0_1538977386805_ui_layout.png

    How to place view in proper place.



  • @amarism
    auto *viewTopLeft = new OpenGlWidget(this);
    auto *viewTopRight = new OpenGlWidget(this);
    auto *viewBottomLeft = new OpenGlWidget(this);
    auto *viewBottomRight = new OpenGlWidget(this);

    QSlider *slider1=new QSlider;
    slider1->setRange(0,100);
    slider1 ->setOrientation(Qt::Horizontal);
    QSlider *slider2=new QSlider;
    slider2->setRange(0,100);
    slider2 ->setOrientation(Qt::Horizontal);
    QSlider *slider3=new QSlider;
    slider3->setRange(0,100);
    slider3 ->setOrientation(Qt::Horizontal);
    QSlider *slider4=new QSlider;
    slider4->setRange(0,100);
    slider4 ->setOrientation(Qt::Horizontal);

    ui->gridLayout->addWidget(viewTopLeft, 0, 0);
    ui->gridLayout->addWidget(viewTopRight, 0, 1);
    ui->gridLayout->addWidget(slider1, 1, 0);
    ui->gridLayout->addWidget(slider2, 1, 1);
    ui->gridLayout->addWidget(viewBottomLeft, 2, 0);
    ui->gridLayout->addWidget(viewBottomRight, 2, 1);
    ui->gridLayout->addWidget(slider3, 3, 0);
    ui->gridLayout->addWidget(slider4, 3, 1);
    

    i try this it's working .. good

    set Orientation of slider ...


Log in to reply