drag drop doesn't behave as expected
-
Iam working on a flashcard programm in qt.
Topics are in a qtreeView. (1)
Every topic has cards in a tableview. (2)
problem:
I want to drag topics into one another for reorganization, it works for the treeview but in the tableview ,things arent happening as expected. Instead of cards there are now references to topics.
code:
class TopicView : public QTreeView { Q_OBJECT public: TopicView(TopicModel*, QWidget* parent = nullptr); TopicModel* model(); TopicItem* selectedItem(); signals: void newSelection(); public slots: void currentChanged(const QModelIndex&, const QModelIndex&) override; private: void setupUi(); }; TopicView::TopicView(TopicModel* model, QWidget* parent) : QTreeView(parent) { setupUi(); setModel(model); } void TopicView::setupUi() { setDragDropMode(InternalMove); setHeaderHidden(true); } TopicModel* TopicView::model() { QAbstractItemModel* model { QAbstractItemView::model() }; return static_cast<TopicModel*>(model); } TopicItem* TopicView::selectedItem() { QModelIndex selectedIndex = currentIndex(); return static_cast<TopicItem*>(model()->itemFromIndex(selectedIndex)); } void TopicView::currentChanged(const QModelIndex&, const QModelIndex&) { emit newSelection(); } void EditingMenu::update() { if (topicView->selectedItem() == nullptr) { toolBar->addChild->setDisabled(true); toolBar->removeTopic->setDisabled(true); cardView->setModel(new QStandardItemModel); return; } toolBar->addChild->setEnabled(true); toolBar->removeTopic->setEnabled(true); CardModel* model = static_cast<CardModel*>(topicView->selectedItem()->cards()); cardView->setModel(model); } void EditingMenu::addCard() { topicView->selectedItem()->addCard(); update(); } void EditingMenu::addTopic() { topicView->model()->addTopic(); } class TopicItem : public QStandardItem { public: TopicItem(const QString&); QStandardItemModel* cards(); void addCard(); private: CardModel* m_cards; }; TopicItem::TopicItem(const QString& name) : m_cards { new CardModel } { setText(name); } QStandardItemModel* TopicItem::cards() { return m_cards; } void TopicItem::addCard() { m_cards->addCard(); }weird fact:
iam getting a segmentation fault in qt6, but not in qt5 -
Iam working on a flashcard programm in qt.
Topics are in a qtreeView. (1)
Every topic has cards in a tableview. (2)
problem:
I want to drag topics into one another for reorganization, it works for the treeview but in the tableview ,things arent happening as expected. Instead of cards there are now references to topics.
code:
class TopicView : public QTreeView { Q_OBJECT public: TopicView(TopicModel*, QWidget* parent = nullptr); TopicModel* model(); TopicItem* selectedItem(); signals: void newSelection(); public slots: void currentChanged(const QModelIndex&, const QModelIndex&) override; private: void setupUi(); }; TopicView::TopicView(TopicModel* model, QWidget* parent) : QTreeView(parent) { setupUi(); setModel(model); } void TopicView::setupUi() { setDragDropMode(InternalMove); setHeaderHidden(true); } TopicModel* TopicView::model() { QAbstractItemModel* model { QAbstractItemView::model() }; return static_cast<TopicModel*>(model); } TopicItem* TopicView::selectedItem() { QModelIndex selectedIndex = currentIndex(); return static_cast<TopicItem*>(model()->itemFromIndex(selectedIndex)); } void TopicView::currentChanged(const QModelIndex&, const QModelIndex&) { emit newSelection(); } void EditingMenu::update() { if (topicView->selectedItem() == nullptr) { toolBar->addChild->setDisabled(true); toolBar->removeTopic->setDisabled(true); cardView->setModel(new QStandardItemModel); return; } toolBar->addChild->setEnabled(true); toolBar->removeTopic->setEnabled(true); CardModel* model = static_cast<CardModel*>(topicView->selectedItem()->cards()); cardView->setModel(model); } void EditingMenu::addCard() { topicView->selectedItem()->addCard(); update(); } void EditingMenu::addTopic() { topicView->model()->addTopic(); } class TopicItem : public QStandardItem { public: TopicItem(const QString&); QStandardItemModel* cards(); void addCard(); private: CardModel* m_cards; }; TopicItem::TopicItem(const QString& name) : m_cards { new CardModel } { setText(name); } QStandardItemModel* TopicItem::cards() { return m_cards; } void TopicItem::addCard() { m_cards->addCard(); }weird fact:
iam getting a segmentation fault in qt6, but not in qt5@squaves said in drag drop doesn't behave as expected:
iam getting a segmentation fault in qt6, but not in qt5
Start by resolving this. What is the stack trace when you get a seg fault, running under the debugger? You have a number of
static_cast<>s.../potentially uncheckednullptrreturns? -
the segmentation fault comes when i click add Card, before that the topic has only an empty table
#0 0x00007ffff745d8a4 in QStandardItemModel::invisibleRootItem() const () from /usr/lib/libQt6Gui.so.6 #1 0x00007ffff745f116 in QStandardItemModel::insertRow(int, QList<QStandardItem*> const&) () from /usr/lib/libQt6Gui.so.6 #2 0x0000555555560336 in QStandardItemModel::insertRow (aitem=0x0, arow=0, this=0x0) at /usr/include/qt6/QtCore/qlist.h:159 #3 CardModel::addCard (this=0x0) at ../src/cardModel.cpp:14 #4 0x0000555555560039 in TopicItem::addCard (this=<optimized out>) at ../src/topicItem.cpp:16 #5 0x000055555555ea05 in EditingMenu::addCard (this=0x7fffffffe510) at ../src/editingMenu.cpp:27 #6 0x00007ffff6b66d70 in ?? () from /usr/lib/libQt6Core.so.6 #7 0x00007ffff74c32a7 in QAction::triggered(bool) () from /usr/lib/libQt6Gui.so.6 #8 0x00007ffff74c8292 in QAction::activate(QAction::ActionEvent) () from /usr/lib/libQt6Gui.so.6 #9 0x00007ffff7a6dab1 in ?? () from /usr/lib/libQt6Widgets.so.6 #10 0x00007ffff7a79b4b in QAbstractButton::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/libQt6Widgets.so.6 #11 0x00007ffff7b78c8f in QToolButton::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/libQt6Widgets.so.6 #12 0x00007ffff79ba33c in QWidget::event(QEvent*) () from /usr/lib/libQt6Widgets.so.6 #13 0x00007ffff7975b5d in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQt6Widgets.so.6 #14 0x00007ffff796ed59 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/libQt6Widgets.so.6 #15 0x00007ffff6b1343a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/libQt6Core.so.6 #16 0x00007ffff796b776 in QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool, bool) () from /usr/lib/libQt6Widgets.so.6 #17 0x00007ffff79c9caf in ?? () from /usr/lib/libQt6Widgets.so.6 #18 0x00007ffff79cb1bd in ?? () from /usr/lib/libQt6Widgets.so.6 #19 0x00007ffff7975b5d in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQt6Widgets.so.6 #20 0x00007ffff6b1343a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/libQt6Core.so.6 #21 0x00007ffff71bf11c in QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent*) () from /usr/lib/libQt6Gui.so.6 #22 0x00007ffff7201995 in QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Gui.so.6 #23 0x00007ffff36450d0 in ?? () from /usr/lib/qt6/plugins/platforms/../../../libQt6XcbQpa.so.6 #24 0x00007ffff6117c6b in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0 #25 0x00007ffff616e001 in ?? () from /usr/lib/libglib-2.0.so.0 #26 0x00007ffff6115392 in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0 #27 0x00007ffff6d2c8d0 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #28 0x00007ffff6b1b94b in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #29 0x00007ffff6b15fb9 in QCoreApplication::exec() () from /usr/lib/libQt6Core.so.6 #30 0x000055555555e74a in main (argc=<optimized out>, argv=<optimized out>) at ../src/main.cpp:10 -
the segmentation fault comes when i click add Card, before that the topic has only an empty table
#0 0x00007ffff745d8a4 in QStandardItemModel::invisibleRootItem() const () from /usr/lib/libQt6Gui.so.6 #1 0x00007ffff745f116 in QStandardItemModel::insertRow(int, QList<QStandardItem*> const&) () from /usr/lib/libQt6Gui.so.6 #2 0x0000555555560336 in QStandardItemModel::insertRow (aitem=0x0, arow=0, this=0x0) at /usr/include/qt6/QtCore/qlist.h:159 #3 CardModel::addCard (this=0x0) at ../src/cardModel.cpp:14 #4 0x0000555555560039 in TopicItem::addCard (this=<optimized out>) at ../src/topicItem.cpp:16 #5 0x000055555555ea05 in EditingMenu::addCard (this=0x7fffffffe510) at ../src/editingMenu.cpp:27 #6 0x00007ffff6b66d70 in ?? () from /usr/lib/libQt6Core.so.6 #7 0x00007ffff74c32a7 in QAction::triggered(bool) () from /usr/lib/libQt6Gui.so.6 #8 0x00007ffff74c8292 in QAction::activate(QAction::ActionEvent) () from /usr/lib/libQt6Gui.so.6 #9 0x00007ffff7a6dab1 in ?? () from /usr/lib/libQt6Widgets.so.6 #10 0x00007ffff7a79b4b in QAbstractButton::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/libQt6Widgets.so.6 #11 0x00007ffff7b78c8f in QToolButton::mouseReleaseEvent(QMouseEvent*) () from /usr/lib/libQt6Widgets.so.6 #12 0x00007ffff79ba33c in QWidget::event(QEvent*) () from /usr/lib/libQt6Widgets.so.6 #13 0x00007ffff7975b5d in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQt6Widgets.so.6 #14 0x00007ffff796ed59 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/libQt6Widgets.so.6 #15 0x00007ffff6b1343a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/libQt6Core.so.6 #16 0x00007ffff796b776 in QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool, bool) () from /usr/lib/libQt6Widgets.so.6 #17 0x00007ffff79c9caf in ?? () from /usr/lib/libQt6Widgets.so.6 #18 0x00007ffff79cb1bd in ?? () from /usr/lib/libQt6Widgets.so.6 #19 0x00007ffff7975b5d in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQt6Widgets.so.6 #20 0x00007ffff6b1343a in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/libQt6Core.so.6 #21 0x00007ffff71bf11c in QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent*) () from /usr/lib/libQt6Gui.so.6 #22 0x00007ffff7201995 in QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Gui.so.6 #23 0x00007ffff36450d0 in ?? () from /usr/lib/qt6/plugins/platforms/../../../libQt6XcbQpa.so.6 #24 0x00007ffff6117c6b in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0 #25 0x00007ffff616e001 in ?? () from /usr/lib/libglib-2.0.so.0 #26 0x00007ffff6115392 in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0 #27 0x00007ffff6d2c8d0 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #28 0x00007ffff6b1b94b in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/libQt6Core.so.6 #29 0x00007ffff6b15fb9 in QCoreApplication::exec() () from /usr/lib/libQt6Core.so.6 #30 0x000055555555e74a in main (argc=<optimized out>, argv=<optimized out>) at ../src/main.cpp:10@squaves
You have not done anything aboutYou have a number of static_cast<>s.../potentially unchecked nullptr returns?
There are other places where you do not check your pointers etc., do so. Maybe
this=0x0is bad? You do no check intopicView->selectedItem()->addCard();? You createnewmodels at some point. Why not step through the crash in the debugger? -
iam checking for null pointers, nothing changes
iam stepping with the debugger, every time iam getting sigsev at invisible root itemclass topicModel : public QStandardItem { public: topicModel(); topicModel(const QString&); QStandardItemModel* cardsModel(); QStandardItem* getContainer(); private: QStandardItemModel* cards; QStandardItem* container; };I just dont understand how qt knows about my ''hidden'' qstandarditems which are, why so ever getting replaced after drag and drop. Iam filling the table with the card model, how can topic references even get there?
i will try out custom model, its my last hope.