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. App crashing with custom drag and drop for QTabBar (Qt 6.5.2 for MacOS)

App crashing with custom drag and drop for QTabBar (Qt 6.5.2 for MacOS)

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 391 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.
  • C Offline
    C Offline
    cadol001
    wrote on 18 Sept 2023, 20:48 last edited by
    #1

    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();
    }
    
    J 1 Reply Last reply 19 Sept 2023, 05:24
    0
    • C cadol001
      18 Sept 2023, 20:48

      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();
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 19 Sept 2023, 05:24 last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply 19 Sept 2023, 13:41
      2
      • C Offline
        C Offline
        CPPUIX
        wrote on 19 Sept 2023, 07:36 last edited by
        #3

        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.

        1 Reply Last reply
        3
        • J jsulm
          19 Sept 2023, 05:24

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

          C Offline
          C Offline
          cadol001
          wrote on 19 Sept 2023, 13:41 last edited by cadol001
          #4

          @Abderrahmene_Rayene thank you using new to make the mime data fixed it

          J 1 Reply Last reply 19 Sept 2023, 13:41
          0
          • C cadol001
            19 Sept 2023, 13:41

            @Abderrahmene_Rayene thank you using new to make the mime data fixed it

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 19 Sept 2023, 13:41 last edited by
            #5

            @cadol001 You should thank @Abderrahmene_Rayene :-)

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            1/5

            18 Sept 2023, 20:48

            • Login

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