PDA

View Full Version : Dragging QDockWidgets between QMainWindows



FelixB
23rd May 2011, 16:09
Hello everyone,

I want to create an application with two windows. One of them is a QMainWindow containing QDockWidgets. I want these DockWidgets to be draggable into the second window. How can that be done? Is there a simple way to reparent the DockWidgets when dragging over second window? Or can the DockWidgets be configured to accept the second window as DockArea?

thanks!
Felix

FelixB
24th May 2011, 11:15
I found a solution :) it still has to be "beautified", but it's working!


static QDockWidget* dw = NULL;

void MainWindow::CreateDockWidgets()
{
QDockWidget *dock = new QDockWidget(tr("Customers"), this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
customerList = new QListWidget(dock);
customerList->addItems(QStringList()
<< "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton"
<< "Jane Doe, Memorabilia, 23 Watersedge, Beaton"
<< "Tammy Shea, Tiblanka, 38 Sea Views, Carlton"
<< "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal"
<< "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston"
<< "Sally Hobart, Tiroli Tea, 67 Long River, Fedula");
dock->setWidget(customerList);
dock->setFeatures(dock->features() & ~QDockWidget::DockWidgetFloatable);
addDockWidget(Qt::RightDockWidgetArea, dock);
viewMenu->addAction(dock->toggleViewAction());
connect(dock, SIGNAL(topLevelChanged(bool)) , this, SLOT(dragStarted(bool)));
connect(dock, SIGNAL(dockLocationChanged (Qt::DockWidgetArea)) , this, SLOT(dragEnded()));

//... more dock widgets
}


void MainWindow::dragStarted(bool started)
{
if(started)
{
if(QDockWidget* dock = qobject_cast<QDockWidget*>(sender()))
dw = dock;
else
dw = NULL;
}
}


void MainWindow::enterEvent(QEvent* event)
{
if(dw && dw->parent() != this)
{
addDockWidget(Qt::RightDockWidgetArea, dw);
dw = NULL;
}
}


void MainWindow::dragEnded()
{
dw = NULL;
}

sandyam
14th October 2011, 06:31
hi ...
i too have same requirement , i have one mainwindow and 4 dockwidgets ,and there is no central widgets also, and all dockwidgets should be floatable , movable ,draggable to all areas , so any idea how i can proceed ..................

thank u in advance ,,,:)

FelixB
14th October 2011, 08:37
Hi,

you set the central widget explicitly to NULL. Then you enable DockNesting and that should be all for your requirements...

just put these lines in your mainwindow constructor:

setDockNestingEnabled(true);
setCentralWidget(0);

hope that helps...
Felix