使用Qt实现的C++画板(包含源码,可执行exe文件)

时间:2023-06-25 02:51:37
【文件属性】:

文件名称:使用Qt实现的C++画板(包含源码,可执行exe文件)

文件大小:18.73MB

文件格式:ZIP

更新时间:2023-06-25 02:51:37

C++ Qt

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // 给pixmap赋值 pixmap = new QPixmap(900, 600); //设置背景色 pixmap->fill(Qt::white); //根据pixmap创建一个qpaiter painter= new QPainter(pixmap); // 使用 pixmap,也就是把这个控件加到ui里面 ui->pixmap_label->setPixmap(*pixmap); //这里关联信号和槽 connect(ui->linecolor_r_slider,SIGNAL(valueChanged(int)),this,SLOT(linecolor_changed())); connect(ui->linecolor_g_slider,SIGNAL(valueChanged(int)),this,SLOT(linecolor_changed())); connect(ui->linecolor_b_slider,SIGNAL(valueChanged(int)),this,SLOT(linecolor_changed())); connect(ui->fillcolor_comboBox,SIGNAL(currentTextChanged(QString)),this,SLOT(fillcolor_changed())); //这里初始化填充颜色下拉框: ui->fillcolor_comboBox->addItem("黑色"); ui->fillcolor_comboBox->addItem("白色"); ui->fillcolor_comboBox->addItem("红色"); ui->fillcolor_comboBox->addItem("绿色"); ui->fillcolor_comboBox->addItem("蓝色"); ui->fillcolor_comboBox->addItem("黄色"); } MainWindow::~MainWindow() { delete ui; delete pixmap; delete painter; delete linecolor;delete fillcolor; delete circle,rect; } //这个函数是生成按钮被按下时的画图实现: void MainWindow::on_draw_button_clicked() { if (ui->tabWidget->currentIndex()==0)//这个是画圆的部分 { circle =new Circle(new QPoint(ui->circle_x_spinBox->value(),ui->circle_y_spinBox->value()),ui->circle_r_spinBox->value()); circle->draw(painter); } else if (ui->tabWidget->currentIndex()==1){ rect=new Rect(new QPoint(ui->rect_x_spinBox->value(),ui->rect_y_spinBox->value()),ui->rect_a_spinBox->value(),ui->rect_b_spinBox->value()); rect->draw(painter); } ui->pixmap_label->setPixmap(*pixmap); } //这个函数就是设置边线颜色的: void MainWindow::linecolor_changed(){ linecolor=new QColor(ui->linecolor_r_slider->value(),ui->linecolor_g_slider->value(),ui->linecolor_b_slider->value()); painter->setPen(*linecolor); QPalette label_palette; label_palette.setColor(QPalette::Background, *linecolor); ui->linecolor_display_label->setAutoFillBackground(true); ui->linecolor_display_label->setPalette(label_palette); } void MainWindow::fillcolor_changed(){ QString str=ui->fillcolor_comboBox->currentText(); if (str=="黑色"){ fillcolor =new QColor(Qt::black); } else if (str=="蓝色"){ fillcolor =new QColor(Qt::blue); } else if (str=="黄色"){ fillcolor =new QColor(Qt::yellow); } else if (str=="白色"){ fillcolor =new QColor(Qt::white); } else if (str=="绿色"){ fillcolor =new QColor(Qt::green); } else if (str=="红色"){ fillcolor =new QColor(Qt::red); } painter->setBrush(QBrush(*fillcolor)); } //这个函数一不小心搞出来,没用,删了会报错? void MainWindow::on_linecolor_r_slider_actionTriggered(int action) { } //这个函数就是用来保存的功能实现: void MainWindow::on_save_button_clicked() { ofstream out("D://data.txt", ios::out|ios::app); if (ui->tabWidget->currentIndex()==0)//此处保存圆 { out<<"circle"<center->x()<<" "<center->y()<<" "<r<tabWidget->currentIndex()==1)//此处保存矩形 { out<<"rect"<northeast->x()<<" "<northeast->y()<<" "<width<<" "<height<<" "<red()<<" "<green()<<" "<blue()<<" "<red()<<" "<green()<<" "<blue()<<" "<>str){ if (str=="rect"){ infile>>z>>a>>b>>c>>d; rect = new Rect(new QPoint(a,b),c,d); infile>>a>>b>>c; linecolor=new QColor(a,b,c); painter->setPen(*linecolor); infile>>a>>b>>c; fillcolor=new QColor(a,b,c); painter->setBrush(QBrush(*fillcolor)); rect->draw(painter); } else{ infile>>z>>a>>b>>c; circle=new Circle(new QPoint(a,b),c); infile>>a>>b>>c; linecolor=new QColor(a,b,c); painter->setPen(*linecolor); infile>>a>>b>>c; fillcolor=new QColor(a,b,c); painter->setBrush(QBrush(*fillcolor)); circle->draw(painter); } ui->pixmap_label->setPixmap(*pixmap); } infile.close();} void MainWindow::on_clear_button_clicked() { pixmap->fill(Qt::white); ui->pixmap_label->setPixmap(*pixmap); }


【文件预览】:
C++画板
----src()
--------mainwindow.ui(14KB)
--------Lab3.pro.user(23KB)
--------circle.h(269B)
--------circle.cpp(315B)
--------mainwindow.cpp(5KB)
--------rect.h(304B)
--------main.cpp(183B)
--------rect.cpp(244B)
--------shape.h(162B)
--------Lab3.pro(1KB)
--------mainwindow.h(1KB)
--------shape.cpp(67B)
----exe所在目录()
--------Qt5Svg.dll(352KB)
--------libGLESV2.dll(2.68MB)
--------moc_mainwindow.cpp(5KB)
--------main.o(1KB)
--------Qt5Widgets.dll(6.06MB)
--------iconengines()
--------libwinpthread-1.dll(78KB)
--------Qt5Gui.dll(5.92MB)
--------Qt5Core.dll(5.81MB)
--------circle.o(2KB)
--------mainwindow.o(57KB)
--------Lab3.exe(56KB)
--------moc_predefs.h(10KB)
--------libgcc_s_dw2-1.dll(118KB)
--------libEGL.dll(22KB)
--------moc_mainwindow.o(10KB)
--------platforms()
--------shape.o(1KB)
--------opengl32sw.dll(15.25MB)
--------Lab3.exe - 快捷方式.lnk(739B)
--------D3Dcompiler_47.dll(3.52MB)
--------translations()
--------libstdc++-6.dll(1.47MB)
--------imageformats()
--------rect.o(2KB)
----Lab3.exe - 快捷方式.lnk(804B)

网友评论