gpt4 book ai didi

c++ - 尝试在 Qt 中使用 boundingRect

转载 作者:行者123 更新时间:2023-11-27 23:55:16 29 4
gpt4 key购买 nike

Kwadrat 类有什么问题?我有一个错误:

Invalid new-expression of abstract class type 'Kwadrat'

Kwadrat* kwadrat = new Kwadrat(20);

我想要在屏幕上移动一个正方形(当它在 X 坐标上达到 370 或在 Y 坐标上达到 370 时它会回到中间)。

当 Kwadrat 是经典的 QGraphicsRectItem 时,我有一个 (0,0) 坐标的错误。

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QPixmap>
#include "poruszanie.h"
#include <QRectF>
#include <QGraphicsRectItem>

class Kwadrat : public QGraphicsItem
{
Q_OBJECT

public:
Kwadrat(int size)
: QGraphicsItem(NULL) // we could parent, but this may confuse at first
{
m_boundingRect = QRectF(0, 0, size, size);
}

QRectF boundingRect() const
{
return m_boundingRect;
}

private:
QRectF m_boundingRect;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene*scena=new QGraphicsScene();

// Poruszanie*kwadrat=new Poruszanie();
// kwadrat->setRect(0,0,20,20);
// kwadrat->setBrush(QBrush(Qt::white));
// scena->addItem(kwadrat);

Kwadrat*kwadrat=new Kwadrat(20);
kwadrat->setBrush(QBrush(Qt::white));
scena->addItem(kwadrat);

kwadrat->setFlag(QGraphicsItem::ItemIsFocusable);
kwadrat->setFocus();

QGraphicsView *widok=new QGraphicsView(scena);
widok->setBackgroundBrush(QBrush(Qt::yellow));
widok->setMinimumSize(400,400);
widok->show();

return a.exec();
}

最佳答案

To write your own graphics item, you first create a subclass of QGraphicsItem, and then start by implementing its two pure virtual public functions: boundingRect(), which returns an estimate of the area painted by the item, and paint(), which implements the actual painting.

来自 http://doc.qt.io/qt-5/qgraphicsitem.html#details

所以你需要实现纯虚函数paint来绘制你的QGraphicsItem来消除错误,你的编译器应该已经表明,除了告诉你知道你的类是抽象的(因为缺少函数实现)。

关于c++ - 尝试在 Qt 中使用 boundingRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43087556/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com