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);
}
2022-05-07 17:19:03
18.73MB
C++
Qt
1