App crashing with custom drag and drop for QTabBar (Qt 6.5.2 for MacOS)
-
wrote on 18 Sept 2023, 20:48 last edited by
Here is a minimal example. Drag and drop a tab anywhere and it will crash. Deleting the drag object after exec doesn't fix the issue.
Any help is much appreciated, thank you!
#include <QApplication> #include <QDrag> #include <QTabBar> #include <QTabWidget> #include <QMainWindow> #include <qmimedata.h> class my_tabbar : public QTabBar { public: my_tabbar(QWidget* parent) : QTabBar(parent) { } protected: void mouseMoveEvent(QMouseEvent* event) { QDrag* drag = new QDrag(this); QMimeData mdata = QMimeData(); mdata.setText("ex"); drag->setMimeData(&mdata); drag->exec(); } }; class my_tabwidget : public QTabWidget { public: my_tabwidget(QWidget* parent) : QTabWidget(parent) { QTabBar* bar = new my_tabbar(parent); setTabBar(bar); setAcceptDrops(true); setMovable(true); for(int x = 0; x < 5; x++) { addTab(new QWidget(), "tab"+QString::number(x)); } } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow w; my_tabwidget tabWidget(&w); w.setGeometry(QRect(0,0,600,600)); tabWidget.setGeometry(QRect(0,0,600,600)); w.show(); return a.exec(); }
-
Here is a minimal example. Drag and drop a tab anywhere and it will crash. Deleting the drag object after exec doesn't fix the issue.
Any help is much appreciated, thank you!
#include <QApplication> #include <QDrag> #include <QTabBar> #include <QTabWidget> #include <QMainWindow> #include <qmimedata.h> class my_tabbar : public QTabBar { public: my_tabbar(QWidget* parent) : QTabBar(parent) { } protected: void mouseMoveEvent(QMouseEvent* event) { QDrag* drag = new QDrag(this); QMimeData mdata = QMimeData(); mdata.setText("ex"); drag->setMimeData(&mdata); drag->exec(); } }; class my_tabwidget : public QTabWidget { public: my_tabwidget(QWidget* parent) : QTabWidget(parent) { QTabBar* bar = new my_tabbar(parent); setTabBar(bar); setAcceptDrops(true); setMovable(true); for(int x = 0; x < 5; x++) { addTab(new QWidget(), "tab"+QString::number(x)); } } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow w; my_tabwidget tabWidget(&w); w.setGeometry(QRect(0,0,600,600)); tabWidget.setGeometry(QRect(0,0,600,600)); w.show(); return a.exec(); }
@cadol001 said in App crashing with custom drag and drop for QTabBar (Qt 6.5.2 for MacOS):
QTabBar* bar = new my_tabbar(parent);
Why are you setting the bar on the parent and not this?
Run through debugger and check the stack trace to see why the app crashes. -
wrote on 19 Sept 2023, 07:36 last edited by
Hi,
I think you have the same problem as in this thread : app crashing on dropevent in qtabwidget.
If you read QDrag documentation you'll notice the
QMimeData
in that example is allocated on the heap.If you read QDrag::setMimeData, you'll notice it takes a pointer, which you worked around by passing
&mdata
.If you read QMimeData documentation you'll notice it says:
QMimeData objects are usually created using new...
Using a debugger would also help to see what happened.
-
@cadol001 said in App crashing with custom drag and drop for QTabBar (Qt 6.5.2 for MacOS):
QTabBar* bar = new my_tabbar(parent);
Why are you setting the bar on the parent and not this?
Run through debugger and check the stack trace to see why the app crashes.wrote on 19 Sept 2023, 13:41 last edited by cadol001@Abderrahmene_Rayene thank you using new to make the mime data fixed it
-
@cadol001 You should thank @Abderrahmene_Rayene :-)
3/5