Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6131665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:00:15+00:00 2026-05-23T17:00:15+00:00

I am making a project which uses QTableWidget. When I tried to make that

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:00:16+00:00Added an answer on May 23, 2026 at 5:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a project which uses HTML elements as nodes in a diagram and
I tried to create a jar file from a java project, which uses some
I'm not too familiar with ExtJS, but am working on a project that uses
I have a number of editions of the project for which I am making
I am making a project that should compile on Windows and Linux. I have
I am making a Python gui project that needs to duplicate the look of
I'm working on a project that uses NetBeans to generate the ANT build files.
i am making a project in which i ahve shedule somthing happen after a
I'm working on a project which uses the following technologies: C# (.NET 4.0) WCF
Say I have a web project which uses a WCF service for behind-the-scenes processing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.