Drag and drop to a Pushbutton
-
@dencla said in Drag and drop to a Pushbutton:
so why does the query model allow calls to edit it?
Because QSqlQueryModel is derived from QAbstractTableModel if you are asking about compilation. At runtime we have already said it returns an unsuccessful result if you call it. Some model types allow editing, some do not.
When I create a ui->CustomTableView nothing is displayed, but when I create a normal tableView it does display the database.
I don't know what this means. If a
QTableView
works but aCustomTableView
derived from that does not look at your code differences in the custom view. I don't know what this has to do withQSqlQueryModel
vsQSqlTableModel
, nor with drag and drop. -
@dencla said in Drag and drop to a Pushbutton:
so why does the query model allow calls to edit it?
Because QSqlQueryModel is derived from QAbstractTableModel if you are asking about compilation. At runtime we have already said it returns an unsuccessful result if you call it. Some model types allow editing, some do not.
When I create a ui->CustomTableView nothing is displayed, but when I create a normal tableView it does display the database.
I don't know what this means. If a
QTableView
works but aCustomTableView
derived from that does not look at your code differences in the custom view. I don't know what this has to do withQSqlQueryModel
vsQSqlTableModel
, nor with drag and drop.@JonB I can Drag and drop from a ui->tableView, now I need to drop to a CustomPushButton. I have promoted a ui->QPushButton to a ui->CustomPushButton. When I drag the drop indicator goes away and the drop fails. I am not sure what triggers the event.
Below is the code for the CustomPushButton::
#include <QPushButton> #include <QMimeData> #include <QDataStream> #include <QDropEvent> class CustomPushButton : public QPushButton { public: CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) { setAcceptDrops(true); } // Override the virtual method to handle drop events void dropEvent(QDropEvent *event) override { const QMimeData *mimeData = event->mimeData(); if (mimeData && mimeData->hasText()) { setText(mimeData->text()); event->acceptProposedAction(); } } }; Any Ideas
-
@JonB I can Drag and drop from a ui->tableView, now I need to drop to a CustomPushButton. I have promoted a ui->QPushButton to a ui->CustomPushButton. When I drag the drop indicator goes away and the drop fails. I am not sure what triggers the event.
Below is the code for the CustomPushButton::
#include <QPushButton> #include <QMimeData> #include <QDataStream> #include <QDropEvent> class CustomPushButton : public QPushButton { public: CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) { setAcceptDrops(true); } // Override the virtual method to handle drop events void dropEvent(QDropEvent *event) override { const QMimeData *mimeData = event->mimeData(); if (mimeData && mimeData->hasText()) { setText(mimeData->text()); event->acceptProposedAction(); } } }; Any Ideas
-
@dencla you also need to implement the dragEnterEvent and dragMoveEvent methods as they handle the dragging until you drop.
-
@dencla you also need to implement the dragEnterEvent and dragMoveEvent methods as they handle the dragging until you drop.
@SGaist This is what I have now. It still does not register the drag on the CustomPushButton.
#include <QPushButton> #include <QMimeData> #include <QDataStream> #include <QDropEvent> #include <QDebug> class CustomPushButton : public QPushButton { public: CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) { setAcceptDrops(true); // Enable drop events for the push button } protected: void dragEnterEvent(QDragEnterEvent *event) override { if (event->mimeData()->hasFormat("application/vnd.text.list")) event->accept(); else event->ignore(); } void dragMoveEvent(QDragMoveEvent *event) override { if (event->mimeData()->hasFormat("application/vnd.text.list")) { event->setDropAction(Qt::CopyAction); event->accept(); } else { event->ignore(); } } void dropEvent(QDropEvent *event) override { const QMimeData *mimeData = event->mimeData(); if (!mimeData->hasFormat("application/vnd.text.list")) { event->ignore(); return; } QByteArray encodedData = mimeData->data("application/vnd.text.list"); QDataStream stream(&encodedData, QIODevice::ReadOnly); QStringList items; while (!stream.atEnd()) { QString text; stream >> text; items.append(text); } qDebug() << "Dropped Items:" << items; // Perform actions based on the dropped items if(items.size() > 0) { setText(items.at(0)); qDebug() << "inserted data"; } // Call the base class implementation to handle other drop events QPushButton::dropEvent(event); } };
-
@SGaist This is what I have now. It still does not register the drag on the CustomPushButton.
#include <QPushButton> #include <QMimeData> #include <QDataStream> #include <QDropEvent> #include <QDebug> class CustomPushButton : public QPushButton { public: CustomPushButton(QWidget *parent = nullptr) : QPushButton(parent) { setAcceptDrops(true); // Enable drop events for the push button } protected: void dragEnterEvent(QDragEnterEvent *event) override { if (event->mimeData()->hasFormat("application/vnd.text.list")) event->accept(); else event->ignore(); } void dragMoveEvent(QDragMoveEvent *event) override { if (event->mimeData()->hasFormat("application/vnd.text.list")) { event->setDropAction(Qt::CopyAction); event->accept(); } else { event->ignore(); } } void dropEvent(QDropEvent *event) override { const QMimeData *mimeData = event->mimeData(); if (!mimeData->hasFormat("application/vnd.text.list")) { event->ignore(); return; } QByteArray encodedData = mimeData->data("application/vnd.text.list"); QDataStream stream(&encodedData, QIODevice::ReadOnly); QStringList items; while (!stream.atEnd()) { QString text; stream >> text; items.append(text); } qDebug() << "Dropped Items:" << items; // Perform actions based on the dropped items if(items.size() > 0) { setText(items.at(0)); qDebug() << "inserted data"; } // Call the base class implementation to handle other drop events QPushButton::dropEvent(event); } };
-
-
@SGaist I recompiled the project in Linux and it works.
I don't know why it doesn't work on my apple computer.Thanks