QPainter和自定义控件

1.基础

#include "mywidget.h"
#include<QPixmap>
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{

}

void MyWidget::paintEvent(QPaintEvent *)//初始化和update()以及系统认为必要的时候会调用
{
    QPixmap pixmap(size());
    QPainter p(&pixmap);
    
    //旋转和缩放,可组合使用
//    QTransform  transform;
//    transform.rotate(30);
//    transform.scale(.5,.5);
//    p.setTransform(transform);

    p.rotate(30);
    p.scale(.5,.5);

    //消锯齿
    p.setRenderHint(QPainter::Antialiasing  );

    p.translate(100,100);//平移,坐标变换
    p.setPen(QPen(Qt::red,2,Qt::DashDotDotLine));
    p.setBrush(Qt::yellow);
    p.setFont(QFont("aaa",40,700,true));

    p.drawLine(QPoint(0,0),QPoint(100,100));

   // p.translate(-100,100);
    p.drawEllipse(QPoint(95,333),50,50);
    p.drawText(QPoint(300,50),"Hello world");
    p.drawPixmap(QPoint(40,40),QPixmap("../map.png"));
   // p.drawRect(QRect(40,60,100,50));
    p.drawRoundRect(QRect(100,100,100,50));

    p.end();

    p.begin(this);
    p.drawPixmap(0,0,pixmap);
}

#include<QApplication>
int main(int argc ,char **argv)
{
    QApplication app(argc,argv);

    MyWidget w;
    w.show();

    return app.exec();
}

2.实例

鼠标点动划线

#include "mywidget.h"
#include<QPixmap>
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{

}

void MyWidget::paintEvent(QPaintEvent *)//初始化和update()以及系统认为必要的时候会调用
{
    QPainter p(this);
    for(int i=0;i<_lines.size();++i)
    {
        const QVector<QPoint> &line=_lines.at(i);
        for(int j=0;j<line.size()-1;++j)
        {
            p.drawLine(line.at(j),line.at(j+1));
        }
    }
}
void MyWidget::mousePressEvent(QMouseEvent *ev)
{
    QVector<QPoint>line;
     _lines.append(line);

     QVector<QPoint>&lastLine=_lines.last();
     lastLine.append(ev->pos());
}
void MyWidget::mouseReleaseEvent(QMouseEvent *ev)
{
    QVector<QPoint>&lastLine=_lines.last();
    lastLine.append(ev->pos());
}
void MyWidget::mouseMoveEvent(QMouseEvent *ev)
{
    QVector<QPoint>&lastLine=_lines.last();
    lastLine.append(ev->pos());
    update();
}

#include<QApplication>
int main(int argc ,char **argv)
{
    QApplication app(argc,argv);

    MyWidget w;
    w.show();

    return app.exec();
}

3.自定义控件(MyButton)

MyButton.h

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QWidget>
#include<QMouseEvent>

class MyButton : public QWidget
{
    Q_OBJECT
public:
    explicit MyButton(QWidget *parent = nullptr);
    MyButton(const QString &text,QWidget *parent);
    QRect _rect;//button的大小的位置;
    QString _text;
    bool _pressed;
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    

signals:
   void clicked(){};
public slots:
};

#endif // MYBUTTON_H

MyButton.cpp

#include "mybutton.h"
#include<QPainter>
MyButton::MyButton(QWidget *parent) : QWidget(parent),_rect(0,0,100,300),_text(QString())
{
   this->setGeometry(_rect);
    _pressed=false;
}

MyButton::MyButton(const QString &text, QWidget *parent):QWidget(parent),_text(text),_rect(0,0,100,30)
{
  this->setGeometry(_rect);
    _pressed=false;
}
void MyButton::mousePressEvent(QMouseEvent *)
{
    _pressed=true;
    update();
}
void MyButton::mouseReleaseEvent(QMouseEvent *)
{
    _pressed=false;
    update();
    if(_rect.contains(ev->pos()))//有条件的发送信号
    emit clicked();
}
void MyButton::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    if(_pressed)
    {
        p.setBrush(Qt::yellow);
    }
    else
    p.setBrush(Qt::darkGray);
    p.drawRect(_rect);
    p.drawText(_rect,_text,QTextOption(Qt::AlignCenter));
}

 

 

posted on 2018-01-02 11:05  rainbow1122  阅读(289)  评论(0编辑  收藏  举报