GraphicView(QGraphicsView的子类)我创建了视图和设置场景(QGraphicScene对象),并在drop事件触发时将对象(QDial,Linegraphseries,Qcustomplot)添加到场景中。我能够将对象添加到场景中我可以调整大小并移动对象但是 在调整大小时我将光标悬停在小部件上,向左,向上,向下。 但是光标没有改变......对于resize对象frameless.cpp使用...
Frameless.h
#include <QtGui/QMouseEvent>
class FrameLess : public QObject
{
public:
enum Edge { None = 0x0, Left = 0x1,Top = 0x2, Right = 0x4,Bottom = 0x8,TopLeft = 0x10, TopRight = 0x20,BottomLeft = 0x40,BottomRight = 0x80};
Q_ENUM(Edge)
Q_DECLARE_FLAGS(Edges, Edge)
FrameLess(QWidget *parent);
void setBorderWidth(int);
int borderWidth() const;
QWidget *_parent = nullptr;
QRubberBand *_rubberband = nullptr;
bool _cursorchanged;
bool _leftButtonPressed;
Edges _mousePress = Edge::None;
Edges _mouseMove = Edge::None;
int _borderWidth;
QPoint _dragPos;
bool _dragStart = false;
protected:
bool eventFilter(QObject *o, QEvent *e) override;
void mouseHover(QHoverEvent*);
void mouseLeave(QEvent*);
void mousePress(QMouseEvent*);
void mouseRealese(QMouseEvent*);
void mouseMove(QMouseEvent*);
void updateCursorShape(const QPoint &);
void calculateCursorPosition(const QPoint &, const QRect &, Edges &);
};
Frameless.cpp
#include "frameless.h"
FrameLess::FrameLess(QWidget *parent) :_parent(parent),_cursorchanged(false),_leftButtonPressed(false),
_borderWidth(5),_dragPos(QPoint())
{
_parent->setMouseTracking(true);
_parent->setWindowFlags(Qt::FramelessWindowHint);
_parent->setAttribute(Qt::WA_PaintOnScreen);
_parent->setAttribute(Qt::WA_Hover);
_parent->installEventFilter(this);
_rubberband = new QRubberBand(QRubberBand::Rectangle);
}
void FrameLess::updateCursorShape(const QPoint &pos)
{
qDebug() << "UpdateCursor called";
if (_parent->isFullScreen() || _parent->isMaximized())
{
if (_cursorchanged)
{
_parent->unsetCursor();
qDebug() << "UpdateCursor called cursor change return";
}
return;
}
if (!_leftButtonPressed)
{
calculateCursorPosition(pos, _parent->frameGeometry(), _mouseMove);
_cursorchanged = true;
if (_mouseMove.testFlag(Edge::Top) || _mouseMove.testFlag(Edge::Bottom))
{
qDebug() << "UpdateCursor called cursor change bottom or top";
_parent->setCursor(Qt::SizeVerCursor);
}
else if (_mouseMove.testFlag(Edge::Left)||_mouseMove.testFlag(Edge::Right))
{
qDebug() << "UpdateCursor called cursor change left or right";
_parent->setCursor(Qt::SizeHorCursor);
}
else if(_mouseMove.testFlag(Edge::TopLeft)||_mouseMove.testFlag(Edge::BottomRight))
{
qDebug() << "UpdateCursor called cursor change TopLeft or BottomRight";
_parent->setCursor(Qt::SizeFDiagCursor);
}
else if (_mouseMove.testFlag(Edge::TopRight)||_mouseMove.testFlag(Edge::BottomLeft))
{
qDebug() << "UpdateCursor called cursor change TopRight or BottomLeft";
_parent->setCursor(Qt::SizeBDiagCursor);
}
else if (_cursorchanged)
{
_parent->unsetCursor();
_cursorchanged = false;
}
}
}
Graphicsview.cpp
#include "frameless.h"
GraphicsView::GraphicsView(QWidget *parent)
: QGraphicsView(parent)
{
scene = new GraphicScene();
this->setScene(scene);
this->setAcceptDrops(true);
this->setFixedSize(1100,750);
scene->setSceneRect(0,0,1100,750);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void GraphicsView::dropEvent(QDropEvent *event)
{
if(event->mimeData()->text() == "Dial")
{
QDial *dial = new QDial(); **// creating object**
FrameLess *frame = new FrameLess(dial); // **frameless for resize**
QGraphicsProxyWidget *proxy = scene->addWidget(dial);
proxy->setPos(event->pos().x(), event->pos().y());
}
}
M在cusor中获取所有调试消息改变功能。
我添加了最小的项目,你可以运行和检查。请仔细阅读此链接 Minimal Project with all related files