#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <mainwindow.h>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
public slots:
void color_from();
private slots:
void on_open_file_Btn_clicked();
void on_save_file_Btn_clicked();
void on_set_font_Btn_clicked();
void on_backcolor_Btn_clicked();
private:
Ui::Form *ui;
MainWindow_color *w;
};
#endif
在这里插入代码片
#ifndef MainWindow_color_H
#define MainWindow_color_H
#include <QMainWindow>
#include <QPushButton>
#include "QDebug"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow_color; }
QT_END_NAMESPACE
class MainWindow_color : public QMainWindow
{
Q_OBJECT
public:
MainWindow_color(QWidget *parent = nullptr);
~MainWindow_color();
signals:
void color_enter();
private:
Ui::MainWindow_color *ui;
QPushButton *btn[10];
int curBtn;
int beforeBtn;
public slots:
void readSlide();
void setSlide();
void cBtn1Click();
void cBtn2Click();
void cBtn3Click();
void cBtn4Click();
void cBtn5Click();
void cBtn6Click();
void cBtn7Click();
void cBtn8Click();
void cBtn9Click();
void cBtn10Click();
void cBtnChanged(int i);
void changeCur();
signals:
void cBtn1Clicked(int value);
void cBtn2Clicked(int value);
void cBtn3Clicked(int value);
void cBtn4Clicked(int value);
void cBtn5Clicked(int value);
void cBtn6Clicked(int value);
void cBtn7Clicked(int value);
void cBtn8Clicked(int value);
void cBtn9Clicked(int value);
void cBtn10Clicked(int value);
private slots:
void on_enter_BTN_clicked();
};
#endif
在这里插入代码片
#include "form.h"
#include "ui_form.h"
#include <QGridLayout>
#include <QDialog>
#include <QFontDialog>
#include <QDebug>
#include <QColorDialog>
#include <QFileDialog>
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
w = new MainWindow_color(this);
connect(w,&MainWindow_color::color_enter,this,&Form::color_from);
QGridLayout *grid = new QGridLayout(this);
grid->addWidget(ui->text_Browser);
grid->addWidget(ui->backcolor_Btn);
grid->addWidget(ui->open_file_Btn);
grid->addWidget(ui->save_file_Btn);
grid->addWidget(ui->set_font_Btn);
this->move(300,0);
this->setWindowTitle("文本编辑器");
this->setWindowIcon(QIcon(":/new/prefix1/txt.png"));
QFile file("F:/qtprojectc++/qtworkspace/work_7_3/color_save.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray allline = file.readAll();
int ret = allline.indexOf(";",5);
allline.remove(ret+1,1);
QString color ="\"";
color +=allline;
color +="\"";
qDebug() <<allline <<" ALLLINE ";
ui->text_Browser->setStyleSheet(allline);
QFile file_font("F:/qtprojectc++/qtworkspace/work_7_3/font_save.txt");
if (!file_font.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray allline_font = file_font.readAll();
QFont font = QFont(allline_font);
font.fromString(allline_font);
ui->text_Browser->setFont(font);
}
Form::~Form()
{
delete ui;
}
void Form::color_from()
{
qDebug() << "color_from";
QFile file("F:/qtprojectc++/qtworkspace/work_7_3/color_save.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray allline = file.readAll();
int ret = allline.indexOf(";",5);
allline.remove(ret+1,1);
QString color ="\"";
color +=allline;
color +="\"";
qDebug() <<allline <<" ALLLINE ";
ui->text_Browser->setStyleSheet(allline);
file.close();
}
void Form::on_open_file_Btn_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this);
QFile file(fileName);
qDebug() <<fileName <<"filename" ;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QByteArray allline = file.readAll();
ui->text_Browser->setPlainText(allline);
file.close();
}
void Form::on_save_file_Btn_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this);
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QString data = ui->text_Browser->toPlainText();
file.write(data.toUtf8());
file.close();
}
void Form::on_set_font_Btn_clicked()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, QFont(),this,"字体设置");
if (ok) {
ui->text_Browser->setFont(font);
qDebug() << font << "ok" <<ok;
QFile file("F:/qtprojectc++/qtworkspace/work_7_3/font_save.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
file.resize(0);
file.write(font.toString().toUtf8());
file.close();
} else {
qDebug() <<ok;
}
}
void Form::on_backcolor_Btn_clicked()
{
this->hide();
w->show();
}
在这里插入代码片
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <string>
#include <QFile>
MainWindow_color::MainWindow_color(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow_color)
{
curBtn =0;
ui->setupUi(this);
btn[0]=ui->cBtn_1;
ui->cBtn_1->setChecked(true);
qDebug() << "-----++++0------" <<btn[0]->isChecked() ;
btn[1]=ui->cBtn_2;
btn[2]=ui->cBtn_3;
btn[3]=ui->cBtn_4;
btn[4]=ui->cBtn_5;
btn[5]=ui->cBtn_6;
btn[6]=ui->cBtn_7;
btn[7]=ui->cBtn_8;
btn[8]=ui->cBtn_9;
btn[9]=ui->cBtn_10;
connect(ui->redHorSlider,&QAbstractSlider::sliderMoved,this,&MainWindow_color::readSlide);
connect(ui->greenHorSlider,&QAbstractSlider::sliderMoved,this,&MainWindow_color::readSlide);
connect(ui->blueHorSlider,&QAbstractSlider::sliderMoved,this,&MainWindow_color::readSlide);
connect(ui->redLdt,&QLineEdit::textEdited,this,&MainWindow_color::setSlide);
connect(ui->greenLdt,&QLineEdit::textEdited,this,&MainWindow_color::setSlide);
connect(ui->blueLdt,&QLineEdit::textEdited,this,&MainWindow_color::setSlide);
connect(ui->cBtn_1,&QPushButton::clicked,this,&MainWindow_color::cBtn1Click);
connect(ui->cBtn_2,&QPushButton::clicked,this,&MainWindow_color::cBtn2Click);
connect(ui->cBtn_3,&QPushButton::clicked,this,&MainWindow_color::cBtn3Click);
connect(ui->cBtn_4,&QPushButton::clicked,this,&MainWindow_color::cBtn4Click);
connect(ui->cBtn_5,&QPushButton::clicked,this,&MainWindow_color::cBtn5Click);
connect(ui->cBtn_6,&QPushButton::clicked,this,&MainWindow_color::cBtn6Click);
connect(ui->cBtn_7,&QPushButton::clicked,this,&MainWindow_color::cBtn7Click);
connect(ui->cBtn_8,&QPushButton::clicked,this,&MainWindow_color::cBtn8Click);
connect(ui->cBtn_9,&QPushButton::clicked,this,&MainWindow_color::cBtn9Click);
connect(ui->cBtn_10,&QPushButton::clicked,this,&MainWindow_color::cBtn10Click);
connect(this,&MainWindow_color::cBtn1Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn2Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn3Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn4Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn5Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn6Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn7Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn8Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn9Clicked,this,&MainWindow_color::cBtnChanged);
connect(this,&MainWindow_color::cBtn10Clicked,this,&MainWindow_color::cBtnChanged);
connect(ui->changeBtn,&QPushButton::clicked,this,&MainWindow_color::changeCur);
}
MainWindow_color::~MainWindow_color()
{
delete ui;
}
void MainWindow_color::readSlide()
{
int red=ui->redHorSlider->value();
int green=ui->greenHorSlider->value();
int blue=ui->blueHorSlider->value();
ui->redLdt->setText(QString::number(red));
ui->greenLdt->setText(QString::number(green));
ui->blueLdt->setText(QString::number(blue));
QString color=QString("background-color:rgb(%1,%2,%3);").arg(red).arg(green).arg(blue);
ui->colorLab->setStyleSheet(color);
}
void MainWindow_color::setSlide()
{
int red=ui->redLdt->text().toInt();
int green=ui->greenLdt->text().toInt();
int blue=ui->blueLdt->text().toInt();
ui->redHorSlider->setSliderPosition(red);
ui->greenHorSlider->setSliderPosition(green);
ui->blueHorSlider->setSliderPosition(blue);
QString color=QString("background-color:rgb(%1,%2,%3);").arg(red).arg(green).arg(blue);
ui->colorLab->setStyleSheet(color);
}
void MainWindow_color::cBtn1Click()
{
emit cBtn1Clicked(0);
}
void MainWindow_color::cBtn2Click()
{
emit cBtn2Clicked(1);
}
void MainWindow_color::cBtn3Click()
{
emit cBtn3Clicked(2);
}
void MainWindow_color::cBtn4Click()
{
emit cBtn4Clicked(3);
}
void MainWindow_color::cBtn5Click()
{
emit cBtn5Clicked(4);
}
void MainWindow_color::cBtn6Click()
{
emit cBtn6Clicked(5);
}
void MainWindow_color::cBtn7Click()
{
emit cBtn7Clicked(6);
}
void MainWindow_color::cBtn8Click()
{
emit cBtn8Clicked(7);
}
void MainWindow_color::cBtn9Click()
{
emit cBtn9Clicked(8);
}
void MainWindow_color::cBtn10Click()
{
emit cBtn10Clicked(9);
}
void MainWindow_color::cBtnChanged(int i)
{
qDebug() << "------------" ;
qDebug() << "-----++++------" <<btn[i]->isChecked() ;
if( curBtn == i &&btn[i]->isChecked() == true)
{
curBtn = i;
}
else {
btn[curBtn]->setChecked(false);
curBtn = i;
btn[i]->setChecked(true);
}
btn[curBtn]->setChecked(true);
std::string oriColor,oriRed,oriGreen,oriBlue;
oriColor=btn[i]->styleSheet().toStdString();
qDebug() << btn[i]->styleSheet();
unsigned int num=0,a=0,c=0;
unsigned int b[2];
int j=0;
for(;num<oriColor.size();num++)
{
if(oriColor[num]=='(')
a=num;
if(oriColor[num]==',')
{
b[j]=num;
qDebug() << " j=" << j << endl;
j++;
}
if(oriColor[num]==')')
{
c=num;
}
}
for(unsigned int x=0;(a+1)<=b[0];a++,x++)
{
oriRed[x]=oriColor[a+1];
}
for(unsigned int x=0;(b[0]+1)<=b[1];b[0]++,x++)
{
oriGreen[x]=oriColor[b[0]+1];
}
for(unsigned int x=0;(b[1]+1)<=c;b[1]++,x++)
{
oriBlue[x]=oriColor[b[1]+1];
}
int R=stoi(oriRed);
int G=stoi(oriGreen);
int B=stoi(oriBlue);
ui->colorLab->setStyleSheet(QString::fromStdString(oriColor));
ui->redHorSlider->setSliderPosition(R);
ui->greenHorSlider->setSliderPosition(G);
ui->blueHorSlider->setSliderPosition(B);
ui->redLdt->setText(QString::number(R));
ui->greenLdt->setText(QString::number(G));
ui->blueLdt->setText(QString::number(B));
}
void MainWindow_color::changeCur()
{
QString color;
color=ui->colorLab->styleSheet();
btn[curBtn]->setStyleSheet(color);
btn[curBtn]->setChecked(false);
if(++curBtn ==10)
{
curBtn = 0;
btn[curBtn]->setChecked(true);
}
else {
btn[curBtn]->setChecked(true);
}
}
void MainWindow_color::on_enter_BTN_clicked()
{
QString color = ui->colorLab->styleSheet();
QFile file("F:/qtprojectc++/qtworkspace/work_7_3/color_save.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
file.resize(0);
file.write(color.toUtf8());
file.close();
this->parentWidget()->show();
emit color_enter();
this->hide();
}
在这里插入代码片
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form w;
w.show();
return a.exec();
}
在这里插入代码片
``
`
效果图:会默认加载上次的字体和颜色
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200705001948726.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ2NjE1MTAx,size_16,color_FFFFFF,t_70#pic_center)