# # ifndef PAINTWIDGET_H
# define PAINTWIDGET_H
#include
# include "shape. H"
H # include "line."
# include "the rect. H"
The namespace Ui {
The class PaintWidget;
}
The class PaintWidget: public QWidget
{
Q_OBJECT
Public:
Explicit PaintWidget (QWidget * parent=0);
~ PaintWidget ();
Private:
Ui: : Ui PaintWidget *;
Shape: : Code currShapeCode;
Shape * Shape;
Bool perm;
QList
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 file the following
# include "paintwidget. H"
# include "ui_paintwidget. H"
# include "qpainter. H"
PaintWidget: : PaintWidget (QWidget * parent) :
QWidget (parent),
UI (new UI: : PaintWidget)
{
The UI - & gt; SetupUi (this);
}
Void PaintWidget: : paintEvent (QPaintEvent *)
{
QPainter painter (this);
Painter. SetBrush (Qt: : white);
Painter. DrawRect (0, 0, the size (). The width (), size (), height ());
Foreach (Shape, Shape * shapeList)
{shape - & gt; Paint (painter);
}
If (shape)
{
Shape - & gt; Paint (painter);
}
}
Void PaintWidget: : mousePressEvent (QMouseEvent *)
{
The switch (currShapeCode)
{
Case Shape: : Line:
{
Shape=new Line;
break;
}
Case Shape: : the Rect:
{
Shape=new the Rect;
break;
}
}
If (shape!=NULL)
{
Perm=false;
ShapeList
Shape - & gt; SetEnd (event - & gt; Pos ());
}
}
Void PaintWidget: : mouseMoveEvent (QMouseEvent *)
{
If (shape & amp; & ! Perm) {
Shape - & gt; SetEnd (event - & gt; Pos ());
The update ();
}
}
Void PaintWidget: : mouseReleaseEvent (QMouseEvent *)
{
Perm=true;
}
PaintWidget: : ~ PaintWidget ()
{
Delete the UI;
}
The code in the above a few questions:
1. F: \ Qt \ studyroad \ test \ paintwidget CPP: 31: error: always convert 'Line *' to 'Shape *' in the assignment
Shape=new Line;
^
2. F: \ Qt \ studyroad \ test \ paintwidget CPP: 52: error: base operand of '- & gt; 'is not a pointer
3. F: \ Qt \ studyroad \ test \ paintwidget CPP: 52: error: invalid use of member (did you forget the '& amp; '?)
Shape - & gt; SetEnd (event - & gt; Pos ());
^
Don't know what went wrong, try several times, troublesome everybody a great god!
CodePudding user response:
Seems to guess why:1: the Line is not the Shape of the subclass
2: no setEnd Shape function, if there is a subclass of the first into subclasses pointer to use again
CodePudding user response:
Ok, thank you for! I'll give it a try