QT拖动窗口实现

 

具体实现步骤

首先是目标窗口

目标窗口TargetWidget需要接受拖动事件,在构造函数中设置

	this->setAcceptDrops(true);//支持拖动操作

实现拖动进入和放下两个函数

void SliceBrowseWindow::dragEnterEvent(QDragEnterEvent *event)
{
	//这句话让该窗口可以捕捉到dropEvent事件
	event->acceptProposedAction();
	QWidget::dragEnterEvent(event);
}
void SliceBrowseWindow::dropEvent(QDropEvent * event)
{
	//拖动完成:业务代码,一般是修改目标窗口内容,刷新目标窗口,也就是当前窗口
	auto source = event->source();
	auto fromWindow = ***::Instance()->GetMovingSeriesWindow();
	***::Instance()->SetMovingSeriesWindow(nullptr);
	auto fileList = SeriesMgr::Instance()->GetSeries(fromWindow->GetSeriesId()).GetSliceFileFullPathList();
	this->GetBrowser()->SetDicomFileNameList(fileList);
	this->GetBrowser()->seriesId = fromWindow->GetSeriesId();
	this->GetBrowser()->GetRenderWindow()->Render();//使用Window的Render来刷新界面
}

其次是源窗口

源窗口需要在一个地方触发拖动,通常不再是Drop***,而是鼠标按下的时候设置进入拖动可以拖的状态。而在QT和VTK结合的程序中,这部分鼠标事件会被QVTKOpenGLWIDget捕获,所以应该在VTK鼠标回调函数中实现。

void SeriesPreviewWindowCallback::Execute(vtkObject *caller, unsigned long eventId, void *callData)
{
	switch (eventId)
	{
	case vtkCommand::LeftButtonPressEvent:
	{
		***::Instance()->SetMovingSeriesWindow(m_seriesPreviewWindow);//进入拖动状态
		break;
	}
	case vtkCommand::MouseMoveEvent:
	{
		if (***::Instance()->GetMovingSeriesWindow())//判断是否进入拖动状态,不然一移动鼠标就会拖动
		{
			QDrag *drag = new QDrag(***::Instance()->GetMovingSeriesWindow());
			QMimeData *mimeData = new QMimeData;//无需释放?
			//mimeData->setData("custom/host_free", ...设置一个ByteArray...);
			drag->setMimeData(mimeData);
			QPixmap pixmap = Tenoke::Instance()->GetMovingSeriesWindow()->GetWindowRectQPixmap();
			drag->setPixmap(pixmap);
			//这里会阻塞
			Qt::DropAction resultAction = drag->exec(Qt::MoveAction);
			delete drag;
			***::Instance()->SetMovingSeriesWindow(nullptr);//结束拖动状态
		}
		break;
	}
	}
}

拖动的时候显示源窗口的缩略图截图

QPixmap SeriesPreviewWindow::GetWindowRectQPixmap(void)
{
	QRect rect = this->ui.seriesWidget->geometry();
	QPixmap pixmap = this->grab(rect);
	return pixmap;
}

另外参考:详解QT下拖动操作Drag-Drop的实现

你可能感兴趣的:(VTK/ITK,QT)