QT入门_QPainter基本绘图

目录

  • 实验效果,使用方法和功能
  • 代码
  • main.cpp
  • widget.h
    • widget.cpp
    • paintarea.h
    • paintarea.cpp
  • 遇到的问题汇总
    • ① error: undefined reference to `vtable for PaintArea'
    • ② error: no matching function for call to 'PaintArea::PaintArea(Widget*)'
    • ③ error: return type specification for destructor invalid void Widget::~Widget()

实验效果,使用方法和功能

初始界面:
QT入门_QPainter基本绘图_第1张图片
点击线条右边的修改
QT入门_QPainter基本绘图_第2张图片
再修改填充,调整线宽

QT入门_QPainter基本绘图_第3张图片
将形状改为RoundRect
QT入门_QPainter基本绘图_第4张图片
附录:
文件一览:
QT入门_QPainter基本绘图_第5张图片

代码

main.cpp

#include 
#include 
int main(int argc,char* argv[])
{
    QApplication a(argc,argv);
    Widget w;
    w.show();
    return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include 
#include 
#include 
#include 
#include 
class Widget:public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget* parent = 0);
    ~Widget();
protected:
    QComboBox* shapeComboBox;
    QFrame* colorFrame;
    QFrame* brushColorFrame;
    QSpinBox* widthSpinBox;
    PaintArea* paintArea;
public slots:
    //修改形状槽函数
    void slotShape(int);
    //修改画笔槽函数
    void slotPenColor();
    //修改笔宽槽函数
    void slotPenWidth(int);
    //修改画刷槽函数
    void slotBrushColor();
};
#endif // WIDGET_H

widget.cpp

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent): QWidget(parent)
{
    //设置主窗口大小
    this->resize(700,400);

    QLabel * SharpLabel = new QLabel("形状:",this);
    shapeComboBox = new QComboBox(this);
    SharpLabel->setGeometry(430,20,60,40);
    shapeComboBox->setGeometry(500,20,150,40);
    shapeComboBox->addItem("Line",PaintArea::Line);
    shapeComboBox->addItem("Rectangle",PaintArea::Rectangle);
    shapeComboBox->addItem("RoundRect",PaintArea::RoundRect);
    shapeComboBox->addItem("Ellipse", PaintArea::Ellipse);
    shapeComboBox->addItem("Path", PaintArea::Path);
    shapeComboBox->addItem("Polyline", PaintArea::Polyline);
    shapeComboBox->addItem("Arc", PaintArea::Arc);
    shapeComboBox->addItem("Points", PaintArea::Points);
    shapeComboBox->addItem("Text", PaintArea::Text);

    QLabel * PenColorLabel = new QLabel("线条:",this);
    PenColorLabel->setGeometry(430,90,60,40);
    colorFrame = new QFrame(this);
    colorFrame->setGeometry(500,90,150,40);
    colorFrame->setAutoFillBackground(true);
    colorFrame->setPalette(QPalette(Qt::blue));
    QPushButton *colorPushButton = new QPushButton("修改",this);
    colorPushButton->setGeometry(650,90,50,40);

    QLabel * BrushColorLabel = new QLabel("填充:",this);
    BrushColorLabel->setGeometry(430,160,60,40);
    brushColorFrame = new QFrame(this);
    brushColorFrame->setGeometry(500,160,150,40);
    brushColorFrame->setAutoFillBackground(true);
    brushColorFrame->setPalette(QPalette(Qt::blue));
    QPushButton *brushcolorPushButton = new QPushButton("修改",this);
    brushcolorPushButton->setGeometry(650,160,50,40);

    QLabel * LineWidthLabel = new QLabel("线宽:",this);
    LineWidthLabel->setGeometry(430,250,60,40);
    widthSpinBox = new QSpinBox(this);
    widthSpinBox->setGeometry(500,250,60,40);
    widthSpinBox->setRange(0,20);

    connect(shapeComboBox,SIGNAL(activated(int)),this,SLOT(slotShape(int)));
    connect(colorPushButton,SIGNAL(clicked()),this,SLOT(slotPenColor()));
    connect(brushcolorPushButton,SIGNAL(clicked()),this,SLOT(slotBrushColor()));
    connect(widthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(slotPenWidth(int)));

    paintArea = new PaintArea(this);
    //设置绘图区域窗口大小及位置
    paintArea->setGeometry(0,0,400,400);
    setWindowTitle("Painter");

}

Widget::~Widget(){

}

void Widget::slotShape(int value)
{
    PaintArea::Shape shape = PaintArea::Shape(shapeComboBox->itemData(value,Qt::UserRole).toInt());
    paintArea->setShape(shape);
}

void Widget::slotPenColor()
{
    QColor color = QColorDialog::getColor(Qt::blue);
    colorFrame->setPalette(QPalette(color));
    int width = widthSpinBox->value();
    Qt::PenStyle style = Qt::SolidLine;
    Qt::PenCapStyle cap = Qt::RoundCap;
    Qt::PenJoinStyle join = Qt::RoundJoin;
    paintArea->setPen(QPen(color, width, style, cap, join));
}

void Widget::slotBrushColor()
{
    QColor color = QColorDialog::getColor(Qt::blue);
    brushColorFrame->setPalette(QPalette(color));
    paintArea->setBrush(QBrush(color));
}

void Widget::slotPenWidth(int value )
{
    QColor color = colorFrame->palette().color(QPalette::Window);
    Qt::PenStyle style = Qt::SolidLine;
    Qt::PenCapStyle cap = Qt::RoundCap;
    Qt::PenJoinStyle join = Qt::RoundJoin;
    paintArea->setPen(QPen(color, value, style, cap, join));
}

paintarea.h

#ifndef PAINTAREA_H
#define PAINTAREA_H

#include 
#include 


class PaintArea:public QWidget
{
//    Q_OBJECT
    public:
    enum Shape
     {Line,Rectangle,RoundRect,Ellipse,Polygon,Polyline,Points,Arc,Path,Text,Pixmap};
    public:
        explicit PaintArea(QWidget *parent = 0);
        ~PaintArea();
    public:
        void setShape(Shape s);
        void setPen(QPen  p);
        void setBrush(QBrush b);
        void paintEvent(QPaintEvent *);
    private:
        Shape shape;
        QBrush brush;
        QPen pen;

};

#endif // PAINTAREA_H

paintarea.cpp

#include 
#include 
#include 

PaintArea::PaintArea(QWidget* parent):QWidget(parent)
{
   //设置背景白色
   QPalette pal = palette();
   pal.setColor(QPalette::Background, Qt::white);
   setAutoFillBackground(true);
   setPalette(pal);
   shape = Line;
}

void PaintArea::setShape(Shape s)
{
    shape = s;
    update();
}

void PaintArea::setBrush(QBrush b)
{
    brush = b;
    update();
}

void PaintArea::setPen(QPen p)
{
    pen = p;
    update();
}

void PaintArea::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setPen(pen);
    p.setBrush(brush);
    QRect rect(50,100,300,200);
    static const QPoint points[4] = {
        QPoint(150,100),
        QPoint(300,150),
        QPoint(350,250),
        QPoint(100,300)
    };

    int startAngle = 30 * 16;
    int spanAngle = 120 * 16;

    QPainterPath path;
    path.addRect(150,150,100,100);
    path.moveTo(100,100);
    path.cubicTo(300,100,200,200,300,300);
    path.cubicTo(100,300,200,200,100,100);

    switch(shape)
    {
        case Line:
            p.drawLine(rect.topLeft(),rect.bottomRight());
            break;
        case Rectangle:
            p.drawRect(rect);
            break;
        case RoundRect:
            p.drawRoundRect(rect);
            break;
        case Ellipse:
            p.drawEllipse(rect);
            break;
        case Polygon:
            p.drawPolygon(points,4);
            break;
        case Polyline:
            p.drawPolyline(points,4);
            break;
        case Points:
            p.drawPoints(points,4);
            break;
        case Arc:
            p.drawArc(rect,startAngle,spanAngle);
            break;
        case Path:
            p.drawPath(path);
            break;
        case Text:
            p.drawText(rect,Qt::AlignCenter,tr("Hello Qt"));
            break;
        default:
            break;
    }
}

PaintArea::~PaintArea(){

}

遇到的问题汇总

汇总自:QT学习日志(笔记/问题)

① error: undefined reference to `vtable for PaintArea’

查找资料
Q_OBJECT 报错问题
解决方法:
注释掉了Q_OBJECT

② error: no matching function for call to ‘PaintArea::PaintArea(Widget*)’

paintArea = new PaintArea(this);
查找资料后发现自己这一块有问题
explicit PaintArea();
应该为:
explicit PaintArea(QWidget *parent = 0);

③ error: return type specification for destructor invalid void Widget::~Widget()

查找资料后发现这个错误统一指向的错误是类定义后没加分号,但是我加了,经过反复查找后发现多加了个void

void Widget::~Widget(){

}

应为:

Widget::~Widget(){

}

你可能感兴趣的:(Qt)