本文学习如何创建一个Qt绘制程序,用户将能够通过使用不同的尺寸和画笔的颜色来表达他们的创造力。
主要功能:保存画板内容为图片、清除画板内容、设置画板大小、设置画笔颜色
新建基于QMainWindow的应用程序,设置MainWindow.ui
代码如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> #include <QPainter> namespace Ui { class MainWindow : public QMainWindow public: virtual void mousePressEvent(QMouseEvent *event); private slots: private: #endif // MAINWINDOW_H |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#include "MainWindow.h"
#include "ui_MainWindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : // create a QImage object, which acts as the canvas QImage tux; drawing = false; } MainWindow::~MainWindow() void MainWindow::mousePressEvent(QMouseEvent *event) } void MainWindow::mouseMoveEvent(QMouseEvent *event) } void MainWindow::mouseReleaseEvent(QMouseEvent *event) } void MainWindow::paintEvent(QPaintEvent *event) void MainWindow::resizeEvent(QResizeEvent *event) } void MainWindow::on_action_2px_triggered() void MainWindow::on_action_5px_triggered() void MainWindow::on_action_10px_triggered() void MainWindow::on_action_Black_triggered() void MainWindow::on_action_White_triggered() void MainWindow::on_action_Red_triggered() void MainWindow::on_action_Green_triggered() void MainWindow::on_action_Blue_triggered() void MainWindow::on_action_Save_triggered() void MainWindow::on_action_Clear_triggered() |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include "MainWindow.h"
#include <QApplication> int main(int argc, char *argv[]) MainWindow w; return a.exec(); |
编译运行,*绘制吧!