上述代码提示error: reference to type 'const QCursor' could not bind to an rvalue of type 'Qt::CursorShape'
时间: 2024-03-28 09:42:11 浏览: 192
抱歉,我的代码有误,应该是 `QCursor(Qt::ClosedHandCursor)` 和 `QCursor(Qt::ArrowCursor)`。 `QCursor` 不能直接绑定到 `Qt::CursorShape` 枚举类型。下面是修正过的代码:
```cpp
class MyRectItem : public QGraphicsItem
{
public:
MyRectItem(QGraphicsItem *parent = nullptr)
: QGraphicsItem(parent)
{
setFlags(ItemIsMovable | ItemSendsGeometryChanges);
}
QRectF boundingRect() const override
{
return QRectF(-50, -50, 100, 100);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
painter->setBrush(Qt::red);
painter->drawRect(boundingRect());
}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->button() == Qt::LeftButton)
{
m_lastPos = event->pos();
setCursor(QCursor(Qt::ClosedHandCursor));
}
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->buttons() & Qt::LeftButton)
{
QPointF delta = event->pos() - m_lastPos;
m_lastPos = event->pos();
QGraphicsRectItem *guideRect = new QGraphicsRectItem(boundingRect(), this);
guideRect->setPen(QPen(Qt::DashLine));
guideRect->setBrush(Qt::NoBrush);
guideRect->setPos(pos() + delta);
setCursor(QCursor(Qt::ClosedHandCursor));
}
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
{
if (event->button() == Qt::LeftButton)
{
QGraphicsItem *item = scene()->itemAt(event->scenePos(), QTransform());
if (item && item->parentItem() == this)
{
setPos(item->pos());
}
QList<QGraphicsItem *> itemList = scene()->items(event->scenePos());
for (QGraphicsItem *item : itemList)
{
if (item->parentItem() == this)
{
delete item;
}
}
setCursor(QCursor(Qt::ArrowCursor));
}
}
private:
QPointF m_lastPos;
};
```
感谢您指出我的错误。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)