QGraphicsView拖拽方案
ProjectView::ProjectView(QWidget* parent)
: QGraphicsView(parent)
{
this->setMouseTracking(true);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->verticalScrollBar()->blockSignals(true);
this->horizontalScrollBar()->blockSignals(true);
this->horizontalScrollBar()->setEnabled(false);
this->verticalScrollBar()->setEnabled(false);
this->verticalScrollBar()->setValue(this->verticalScrollBar()->minimum());
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->minimum());
this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
this->setDragMode(QGraphicsView::RubberBandDrag);
this->setRubberBandSelectionMode(Qt::ContainsItemBoundingRect);
this->setAcceptDrops(true);
}
ProjectView::~ProjectView()
{
//qDebug()<<__FUNCTION__;
}
void ProjectView::mousePressEvent(QMouseEvent* mouseEvent)
{
m_lastPointF = mouseEvent->pos();
isPressed = true;
QGraphicsView::mousePressEvent(mouseEvent);
}
void ProjectView::mouseMoveEvent(QMouseEvent* mouseEvent)
{
if (isPressed ) {
QPointF disPointF = mouseEvent->pos() - m_lastPointF;
m_lastPointF = mouseEvent->pos();
this->scene()->setSceneRect(this->scene()->sceneRect().x() - disPointF.x(),
this->scene()->sceneRect().y() - disPointF.y(),
this->scene()->sceneRect().width(),
this->scene()->sceneRect().height());
this->scene()->update();
}
QGraphicsView::mouseMoveEvent(mouseEvent);
}
void ProjectView::mouseReleaseEvent(QMouseEvent* mouseEvent)
{
if (isPressed) {
isPressed = false;
}
QGraphicsView::mouseReleaseEvent(mouseEvent);
}