QT报错:cannot take the address of an rvalue of type 'void'
时间: 2023-06-18 09:02:35 浏览: 427
这个错误通常出现在尝试获取一个无法获取地址的值的指针时。在C++中,void类型是没有实际的内存分配的,因此无法对其进行地址运算。可能的原因是您在尝试获取void类型的指针。解决方法是确保您要获取地址的值不是void类型,或者将其转换为可以获取地址的类型。如果您可以提供更多的代码和上下文,我可以更具体地帮助您解决问题。
相关问题
C:\Users\31073\Documents\QtStudy\main.cpp:20: error: cannot take the address of an rvalue of type 'void'
这个错误通常发生在你试图获取一个无效的值的地址时。在 C++ 中,不能获取一个临时值(rvalue)的地址,因为临时值是没有实际的存储位置的。要解决这个错误,你需要检查代码中是否有尝试获取临时值的地址的情况。
请检查你的代码,特别是第20行,在那里你可能会找到尝试获取一个 void 类型值的地址的问题。你可能需要修改代码逻辑,确保你只获取有效值的地址。如果你需要进一步的帮助,请提供相关代码,我将尽力帮助你解决问题。
上述代码提示error: reference to type 'const QCursor' could not bind to an rvalue of type 'Qt::CursorShape'
抱歉,我的代码有误,应该是 `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;
};
```
感谢您指出我的错误。