Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. drag drop doesn't behave as expected

drag drop doesn't behave as expected

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 357 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    squaves
    wrote on last edited by
    #1

    Iam working on a flashcard programm in qt.
    Topics are in a qtreeView. (1)
    Every topic has cards in a tableview. (2)

    f.png

    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.

    Untitled.png

    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

    JonBJ 1 Reply Last reply
    0
    • S squaves

      Iam working on a flashcard programm in qt.
      Topics are in a qtreeView. (1)
      Every topic has cards in a tableview. (2)

      f.png

      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.

      Untitled.png

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 unchecked nullptr returns?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        squaves
        wrote on last edited by
        #3

        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
        
        JonBJ 1 Reply Last reply
        0
        • S squaves

          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
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @squaves
          You have not done anything about

          You 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=0x0 is bad? You do no check in topicView->selectedItem()->addCard();? You create new models at some point. Why not step through the crash in the debugger?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            squaves
            wrote on last edited by
            #5

            iam checking for null pointers, nothing changes
            iam stepping with the debugger, every time iam getting sigsev at invisible root item

            class 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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved