QT实现截图功能
最近突发奇想,想实现一个截图软件,于是使用QT写了一个基本的截图软件功能,后续会在优化,接下来我先介绍一下自己的实现思路
主体部分
主体部分就是实现一个遮罩和截图框,加上一个横向的菜单。
遮罩和截图框使用两个pixmap实现。实现一个Screen类,用来控制截图框的大小
#include <QObject> #include <QSize> #include <QPoint> class TurboScreen : public QObject { Q_OBJECT Q_PROPERTY(QPoint pos_left_up_ READ getPosLeftUp WRITE setPosLeftUp USER true); Q_PROPERTY(QPoint pos_right_down_ READ getPosLeftUp WRITE setPosLeftUp USER true); Q_PROPERTY(QPoint mouse_start_pos_ READ getMouseStartPos WRITE setMouseStartPos USER true); Q_PROPERTY(QPoint mouse_end_pos_ READ getMouseEndPos WRITE setMouseEndPos USER true); Q_PROPERTY(int max_width_ READ getMaxWidth WRITE setMaxWidth USER true); Q_PROPERTY(int max_height_ READ getMaxHeight WRITE setMaxHeight USER true); public: enum STATUS { emSELECT, emMOV, emDRAW }; explicit TurboScreen(QObject *parent = 0); explicit TurboScreen(const QSize &size, QObject *parent = 0); ~TurboScreen() override; void setPosLeftUp(const QPoint &point); QPoint getPosLeftUp(); void setPosRightDown(const QPoint &point); QPoint getPosRightDown(); void setMouseStartPos(const QPoint &point); QPoint getMouseStartPos(); void setMouseEndPos(const QPoint &point); QPoint getMouseEndPos(); void setMaxWidth(const int &value); int getMaxWidth(); void setMaxHeight(const int &value); int getMaxHeight(); void setStatus(const STATUS &status); STATUS getStatus(); bool checkIsInArea(const QPoint &pos); void move(const QPoint &p); protected: void comparePoints(QPoint &leftTop, QPoint &rightDown); private: QPoint pos_left_up_; QPoint pos_right_down_; QPoint mouse_start_pos_; QPoint mouse_end_pos_; int max_width_{0}; int max_height_{0}; STATUS status_; };
截图主体类的设计
class TurboScreenCut : public QWidget { Q_OBJECT public: explicit TurboScreenCut(QWidget *parent = nullptr); static TurboScreenCut & instance(); ~TurboScreenCut() override; void saveCutScreen(); void drawPaintbrush(); void drawEllipse(); void drawRect(); void drawMosaic(); void drawArrow(); void setCurrentCode(TurboShape::Code code); protected: void showEvent(QShowEvent *event) override; void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void keyPressEvent(QKeyEvent *event) override; bool event(QEvent *event) override; private: TurboScreen *screen_{nullptr}; QPixmap *blur_pix_{nullptr}; //模糊背景图 QPixmap *full_pix_{nullptr}; //全屏图像 QPixmap mosaic_pix_; QPoint mov_pos_; //坐标 QPoint rect_left_pos_; //坐标 TurboMenu *menu_; QList<TurboShape*> shape_list_; TurboShape *shape_{nullptr}; TurboShape::Code current_code_; };
横向菜单的设计
横向菜单是使用QWidget加按钮做成的
Menu主体代码:
void TurboMenu::initUi() { QFrame *frame = new QFrame(this); QHBoxLayout *layout1 = new QHBoxLayout(this); QHBoxLayout *layout2 = new QHBoxLayout(this); frame->setLayout(layout1); setLayout(layout2); QPushButton *btnRect = new QPushButton(this); btnRect->setIcon(QIcon(":resources/image/rectangle.png")); btnRect->setIconSize(QSize(16, 16)); connect(btnRect, &QPushButton::clicked, this, &TurboMenu::btnRectClicked); QPushButton *btnEllipse = new QPushButton(this); btnEllipse->setIcon(QIcon(":resources/image/ellipse.png")); btnEllipse->setIconSize(QSize(16, 16)); connect(btnEllipse, &QPushButton::clicked, this, &TurboMenu::btnEllipseClicked); QPushButton *btnArrow = new QPushButton(this); btnArrow->setIcon(QIcon(":resources/image/arrow.png")); btnArrow->setIconSize(QSize(16, 16)); connect(btnArrow, &QPushButton::clicked, this, &TurboMenu::btnArrowClicked); QPushButton *btnPaintbrush = new QPushButton(this); btnPaintbrush->setIcon(QIcon(":resources/image/paintbrush.png")); btnPaintbrush->setIconSize(QSize(16, 16)); connect(btnPaintbrush, &QPushButton::clicked, this, &TurboMenu::btnPaintbrushClicked); QPushButton *btnMosaic = new QPushButton(this); btnMosaic->setIcon(QIcon(":resources/image/mosaic.png")); btnMosaic->setIconSize(QSize(16, 16)); connect(btnMosaic, &QPushButton::clicked, this, &TurboMenu::btnMosaicClicked); QPushButton *btnSave = new QPushButton(this); btnSave->setIcon(QIcon(":resources/image/save.png")); btnSave->setIconSize(QSize(16, 16)); connect(btnSave, &QPushButton::clicked, this, &TurboMenu::btnSaveClicked); QPushButton *btnQuit = new QPushButton(this); btnQuit->setIcon(QIcon(":resources/image/close.png")); btnQuit->setIconSize(QSize(16, 16)); connect(btnQuit, &QPushButton::clicked, this, &TurboMenu::btnQuitClicked); layout1->addWidget(btnRect); layout1->addWidget(btnEllipse); layout1->addWidget(btnArrow); layout1->addWidget(btnPaintbrush); layout1->addWidget(btnMosaic); layout1->addWidget(btnSave); layout1->addWidget(btnQuit); layout2->addWidget(frame); QFile file(":resources/style/menu.qss"); file.open(QIODevice::ReadOnly); QByteArray array = file.readAll(); file.close(); setStyleSheet(array); }
绘制类
基类设计
class TurboShape { public: enum Code { emLine, emRect, emEllipse, emArrow, emMosaic }; TurboShape(); ~TurboShape(); void setPosLeftUp(const QPoint &point); QPoint getPosLeftUp(); void setPosRightDown(const QPoint &point); QPoint getPosRightDown(); void setMovePoint(const QPoint &point); virtual void pushLinePoint(const QPoint &point){}; void setMoveStatus(const bool &status); QVector<QPoint> getArrowShape(); virtual void setMosaic(const QPixmap &map){}; virtual QPixmap getMosaic(){ return QPixmap(); }; virtual void paint(QPainter & painter) = 0; protected: QPoint left_up_pos_; QPoint right_down_pos_; QPoint move_point_; bool move_status_; };
各种形状的设计
class Line : public TurboShape { public: Line(); void paint(QPainter &painter) override; void pushLinePoint(const QPoint &point); private: QVector<QPoint> line_points_; }; class Rect : public TurboShape { public: Rect(); void paint(QPainter &painter); }; class Ellipse : public TurboShape { public: Ellipse(); void paint(QPainter &painter); }; class Arrow : public TurboShape { public: Arrow(); void paint(QPainter &painter); void pushLinePoint(const QPoint &point); private: QVector<QPoint> line_points_; }; class Mosaic : public TurboShape { public: Mosaic(); void paint(QPainter &painter); void pushLinePoint(const QPoint &point) override; void setMosaic(const QPixmap &map) override; QPixmap getMosaic() override; private: QPixmap mosaic_map_; QVector<QPoint> line_points_; };
最终效果展示
具体实现代码暂不展示,有需要的话请三连加私信。