Capture drag and drop events when moving QDockWidget



  • I'm attempting to create a system where I can drag and drop QDockWidgets into a central QTabWidget.

    Unfortunately I've hit a road block. Is there a way to capture the drag/drop events from moving a QDockWidget within my QTabWidget. I haven't had any success.

    Obviously, QMainWindow is able to recognize QDockWidgets. I need to write a QTabWidget to do the same.

    Edit: Below is my simple attempt at trying to catch the drop event in my debugger.

    @
    // My simple main window for testing.
    FormMain::FormMain(QWidget* parent) : QMainWindow(parent)
    {
    this->setGeometry(80, 80, 800, 600);

     auto wTab = new TabWidget();
    
     auto wLeftDock = new QDockWidget();
     wLeftDock->setStyleSheet("background-color:red");
    
     auto wRightDock = new QDockWidget();
     wRightDock->setStyleSheet("background-color:blue");
    
     auto wTopDock = new QDockWidget();
     wTopDock->setStyleSheet("background-color:green");
    
     auto wBottomDock = new QDockWidget();
     wBottomDock->setStyleSheet("background-color:yellow");
    
     this->setCentralWidget(wTab);
     this->addDockWidget(Qt::LeftDockWidgetArea, wLeftDock);
     this->addDockWidget(Qt::RightDockWidgetArea, wRightDock);
     this->addDockWidget(Qt::TopDockWidgetArea, wTopDock);
     this->addDockWidget(Qt::BottomDockWidgetArea, wBottomDock);
    

    }

    // My simple tab widget for testing
    TabWidget::TabWidget(QWidget* parent) : QTabWidget(parent)
    {
    this->setAcceptDrops(true);
    this->installEventFilter(this);
    }

    void TabWidget::dragEnterEvent(QDragEnterEvent* event)
    {
    // Break point here.
    event->acceptProposedAction();
    }

    void TabWidget::dropEvent(QDropEvent* event)
    {
    // Break point here.
    event->acceptProposedAction();
    }

    bool TabWidget::eventFilter(QObject* obj, QEvent* event)
    {

     if(event->type() == QEvent::Type::MouseButtonRelease)
     {
                // Break point here.
          auto dummy = 2 + 3;
     }
    
     return this->QTabWidget::eventFilter(obj, event);
    

    }
    @

    None of my break points are hit when I attempt to drag a QDockWidget from it's docked location to the center tab widget.


  • Lifetime Qt Champion

    Hi,

    What did you try ?



  • Sorry, updated my post with my test code.

    My idea is to drag a qdockwidget over to a qtabwidget and when a drop event occurs, set the contents of the qdockwidget into a new tab on the qtabwidget.

    Then, i also want to be able to drag a "tab" from the tab widget which will immediately convert it back to a qdockwidget which can then be dropped back into a docking location on the qmainwindow.


  • Lifetime Qt Champion

    The drag and drop of QDockWidgets is specially handled in QMainWindow so you won't have any drop in your QTableView from it. You'd need to take a look at QMainWindow source code to replicate the functionality



  • That's what I figured I'd have to do. Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.