頭檔案如下:
#ifndef PAINTWIDGET_H
#define PAINTWIDGET_H
#include <QWidget>
#include "shape.h"
#include "line.h"
#include "rect.h"
namespace Ui {
class PaintWidget;
}
class PaintWidget : public QWidget
{
Q_OBJECT
public:
explicit PaintWidget(QWidget *parent = 0);
~PaintWidget();
private:
Ui::PaintWidget *ui;
Shape::Code currShapeCode;
Shape *shape;
bool perm;
QList<Shape*> shapeList;
public slots:
void setCurrentShape(Shape::Code s)
{
if(s !=currShapeCode)
{
currShapeCode=s;
}
}
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *);
};
#endif // PAINTWIDGET_H
.cpp檔案如下
#include "paintwidget.h"
#include "ui_paintwidget.h"
#include "qpainter.h"
PaintWidget::PaintWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::PaintWidget)
{
ui->setupUi(this);
}
void PaintWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setBrush(Qt::white);
painter.drawRect(0,0,size().width(),size().height());
foreach(Shape*shape,shapeList)
{shape->paint(painter);
}
if(shape)
{
shape->paint(painter);
}
}
void PaintWidget::mousePressEvent(QMouseEvent *)
{
switch(currShapeCode)
{
case Shape::Line:
{
shape = new Line;
break;
}
case Shape::Rect:
{
shape = new Rect;
break;
}
}
if(shape != NULL)
{
perm=false;
shapeList<<shape;
shape->setStart(event->pos());
shape->setEnd(event->pos());
}
}
void PaintWidget::mouseMoveEvent(QMouseEvent *)
{
if(shape && !perm) {
shape->setEnd(event->pos());
update();
}
}
void PaintWidget::mouseReleaseEvent(QMouseEvent *)
{
perm = true;
}
PaintWidget::~PaintWidget()
{
delete ui;
}
上述代碼中出現了這幾類問題:
1. F:\Qt\studyroad\test\paintwidget.cpp:31: error: cannot convert 'Line*' to 'Shape*' in assignment
shape = new Line;
^
2. F:\Qt\studyroad\test\paintwidget.cpp:52: error: base operand of '->' is not a pointer
3. F:\Qt\studyroad\test\paintwidget.cpp:52: error: invalid use of member (did you forget the '&' ?)
shape->setEnd(event->pos());
^
不知道哪里出了問題,試了好幾遍了。麻煩各位大神!
uj5u.com熱心網友回復:
看起來猜測原因:1:Line不是Shape的子類
2:Shape沒有setEnd函式,如果是子類存在的,那得先轉為子類的指標再使用
uj5u.com熱心網友回復:
好的,謝謝提供思路!我試試看轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/102821.html
標籤:Qt
上一篇:為什么我的android stdio的setting下的plugins下沒有browse repositories這個選項
