Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - QTableWidget is not accepting drops

I am making a project which uses QTableWidget. When I tried to make that to accept the drops, I came to know that it is not behaving as I thought. The thing is, If I change QTableWidget into QWidget then drop got accepted. So, the problem is not in coding. Here comes the code, and as for as I am concerning the problem is in "MyDropWidget" class

#include <QMouseEvent>
#include <QWidget>
#include <QMessageBox>
#include <math.h>
#include <QApplication>
#include <QPainter>
#include <sstream>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QLabel>
#include <QPaintEvent>
#include <QTableWidget>
#define RADIUS 2
#define DISTANCE_BETWEEN_OBJECTS 2
#define DASH_RECT_HALF_WIDTH 2
#include <QHeaderView>
#include <QPainter>
#include <QHBoxLayout>
#include <QWidget>
#include <QApplication>

class MyMessageBox:public QMessageBox
{
public:
    MyMessageBox(std::string message,QWidget *parent=0):QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget)
    {
    }
};

class MyDragWidget:public QWidget
{
private:
    QPoint * start_Pos;
    QPixmap drag_Pixmap;
public:
    MyDragWidget(QWidget * parent);
private:
    void mousePressEvent(QMouseEvent * event);
    void mouseReleaseEvent(QMouseEvent * event);
    void mouseMoveEvent(QMouseEvent * event);
};

MyDragWidget::MyDragWidget(QWidget * parent):QWidget(parent)
{
    setPalette(QPalette(QColor(0,0,0)));
    setAutoFillBackground(true);
    start_Pos = NULL;

    drag_Pixmap = QPixmap(50,50);
    QPainter painter(&drag_Pixmap);
    painter.setPen(QColor(255,0,0));
    painter.drawText(0,0,50,50,Qt::AlignCenter,"drag Pic");
    painter.end();
    resize(100,200);
}

void MyDragWidget::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);
    if(event->button() == Qt::LeftButton)
    {
        if(start_Pos)
            delete start_Pos;
        start_Pos = new QPoint(event->pos());
    }
}

void MyDragWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(start_Pos)
        delete start_Pos;
    start_Pos = NULL;
}

void MyDragWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(!(event->buttons() & Qt::LeftButton))
    {
        MyMessageBox mb("mouse move 0");
        mb.exec();
        return;
    }

    if(!start_Pos)
        return;
    QDrag * drag = new QDrag(this);
    drag->setPixmap(drag_Pixmap);
    QMimeData * mimeData = new QMimeData();
    mimeData->setText("name");
    drag->setMimeData(mimeData);

    Qt::DropAction dropAction = drag->exec(Qt::CopyAction,Qt::MoveAction);
}

class MyDropWidget:public QTableWidget
{
public:
    MyDropWidget(QWidget * parent);
private:
    void dragEnterEvent(QDragEnterEvent * event);
    void dropEvent(QDropEvent *event);
};

MyDropWidget::MyDropWidget(QWidget * parent):QTableWidget(parent)
{
    setColumnCount(6);
    setRowCount(9);
    setAcceptDrops(true);
    setPalette(QPalette(QColor(250,150,210)));
    setAutoFillBackground(true);
}

void MyDropWidget::dragEnterEvent(QDragEnterEvent * event)
{
    if(event->mimeData()->hasFormat("text/plain"))
        event->acceptProposedAction();

    QTableWidget::dragEnterEvent(event);
}

void MyDropWidget::dropEvent(QDropEvent * event)
{
    QString shape = event->mimeData()->text();

    MyMessageBox mm("Drop ");
    mm.exec();
    QTableWidget::dropEvent(event);
}

class Main_Widget:public QWidget
{
    MyDragWidget * courses_DragWidget;
    MyDragWidget * rooms_DragWidget;
    MyDropWidget * timeTable_Widget;
public:
    Main_Widget();
};

Main_Widget::Main_Widget()
{
    courses_DragWidget = new MyDragWidget(NULL);
    timeTable_Widget = new MyDropWidget(NULL);

    QHBoxLayout * hBoxLayout = new QHBoxLayout;
    hBoxLayout->addWidget(courses_DragWidget,1);
    hBoxLayout->addWidget(timeTable_Widget,2);

    setLayout(hBoxLayout);
}

int main(int argc,char * argv[])
{
    QApplication app(argc,argv);

    Main_Widget * main_Widget = new Main_Widget;
    main_Widget->show();
    main_Widget->resize(100,200);

    return app.exec();
}

So, if the QTableWidget accepts the drop, then a message box will come up with a message "Drop". But, is not coming=> drop is not accepted. Can anybody help me in this issue?

Note: I use Qt-4.7.2 in windows platform

like image 535
prabhakaran Avatar asked Feb 23 '23 15:02

prabhakaran


1 Answers

There are two things you need to do:

1) Implement dragMoveEvent. This is for when the drag has entered the QWidget. An implementation might look like this :

void MyDropWidget::dragMoveEvent(QDragMoveEvent *event)
{
    event->accept();
}

As you checked the drag mime data when it entered, no need to check it again here, but you could do checking of the area you want to be able to drop in to.

2) Don't call the parent dragEnterEvent handler. So your dragEnterEvent function would be like this:

void MyDropWidget::dragEnterEvent(QDragEnterEvent * event)
{
    if(event->mimeData()->hasFormat("text/plain"))
        event->acceptProposedAction();

    /* Don't need this.. QTableWidget::dragEnterEvent(event); */
}

Reason is, QTableWidget supports it's own form of drag-drop so it's setting the event back to being rejected even though you are accepting it.

like image 188
docsteer Avatar answered Mar 02 '23 00:03

docsteer