我试图创建可移动的矩形,这是我的代码。但是出现了3个错误。
invalid use of incomplete type 'class QGraphicsRectItem' rect->setFlag(QGraphicsItem::ItemIsMovable, true);
forward declaration of 'class QGraphicsItem' class QGraphicsItem;
incomplete type 'QGraphicsItem' used in nested name specifier
没有这一行(rect->setFlag(QGraphicsItem::ItemIsMovable, true);
)程序运行没有任何错误。
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QGraphicsScene>
#include <QWidget>
#include <QtCore>
#include <QtGui>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
QGraphicsScene *scene;
QGraphicsEllipseItem *elipse;
QGraphicsRectItem *rect;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QCursor>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QBrush redBrush(Qt::red);
QBrush blueBrush(Qt::blue);
QPen blackPen(Qt::black);
blackPen.setWidth(6);
elipse = scene->addEllipse(10,10,100,100,blackPen,redBrush);
rect = scene->addRect(-10,-10,100,100,blackPen,redBrush);
rect->setFlag(QGraphicsItem::ItemIsMovable, true);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
}
答案 0 :(得分:0)
您尚未在代码中包含正在使用的<QGraphicsRectItem>
类。像这样在开头添加它:
#include <QGraphicsScene>
#include <QGraphicsRectItem> //You missed
#include <QWidget>
#include <QtCore>
#include <QtGui>