#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "donewdialog.h"
#include "QMessageBox"
#include <QFileDialog>
#include <QPainter>
//#include <QColor>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// curFile=tr("未命名.png");
setWindowTitle("Qt Painting Board");//设置窗口标题
setFixedSize(700,500);//设置窗口大小
ui->action1_3->setShortcut(tr("Alt+F4"));//快速退出
area=new PaintArea;
scrollArea=new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(area);
scrollArea->widget()->setMinimumSize(700,600);
ui->dockWidget->hide();
setCentralWidget(scrollArea);
isSaved=false;
creatColorComboBox(ui->penColorComboBox);
creatColorComboBox(ui->brushColorComboBox);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)//改变事件响应
{
QMainWindow::changeEvent(e);
switch(e->type()){
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::doOpen()//打开文件操作
{
if(maybeSave())
{
QString fileName=QFileDialog::getOpenFileName(this,
tr("打开文件"),
QDir::currentPath(),
tr("Images (*.png *.jpg *.jpeg *.ppm *.tiff *.xbm *.xpm)"));
if(!fileName.isEmpty())
{
area->openImage(fileName);
scrollArea->widget()->resize(area->getImageSize());
isSaved=true;
curFile=fileName;
}
}
}
void MainWindow::doNew()
{
if(maybeSave())
{
DoNewDialog dlg;
if(dlg.exec()==QDialog::Accepted)
{
int width=dlg.getWidth();
int height=dlg.getHeight();
area->setImageSize(width,height);
scrollArea->widget()->resize(width,height);
area->setImageColor(dlg.getBackColor());
area->seteraserColor(dlg.getBackColor());
curFile=dlg.getfileName();
setWindowTitle("Qt Painting Board");
isSaved=false;
}
}
}
bool MainWindow::maybeSave()//退出前是否保存图像
{
if(area->isModified())
{
QMessageBox::StandardButton box;
box=QMessageBox::warning(this,tr("保存文件"),
tr("图片已经改变,是否保存?"),
QMessageBox::Yes|
QMessageBox::Discard|
QMessageBox::Cancel);
if(box==QMessageBox::Yes)
{
return doFileSave();
}
else if(box==QMessageBox::Cancel)
{
return false;
}
}
return true;
}
bool MainWindow::doFileSave()
{
if(isSaved)
{
return saveFile(curFile);
}
else
return doFileSaveAs();
}
bool MainWindow::saveFile(QString fileName)
{
if(area->saveImage(fileName,"png"))
{
isSaved=true;
return true;
}
else
return false;
}
bool MainWindow::doFileSaveAs()
{//*.png *.jpg *.jpeg *.ppm *.tiff *.xbm *.xpm
QString fileName=QFileDialog::getSaveFileName(this,
tr("save as"),curFile,
tr("PNG(*.png);;JPG(*.jpg);;JPEG(*.jpeg);;PPM(*.ppm);;TIFF(*.tiff);;XBM(*.xbm);;XPM(*.xpm)"));
if(fileName.isEmpty())
{
return false;
}
else
return saveFile(fileName);
}
void MainWindow::creatColorComboBox(QComboBox *comboBox)//颜色选择下拉列表
{
QPixmap pix(16,16);
QPainter painter(&pix);
painter.fillRect(0,0,16,16,Qt::black);
comboBox->addItem(QIcon(pix),tr("黑色"),QColor(Qt::black));
painter.fillRect(0,0,16,16,Qt::white);
comboBox->addItem(QIcon(pix),tr("白色"),QColor(Qt::white));
painter.fillRect(0,0,16,16,Qt::red);
comboBox->addItem(QIcon(pix),tr("红色"),QColor(Qt::red));
painter.fillRect(0,0,16,16,Qt::blue);
comboBox->addItem(QIcon(pix),tr("蓝色"),QColor(Qt::blue));
painter.fillRect(0,0,16,16,Qt::green);
comboBox->addItem(QIcon(pix),tr("绿色"),QColor(Qt::green));
painter.fillRect(0,0,16,16,Qt::yellow);
comboBox->addItem(QIcon(pix),tr("黄色"),QColor(Qt::yellow));
comboBox->addItem(tr("no color"),QColor(Qt::transparent));
}
void MainWindow::on_action_triggered()
{
doNew();
}
void MainWindow::on_actionFd_triggered()
{
doOpen();
}
void MainWindow::on_action1_triggered()
{
doFileSave();
}
void MainWindow::on_action1_2_triggered()
{
doFileSaveAs();
}
void MainWindow::on_action1_3_triggered()
{
if(maybeSave())
qApp->quit();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(maybeSave())
qApp->quit();
else
event->ignore();
}
void MainWindow::on_action2_triggered()
{
area->doPrint();
}
void MainWindow::on_action1_14_triggered()
{
aboutDialog a;
a.exec();
}
void MainWindow::on_action1_4_triggered()
{
area->zoomIn();
scrollArea->widget()->resize(area->getImageSize());
}
void MainWindow::on_action2_2_triggered()
{
area->zoomOut();
}
void MainWindow::on_action3_triggered()
{
area->zoom_1();
}
void MainWindow::on_action1_5_triggered()
{
area->doRotate();
}
void MainWindow::on_action1_6_triggered()
{
area->doShear();
}
void MainWindow::on_action1_7_triggered()
{
area->doClear();
}
void MainWindow::on_action1_15_triggered()
{
ui->dockWidget->show();
}
void MainWindow::on_shapeComboBox_currentIndexChanged(QString shape)
{
if(shape==tr("画笔"))
area->setShape(PaintArea::None);
else if(shape==tr("矩形"))
area->setShape(PaintArea::Rectangle);
else if(shape==tr("直线"))
area->setShape(PaintArea::Line);
else if(shape==tr("椭圆"))
area->setShape(PaintArea::Ellipse);
}
void MainWindow::on_penStyleComboBox_currentIndexChanged(QString style)
{
if(style==tr("实线"))
{
area->setPenStyle(Qt::SolidLine);
}
else if(style==tr("虚线"))
{
area->setPenStyle(Qt::DotLine);
}
}
void MainWindow::on_penWidthSpinBox_valueChanged(int width)
{
area->setPenWidth(width);
}
void MainWindow::on_penColorComboBox_currentIndexChanged(int index)
{
QColor color=ui->penColorComboBox->itemData(index,Qt::UserRole).value<QColor>();
area->setPenColor(color);
}
void MainWindow::on_brushColorComboBox_currentIndexChanged(int index)
{
QColor color=ui->brushColorComboBox->itemData(index,Qt::UserRole).value<QColor>();
area->setBrushColor(color);
}
void MainWindow::on_action1_9_triggered()
{
ui->shapeComboBox->setCurrentIndex(1);
//area->setShape(PaintArea::Line);
}
void MainWindow::on_action1_10_triggered()
{
ui->shapeComboBox->setCurrentIndex(2);
//area->setShape(PaintArea::Rectangle);
}
void MainWindow::on_action1_8_triggered()
{
ui->shapeComboBox->setCurrentIndex(0);
//area->setShape(PaintArea::None);
}
void MainWindow::on_action1_11_triggered()
{
ui->shapeComboBox->setCurrentIndex(3);
//area->setShape(PaintArea::Ellipse);
}
void MainWindow::on_action1_12_triggered()
{
ui->penStyleComboBox->setCurrentIndex(1);
//area->setPenStyle(Qt::DotLine);
}
void MainWindow::on_action1_13_triggered()
{
ui->penStyleComboBox->setCurrentIndex(0);
//area->setPenStyle(Qt::SolidLine);
}
void MainWindow::on_actionShootcut_toolbar_triggered()
{
ui->dockWidget_2->show();
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于qt实现的画图板,下面有详细说明
共45个文件
png:21个
cpp:6个
h:5个
需积分: 0 0 下载量 4 浏览量
2024-01-16
12:14:44
上传
评论
收藏 603KB ZIP 举报
温馨提示
款软件基于Qt框架,实现了在线画图的基本功能。 主要功能: 选择不同颜色和粗细的画笔进行绘图。 支持填充选定区域。 支持导入图片作为绘图底图。 支持保存当前画布为图片文件。 提供基本的绘画工具如橡皮擦和撤销功能。 实现方法: 使用QPainter路径绘制实时线条。 设计画笔工具类封装各属性。 填充算法使用扫描线填充法。 采用Qt多媒体类加载和保存图片。 信号槽连接PaintWidget更新画布。 采用MVC模式分离界面和逻辑代码。 测试结果: 各功能按预期流畅运行。 不同分辨率下界面自适应很好。 代码结构清晰规范,具有很好的扩展性。
资源推荐
资源详情
资源评论
收起资源包目录
基于qt实现的画图板.zip (45个子文件)
基于qt实现的画图板
WX20240116-121152@2x.png 151KB
PaintingBoard
info
information.qrc 230B
9e9b330465a2a8b0db3a2d396f69b0c8.jpg 263KB
B4BCCC92AE96797F56E17DC337B52248.jpg 79KB
2cffb48f3ec098b8f11f3668.jpg 56KB
icon
旋转.png 588B
直线.png 434B
放大.png 622B
顺时针.png 703B
矩形.png 355B
逆时针.png 683B
icon.qrc 779B
虚线.png 412B
保存.png 854B
画笔.png 617B
橡皮.png 664B
关于.png 709B
新建.png 374B
缩小.png 667B
打开.png 637B
另存为.png 557B
拉伸.png 701B
还原.png 384B
QQ图片20141225230102.jpg 36KB
打印.png 582B
椭圆.png 684B
退出.png 646B
qtstudy1
paintarea.cpp 9KB
mainwindow.h 3KB
aboutdialog.ui 2KB
mainwindow.cpp 9KB
qtstudy1.pro.user 23KB
icon.qrc 725B
main.cpp 272B
userdialog.h 374B
mainwindow.ui 23KB
donewdialog.cpp 1KB
aboutdialog.h 386B
donewdialog.ui 4KB
userdialog.cpp 301B
qtstudy1.pro 710B
donewdialog.h 612B
aboutdialog.cpp 307B
userdialog.ui 3KB
paintarea.h 2KB
共 45 条
- 1
资源评论
m0_48799271
- 粉丝: 12
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功