Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting problem in drag and drop of thumbnail image inside the image view?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 2 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amarism
    wrote on last edited by amarism
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      anil_arise
      wrote on last edited by
      #2

      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();
      
      A 1 Reply Last reply
      0
      • A anil_arise

        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();
        
        A Offline
        A Offline
        amarism
        wrote on last edited by
        #3

        @anil_arise Sorry I m not getting this one code

        A 1 Reply Last reply
        0
        • A amarism

          @anil_arise Sorry I m not getting this one code

          A Offline
          A Offline
          anil_arise
          wrote on last edited by
          #4

          @amarism you checked it or not this Git code

          A 1 Reply Last reply
          0
          • A anil_arise

            @amarism you checked it or not this Git code

            A Offline
            A Offline
            amarism
            wrote on last edited by amarism
            #5

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

            A 1 Reply Last reply
            0
            • A amarism

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

              A Offline
              A Offline
              anil_arise
              wrote on last edited by
              #6

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

              A 1 Reply Last reply
              0
              • A anil_arise

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

                A Offline
                A Offline
                amarism
                wrote on last edited by
                #7

                @anil_arise still this one not working

                A 1 Reply Last reply
                0
                • A amarism

                  @anil_arise still this one not working

                  A Offline
                  A Offline
                  anil_arise
                  wrote on last edited by
                  #8

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

                  A 1 Reply Last reply
                  0
                  • A anil_arise

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

                    A Offline
                    A Offline
                    amarism
                    wrote on last edited by amarism
                    #9

                    @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

                    A 1 Reply Last reply
                    0
                    • A amarism

                      @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

                      A Offline
                      A Offline
                      anil_arise
                      wrote on last edited by
                      #10

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

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

                      A 1 Reply Last reply
                      0
                      • A anil_arise

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

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

                        A Offline
                        A Offline
                        amarism
                        wrote on last edited by amarism
                        #11

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

                        A 1 Reply Last reply
                        0
                        • A amarism

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

                          A Offline
                          A Offline
                          anil_arise
                          wrote on last edited by
                          #12

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

                          A 1 Reply Last reply
                          0
                          • A anil_arise

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

                            A Offline
                            A Offline
                            amarism
                            wrote on last edited by
                            #13

                            @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

                            A 1 Reply Last reply
                            0
                            • A amarism

                              @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

                              A Offline
                              A Offline
                              anil_arise
                              wrote on last edited by
                              #14

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

                              A 1 Reply Last reply
                              0
                              • A anil_arise

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

                                A Offline
                                A Offline
                                amarism
                                wrote on last edited by
                                #15

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

                                A 1 Reply Last reply
                                0
                                • A amarism

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

                                  A Offline
                                  A Offline
                                  anil_arise
                                  wrote on last edited by
                                  #16

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

                                  A 1 Reply Last reply
                                  0
                                  • A anil_arise

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

                                    A Offline
                                    A Offline
                                    amarism
                                    wrote on last edited by
                                    #17

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

                                    A 1 Reply Last reply
                                    0
                                    • A amarism

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

                                      A Offline
                                      A Offline
                                      anil_arise
                                      wrote on last edited by
                                      #18

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

                                      A 1 Reply Last reply
                                      0
                                      • A anil_arise

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

                                        A Offline
                                        A Offline
                                        amarism
                                        wrote on last edited by
                                        #19

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

                                        A 1 Reply Last reply
                                        0
                                        • A amarism

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

                                          A Offline
                                          A Offline
                                          anil_arise
                                          wrote on last edited by
                                          #20

                                          @amarism by using LAYOUT

                                          A 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved