Tablewidget not accepting drop .



  • hello , i have return code to perform drag and drop between two tablewidget . i am not getting what mistake i have did, here is my code

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QListWidget>
    #include <QMouseEvent>
    #include <QMimeData>
    #include <QDrag>
    #include <QDebug>
    #include <QTabWidget>
    #include <QTableWidget>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    private slots:
    void on_itemchanged();
    };
    class ProjectListWidget :public QTableWidget
    {
    Q_OBJECT

    public:
    ProjectListWidget(QWidget *parent = 0);

    protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void dragEnterEvent(QDragEnterEvent *event);
    void dragMoveEvent(QDragMoveEvent *event);
    void dropEvent(QDropEvent *event);

    private:
    void performDrag();
    QPoint startPos;

    };

    #endif // MAINWINDOW_H
    @

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ProjectListWidget *tabel=new ProjectListWidget(ui->widget);
    tabel->setColumnCount(2);
    tabel->setRowCount(2);
    ProjectListWidget *tabel2=new ProjectListWidget(ui->widget_2);
    tabel2->setColumnCount(2);
    tabel2->setRowCount(2);
    }
    ProjectListWidget::ProjectListWidget(QWidget *parent)
    : QTableWidget(parent)
    {
    setAcceptDrops(true);

    }
    void ProjectListWidget::mousePressEvent(QMouseEvent *event)
    {
    qDebug()<<"mouse press";
    QWidget::mousePressEvent(event);
    if(event->button() == Qt::LeftButton)
    {
    // if(startPos)
    // delete startPos;
    //startPos = new QPoint(event->pos());
    }
    // if (event->button() == Qt::LeftButton)
    // startPos = event->pos();
    //ProjectListWidget::mousePressEvent(event);
    }
    void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)
    {
    qDebug()<<"mouse move event";
    if (event->buttons() & Qt::LeftButton) {
    int distance = (event->pos() - startPos).manhattanLength();
    if (distance >= QApplication::startDragDistance())
    performDrag();
    }
    QTableWidget::mouseMoveEvent(event);
    }
    void ProjectListWidget::performDrag()
    {
    qDebug()<<"perform drag";
    QTableWidgetItem *item = currentItem();
    if (item) {
    QMimeData *mimeData = new QMimeData;
    mimeData->setText(item->text());

        QDrag *drag = new QDrag(this);
        drag->setMimeData(mimeData);
        QString fileName = "C:/Users/teclever/Desktop/qt-logo.jpg";
        QPixmap pic (fileName);
        drag->setPixmap(pic);
        if (drag->exec&#40;Qt::CopyAction&#41; == Qt::CopyAction&#41;
        {
        }
    }
    

    }
    void ProjectListWidget::dragEnterEvent(QDragEnterEvent *event)
    {
    qDebug()<<"drag enter";
    ProjectListWidget *source =
    qobject_cast<ProjectListWidget *>(event->source());
    if (source && source != this) {
    event->setDropAction(Qt::CopyAction);
    event->accept();
    }
    }
    void ProjectListWidget::dragMoveEvent(QDragMoveEvent *event)
    {
    qDebug()<<"drag move event";
    ProjectListWidget *source =
    qobject_cast<ProjectListWidget *>(event->source());
    if (source && source != this) {
    event->setDropAction(Qt::MoveAction);
    event->accept();
    }
    }
    void ProjectListWidget::dropEvent(QDropEvent *event)
    {
    qDebug()<<"drop event";
    ProjectListWidget *source =
    qobject_cast<ProjectListWidget *>(event->source());
    if (source && source != this) {
    QString plaintext= event->mimeData()->text();
    event->setDropAction(Qt::MoveAction);
    event->accept();
    }
    }

    void MainWindow:: on_itemchanged()
    {
    qDebug()<<"on_itemchanged";
    }

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

    please correct me in my code .



  • better use a QTableView.
    you should to set a model with:
    virtual void QTableView::setModel(QAbstractItemModel * model)
    and then in your class model QAbstractItemModel override this functions:
    virtual QStringList QAbstractItemModel::mimeTypes() const
    virtual QMimeData * QAbstractItemModel::mimeData(const QModelIndexList & indexes) const
    ,this give you a format of the data that you can drag and drop.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.