This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/SoftwareDesign/Lab5/lab5_1_painter/mainwindow.cpp

72 lines
1.8 KiB
C++
Raw Normal View History

2022-05-31 09:21:56 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColorDialog>
#include <QDebug>
#include <QDialog>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <QProgressDialog>
bool enable = false;
bool drawing = false;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
this->setProperty("canMove", false);
resize(600, 500); //窗口大小设置为600*500
pix = QPixmap(600, 500);
pix.fill(Qt::white);
}
MainWindow::~MainWindow() { delete ui; }
void MainWindow::on_styCor_triggered() //边框颜色
{
QPainter pp(&pix); // 根据鼠标指针前后两个位置就行绘制直线
pp.drawLine(
lastPoint,
endPoint); // 让前一个坐标值等于后一个坐标值,这样就能实现画出连续的线
lastPoint = endPoint;
QPainter painter(this);
painter.drawPixmap(0, 0, pix);
}
void MainWindow::paintEvent(QPaintEvent *event) {
if (enable)
switch (drawStatus) {
case line:
QPainter pp(&pix); // 根据鼠标指针前后两个位置就行绘制直线
pp.drawLine(
lastPoint,
endPoint); // 让前一个坐标值等于后一个坐标值,这样就能实现画出连续的线
enable = false;
}
QPainter painter(this);
painter.drawPixmap(0, 0, pix);
drawStatus = idle;
}
void MainWindow::drawLineTriger() { drawStatus = line; }
void MainWindow::on_drawLine_triggered() {
enable = true;
drawStatus = line;
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) //鼠标左键按下
if (drawing) {
endPoint = event->pos();
drawing = false;
enable = true;
update();
} else {
drawing = true;
lastPoint = event->pos();
}
}