setDragCursor for QTreeView?
-
So as the title states i want to be able to change the icon for the cursor for my qtreeview. I would want to do this in my QDragMoveEvent if possible.
(i want to change the red forbbiden sign and the accept drop sign to my own icons)
How can i do that?
-
-
@StudentScripter
I think you call the QDrag::setDragCursor function on the drag object before calling drag.exec(). -
I tried it this way in my dragMoveEvent of my QTreeView but had no success with that. Still the forbidden cursor and the drop cursor appear:
// Create a QPixmap for the custom cursor QPixmap customCursor(":/resource/rectangle.png"); // Create a QDrag object QDrag *drag = new QDrag(this); // Set the custom cursor and drop action on the QDrag object drag->setDragCursor(customCursor, Qt::MoveAction); // Start the drag and drop operation drag->exec(Qt::MoveAction);
-
@StudentScripter said in setDragCursor for QTreeView?:
I tried it this way in my dragMoveEvent
The dragMoveEvent is called on the drop target widget. That's not the place to be creating QDrag objects.
You are creating the QDrag object in a mouse event of the source widget , right?
One thing I have found is that setting the drag object's pixmap via setPixmap() makes a difference to what cursors are shown, and for the default system cursors, what size they are drawn at. So your source widget event handler will look something like:
void SourceWidget::mousePressEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) { QDrag * drag = new QDrag(this); QMimeData * mimeData = new QMimeData; mimeData->setText("some text"); drag->setMimeData(mimeData); drag->setPixmap(mBlue); // <<< necessary drag->setDragCursor(mGreen, Qt::MoveAction); drag->setDragCursor(mRed, Qt::IgnoreAction); drag->exec(Qt::MoveAction); } }
This does seem to be a misfeature of QDrag.
This is Qt.6.6 on Windows 10. -
@KenAppleby-0 @SGaist well thanks i really appreciate you trying to help cause i'm really stuck with this. Unfortunately this isn't working for me. Cause by using the mimeData statement it crashes my programm somehow. If i leave out mimeData the programm works, no crashes but the cursor does not get overriden with my custom pixmap.
I only subclassed QTreeView and created delegates for my QStandartItems. Also i have a custom model but this only manages the flags and allows move and copy Actions.
Here is my ViewLayerStandartItemModel.cpp:
#include "ViewLayerStandartItemModel.h" #include "ViewLayerList.h" #include <QMimeData> #include <QDataStream> ViewLayerStandartItemModel::ViewLayerStandartItemModel(int rows, int columns, QObject *parent) : QStandardItemModel(rows, columns, parent) { } Qt::ItemFlags ViewLayerStandartItemModel::flags(const QModelIndex &index) const { Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index); // Überprüfen Sie, ob der Index zu einem Ihrer speziellen Delegaten gehört if (!data(index, CanHaveChildrenRole).toBool()) { return (defaultFlags & ~Qt::ItemIsDropEnabled) | Qt::ItemIsDragEnabled; // Entfernen Sie das ItemIsDropEnabled-Flag und fügen Sie das ItemIsDragEnabled-Flag hinzu } return defaultFlags | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; // Fügen Sie das ItemIsDragEnabled und ItemIsDropEnabled Flag hinzu } Qt::DropActions ViewLayerStandartItemModel::supportedDragActions() const { return Qt::MoveAction | Qt::CopyAction; } Qt::DropActions ViewLayerStandartItemModel::supportedDropActions() const { return Qt::MoveAction | Qt::CopyAction; }
And here is my subclassed QTreeView (ViewLayerList.cpp:)
#include "ViewLayerList.h" #include <QHBoxLayout> #include <QCheckBox> #include <QLabel> #include "ViewLayerDropIndicatorStyle.h" #include <QMouseEvent> #include "resizablepixmapitem.h" #include "SignalManager.h" #include <QHeaderView> #include <QPushButton> #include <QTimer> #include <QApplication> #include <QDrag> #include <QCursor> #include <QMimeData> ViewLayerList::ViewLayerList(CustomGraphicsScene *scene, QWidget *parent) : QTreeView{parent}, scene_durchgereicht(scene) { setStyle(new ViewLayerDropIndicatorStyle(style())); // Ändern Sie den Abstand zwischen den Items und der vertikalen Scrollbar setViewportMargins(0, 0, 50, 0); // Passen Sie den rechten Rand (20) an Ihre Anforderungen an //Versteckt die sinnlose Kopfzeile setHeaderHidden(true); setRootIsDecorated(true); setMouseTracking(true); mydelegate = new ViewLayerItemDelegate(this); model = new ViewLayerStandartItemModel(3,1,this); for(int row = 0; row < 3; ++row) { for(int col = 0; col < 1; ++col) { QModelIndex index = model->index(row, col, QModelIndex()); model->setData(index, ""); model->setData(index, true, ViewLayerStandartItemModel::CanHaveChildrenRole); // Dieses Element kann keine Kinder haben if(row == 0 && col == 0) { model->setData(index, false, ViewLayerStandartItemModel::CanHaveChildrenRole); // Dieses Element kann keine Kinder haben } } } this->setModel(model); this->setItemDelegate(mydelegate); this->setDragDropMode(QAbstractItemView::InternalMove); this->setSelectionMode(QAbstractItemView::ExtendedSelection); this->setDragEnabled(true); this->setAcceptDrops(true); this->setDropIndicatorShown(true); } void ViewLayerList::mousePressEvent(QMouseEvent *event) { // Ermitteln Sie den Index des Items unter der Maus QModelIndex index = indexAt(event->pos()); if (!index.isValid()) { // Wenn kein gültiges Item unter der Maus ist, rufen Sie die Basisklasse-Methode auf und kehren Sie zurück QTreeView::mousePressEvent(event); return; } MouseIsPressed = true; // Holen Sie sich die ausgewählten Indizes QModelIndexList indexes = this->selectionModel()->selectedIndexes(); //qDebug() << "Selected Index: " <<indexes; if (indexes.size() > 1) { OnlyOneItemSelected = false; }else{ //qDebug() << "Index ist 1"; OnlyOneItemSelected = true; } const QRect itemRect = this->visualRect(index); //QPointF ClickPosition = event->position(); // press position in item's coords QPointF BetterClickPos = event->position() - itemRect.topLeft(); // press position in item's coords //qDebug() << "(Better) TREE View MousePress: " << BetterClickPos; //qDebug() << "TREE Item Rect: " << itemRect; if(BetterClickPos.x() >= itemRect.width()-35 && BetterClickPos.x() <= itemRect.width()-10) { // Zugriff auf das Modell ViewLayerStandartItemModel *modelObj = qobject_cast<ViewLayerStandartItemModel*>(this->model); // Überprüfen Sie, ob das Modell korrekt gecastet wurde if(modelObj) { if(OnlyOneItemSelected == false) { for (const QModelIndex &index : indexes) { bool value = index.data(Qt::CheckStateRole).toBool(); modelObj->setData(index, !value, Qt::CheckStateRole); } }else{ bool value = index.data(Qt::CheckStateRole).toBool(); modelObj->setData(index, !value, Qt::CheckStateRole); } mydelegate->setOtherIndexClicked(true); } //Returned ohne das mouseEvent zu handeln dadurch wird ein DoubleClicken über //Der Checkbox verhindert return; } QDrag * drag = new QDrag(this); QMimeData * mimeData = new QMimeData; QPixmap mBlue("://resource/rectangle.png"); //mimeData->setText("some text"); drag->setMimeData(mimeData); drag->setPixmap(mBlue); // <<< necessary drag->setDragCursor(mBlue, Qt::MoveAction); drag->setDragCursor(mBlue, Qt::IgnoreAction); drag->exec(Qt::MoveAction); QTreeView::mousePressEvent(event); mydelegate->setOtherIndexClicked(false); } void ViewLayerList::dragEnterEvent(QDragEnterEvent *event) { selectedIndexes.clear(); selectedRows.clear(); //Casten um auf das ViewLayerStandartItemModel zuzugreifen ViewLayerStandartItemModel *modelObj = dynamic_cast<ViewLayerStandartItemModel*>(model); // Überprüfen Sie, ob das Modell korrekt gecastet wurde if (modelObj) { //Erstelle eine Liste um alle ausgewählten Items darin zu speichern selectedIndexes = selectionModel()->selectedIndexes(); qDebug() << "Selected Indexes List: "<< selectedIndexes; // Sortiere die ausgewählten Elemente nach der Zeilenposition std::sort(selectedIndexes.begin(), selectedIndexes.end(), [](const QModelIndex &a, const QModelIndex &b) { return a.row() > b.row(); }); // Extrahiere die Zeilennummern aus den ausgewählten Indizes und speichere sie in selectedRows for (const QModelIndex &index : selectedIndexes) { selectedRows.append(index.row()); } //Da die liste vom höchstenrow wert zum niedrigsten sortiert ist nehme den ersten Index HighestRow = selectedIndexes.first().row(); } QTreeView::dragEnterEvent(event); } void ViewLayerList::dragMoveEvent(QDragMoveEvent *event) { QTreeView::dragMoveEvent(event); // Verwandle die globale Position der Maus in die lokale Position des TreeView // Ermitteln Sie den Index des Items unter der Maus QModelIndex currentIndex = indexAt(event->position().toPoint()); qDebug() << "CurentIndex: " << currentIndex; if (currentIndex.isValid() && !selectedRows.contains(currentIndex.row())) { QRect rect = visualRect(currentIndex); if(event->position().toPoint().y() > rect.y() + rect.height()-10) { qDebug() << "Mouse Unter Item"; }else if(event->position().toPoint().y() < rect.y() + 10){ qDebug() << "Mouse Über Item"; } event->acceptProposedAction(); } }
-
@StudentScripter
I suggest you simplify your code (and perhaps structure and lay it out better) and at least temporarily remove any code that alters the model in the mouse and drag events. Also, of course, the debugger will tell you where it is crashing and why. -
@KenAppleby-0 I guess i fixed the issue i had to call the code after the standad implementation of qmousepress event. 2nd problem i have is the QMimeData.
As soon as i added this:
//QMimeData * mimeData = new QMimeData; //mimeData->setText("some text"); //drag->setMimeData(mimeData);
The cursor does work, but my treeview does not accept any move or copy actions of my QStandartitems anymore when adding these mimedata statements.
Also when adding the mimeData statement i get 2 cursors, these two big mushrooms:
-
@StudentScripter
QTreeView has its own implementation of drag and drop which you are probably clashing with. So QTreeView is responding to a mouse press event by creating its own QDrag object and using that. I don't think you can get access to the QTreeView's drag object.I have in a previous life been able to override the QTreeView's drag and drop behaviour by calling:
setDragDropMode(NoDragDrop);
in the view constructor, creating my own QDrag object in the mouseMoveEvent, and calling
setDragDropMode(InternalMove);
during the drag, resetting it to NoDragDrop at the completion.
For moving model items around using drag and drop, the mime data was set to the QModelIndexList of selected items. And the drag object's pixmap was set to the selected items rendered to a pixmap.
-
This works for me:
// The cursor while dragging can only be set like follows and it only accepts a QPixmap which the Qt::Cursors can't provide QPixmap drag_cursor(":/Images/dragCursor.png"); QMimeData *p_mime_data = new QMimeData; p_mime_data->setText("whaddup"); QDrag *p_drag = new QDrag(this); p_drag->setMimeData(p_mime_data); p_drag->setDragCursor(drag_cursor, Qt::CopyAction); p_drag->exec(Qt::CopyAction);
-
@qwasder85
Yes that's the way it's done, in the mousePressEvent().But in a QAbstractItemView, like QTreeView, as the OP is using, this is either overriding the QAbstractItemView's implementation, if you don't call QTreeView::mousePressEvent(), or clashing with it if you do call QTreeView::mousePressEvent().
If you're overriding, you have to implement your own drag and drop behaviour, which is, of course, perfectly doable.
-
@KenAppleby-0 @qwasder85 Well thank you 2 so much im really kind of a noobie with qt, but i really want to get this done. :)
So well @KenAppleby-0 so you say i would have to implement the drag and drop behaviour myself when i override the look of the cursor?
If so what exactly would i have to do and to handle to make this work, could you please give me a minimum starting point (i know thats much to ask for, as im sure you have plenty to do also) -
@StudentScripter
Here's a very rudimentary example. It turns out that you can reuse most of the QAbstractView drag and drop functionality. You just need to set up the QDrag object correctly with the model indexes.
Caveat emptor.#include <QMainWindow> #include <QApplication> #include <QStandardItemModel> #include <QTreeView> #include <QHBoxLayout> #include <QPainter> #include <QMouseEvent> #include <QMimeData> #include <QDrag> class MainWindow : public QMainWindow { public: MainWindow(QWidget * parent =nullptr); QStandardItemModel mModel; }; class TreeView : public QTreeView { public: TreeView(QWidget * parent); void setPixmaps(QPixmap red, QPixmap green, QPixmap blue); void mousePressEvent(QMouseEvent * event) override; void mouseMoveEvent(QMouseEvent * event) override; void dragEnterEvent(QDragEnterEvent * event) override; void dragMoveEvent(QDragMoveEvent * event) override; void dropEvent(QDropEvent * e) override; QPixmap mRed; QPixmap mGreen; QPixmap mBlue; QPoint mDragPos; bool mDragging{ false }; }; TreeView::TreeView(QWidget * parent) : QTreeView{ parent } { setDragDropMode(InternalMove); } void TreeView::setPixmaps(QPixmap red, QPixmap green, QPixmap blue) { mRed = red; mGreen = green; mBlue = blue; } void TreeView::mousePressEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) { QModelIndex index{ indexAt(event->pos()) }; if (index.isValid()) { mDragPos = event->pos(); mDragging = true; } } QTreeView::mousePressEvent(event); // implements item selection } void TreeView::mouseMoveEvent(QMouseEvent * e) { if (mDragging) { QPoint p = mDragPos - e->pos(); int d = p.manhattanLength(); if (d >= QApplication::startDragDistance()) { QModelIndexList selectedItems = selectedIndexes(); // at least one item must be selected if (selectedItems.size()) { QMimeData * mimeData = model()->mimeData(selectedItems); QDrag * drag = new QDrag(this); connect(drag, &QObject::destroyed, this, []() { qDebug() << "drop dead"; }); drag->setPixmap(mBlue); drag->setMimeData(mimeData); drag->setDragCursor(mGreen, Qt::MoveAction); drag->setDragCursor(mRed, Qt::IgnoreAction); Qt::DropAction result = drag->exec(Qt::CopyAction|Qt::MoveAction, Qt::IgnoreAction); qDebug() << "drag exec result" << result; } } } } void TreeView::dragEnterEvent(QDragEnterEvent * event) { qDebug() << "drag enter" << event->proposedAction() << event->possibleActions(); QTreeView::dragEnterEvent(event); } void TreeView::dragMoveEvent(QDragMoveEvent * event) { QTreeView::dragMoveEvent(event); event->acceptProposedAction(); } void TreeView::dropEvent(QDropEvent * e) { qDebug() << "drop accepted" << e->mimeData()->hasText() << e->mimeData()->text(); QTreeView::dropEvent(e); e->acceptProposedAction(); } MainWindow::MainWindow(QWidget * parent) : QMainWindow{ parent } { QWidget * centralwidget; QHBoxLayout * horizontalLayout; TreeView * treeView; resize(450, 400); centralwidget = new QWidget{ this }; horizontalLayout = new QHBoxLayout{ centralwidget }; treeView = new TreeView{ centralwidget }; horizontalLayout->addWidget(treeView); setCentralWidget(centralwidget); QImage canvas{ 32, 32, QImage::Format_ARGB32 }; canvas.fill(Qt::transparent); QPainter painter{ &canvas }; painter.setBrush(Qt::red); painter.drawEllipse(0, 0, 32, 32); QPixmap red{ QPixmap::fromImage(canvas) }; painter.setBrush(Qt::green); painter.drawEllipse(0, 0, 32, 32); QPixmap green{ QPixmap::fromImage(canvas) }; painter.setBrush(Qt::blue); painter.drawEllipse(0, 0, 32, 32); QPixmap blue{ QPixmap::fromImage(canvas) }; treeView->setPixmaps(red, green, blue); QStandardItem * parentItem = mModel.invisibleRootItem(); for (int i = 1; i <= 4; ++i) { QStandardItem * item = new QStandardItem(QString("item %1").arg(i)); parentItem->appendRow(item); } treeView->setModel(&mModel); } int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
-
Can't thank you enough. Seeing your response definitely made me smile. Gonna have a look and test it tomorrow to get an understanding of whats going on. :)
-
@KenAppleby-0 So thanks Ken, i tested the code you provided and it's really great! :D
But two small problems:
1. the items i move do not get removed/deleted from their old position so i end up with a bunch of copies2. In the drag i don't see the item beeing dragged/grapped like it's natively in qt treeview (hope you understand what i mean), i only see the cursor that is changed now; BIG THANKS
So yeah may you could help me with these two again if you got a minute the next few days. Would be really appreciated. :)
-
@StudentScripter said in setDragCursor for QTreeView?:
- the items i move do not get removed/deleted from their old position so i end up with a bunch of copies
Yes, that is puzzling. I don't know how to help with that specifically at the moment. But see below.
@StudentScripter said in setDragCursor for QTreeView?:
- In the drag i don't see the item beeing dragged/grapped like it's natively in qt treeview (hope you understand what i mean), i only see the cursor that is changed now
That's done by rendering the selected items to a pixmap and setting the pixmap of the drag object with the result.
Have you looked at the source code for QAbstractItemView? It's in Src/qtbase/src/widgets/itemviews/
I realise now that there's only one function you need to override:
QAbstractItemView::startDrag()
Use the souce code from QAbstractItemView.
That already does everything you want to do with the exception of the drag cursors. It should be easy to change that in an override. I'm sorry I didn't think of this sooner: I didn't realise that startDrag() is a virtual function.[ This does all seem a lot of work to do just because you don't like the system-provided drag cursors! :-) ]
-
@KenAppleby-0 Well the thing is i want to override the cursor to make a different one for when the user drags an item onto another (parenting) vs. other cursor for just moving/reordering the item. With the standard cursors it's alwasy the accept proposed action cursor, which also confused myself regularely.
I currently reverted my code back to the standard implementation, but could you please elaborate more on the start drag and changing the cursor?
PS: Yes i know much work for a hobby project, but well i tried 2 months now to get my treeview setup how i want it, therefore no problem with trying to get the last things finished. :D
-
@StudentScripter said in setDragCursor for QTreeView?:
Well the thing is i want to override the cursor to make a different one for when the user drags an item onto another (parenting) vs. other cursor for just moving/reordering the item
Ah, I see. I hadn't picked that requirement up.
If that isn't already provided by QAbstractItemView and QStandardItemModel then that would be done in the dragMoveEvent, I think. You'd need to track the model indexes that the cursor is on or between.
I can only suggest that you look at the source code for QAbstractItemView, particularly dragMoveEvent() and startDrag(). -
@KenAppleby-0 Well i got the some system implemented to check where the cursor is, but still i don't know how to set the cursor cause with the methode as mentioned before i can't get the old items deleted when they are moved to a new position.
But two small problems:
-
the items i move do not get removed/deleted from their old position so i end up with a bunch of copies
-
In the drag i don't see the item beeing dragged/grapped like it's natively in qt treeview (hope you understand what i mean), i only see the cursor that is changed now; BIG THANKS*Here is the system i implemented to check where the mouse is on dragMoveEvent():**
void ViewLayerList::dragMoveEvent(QDragMoveEvent *event) { QModelIndex indexAtMouse = indexAt(event->pos()); if (indexAtMouse.isValid()) { ViewLayerStandartItemModel* model = dynamic_cast<ViewLayerStandartItemModel*>(this->model); if (model) { QStandardItem* itemAtMouse = model->itemFromIndex(indexAtMouse); if (itemAtMouse) { QRect itemRect = visualRect(indexAtMouse); // QRect des Items int RectMinHeight = itemRect.y(); int RectMaxHeight = itemRect.height() + itemRect.y(); QPoint mousePos = event->position().toPoint(); // Mausposition in der Ansicht // Übersetzen Sie die Mauskoordinaten in das Koordinatensystem des Items QPoint mousePosInItem = mapFrom(this, itemRect.topLeft()) + mousePos - itemRect.topLeft(); qDebug() << "ItemIndex:" << itemAtMouse << " ItemRect MinHeight: " << RectMinHeight << " ItemRect MaxHeight: " << RectMaxHeight << " MousePos: " << mousePosInItem; if(mousePosInItem.y() < RectMinHeight + 5) { qDebug() << "Mouse is ABOVE Item: " << itemAtMouse; }else if(mousePosInItem.y() > RectMaxHeight-5){ qDebug() << "Mouse is BELOW Item: " << itemAtMouse; } } } } QTreeView::dragMoveEvent(event); event->acceptProposedAction(); }
-