PDA

View Full Version : Drag and Drop with tabified dock widgets



chezifresh
1st June 2011, 23:37
I have a QMainWindow with a bunch of dock widgets. Like most of you, I can drag something from one of the dock widgets and drop it on my desktop. I can also drag something from one of the dock widgets to another dock widget. Pretty straight forward. (Something like this example http://www.qtcentre.org/wiki/index.php?title=Extended_Main_Window)

The hard part that I can't find out is, I want to drag something from one dock widget to a tabified dock widget. For example, imagine you have two tabified dock widgets, one on top of the other, how can you drag something from the top dock widget to the obscured dock widget? Obviously, the only thing you can drag it to is the tab bar, but I'm finding it exceedingly hard to detect a drag event ontop of the tab bar.

Anyone cracked this nut yet?

6503 6504

chezifresh
2nd June 2011, 22:40
I was already close. Here's the solution:



MyMainWindow(QWidget * parent) : QMainWindow(parent) {
setAcceptDrops(true);
_tab_drag_index = -1;
_tab_drag_tab_bar = NULL;
_tab_drag_timer = new QTimer(this);
_tab_drag_timer->setSingleShot(True);
connect(_tab_drag_timer, SIGNAL(timeout()); this, SLOT(_auto_switch_tab));
}

void dragEnterEvent(QDragEnterEvent * event) {
event.accept();
}

void dragLeaveEvent(QDragLeaveEvent * event) {
_tab_drag_timer.stop();
event.accept();
}

void dragMoveEvent(QDragMoveEvent * event) {
QTabBar * tabBar = dynamic_cast<QTabBar *>(childAt(event.pos));
if (tabBar == NULL || tabBar->parent() != this) {
event->ignore();
return;
}

QPoint global_pos = mapToGlobal(event->pos());
QPoint widget_pos = tabBar->mapFromGlobal(global_pos);

int tab_index = tabBar->tabAt(widget_pos);
if (tab_index != _tab_drag_index && tabBar != _tab_drag_tab_bar) {
_tab_drag_timer->stop();
_tab_drag_index = tab_index;
_tab_drag_tab_bar = tabBar;
_tab_drag_timer->start(QApplication.startDragTime());
}
if (tab_index == -1) {
event->ignore();
return
}
event->accept();

}

void _auto_switch_tab() {
if (!_tab_drag_tab_bar || _tab_drag_index == -1)
return;
_tab_drag_tab_bar->setCurrentIndex(_tab_drag_index);
_tab_drag_tab_bar = NULL;
_tab_drag_index = -1;
}