这次是结合上次的色板,实现一个简单的txt文档编辑

在这里插入图片描述

#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 // FORM_H
在这里插入代码片
#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 // MainWindow_color_H
在这里插入代码片
#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();
        //allline.remove(0,1);
        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();
        //allline.remove(0,1);
        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;

//          while (!file.atEnd()) {
              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()
{
//    QFontDialog *font = new QFontDialog(this);
    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();

//     QColor color = QColorDialog::getColor(Qt::white, this,"背景颜色设置");
//    int red = color.red();
//    int green = color.green();
//    int blue = color.blue();
//    ui->text_Browser->setStyleSheet(QString("background-color:rgb(%1,%2,%3"
//                                            ");").arg(red).arg(green).arg(blue));
//    QFile file("F:/qtprojectc++/qtworkspace/work_7_3/color_save.txt");
//    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
//        return;
//    QString data = QString("background-color:rgb(%1,%2,%3"
//                           ");").arg(red).arg(green).arg(blue);
//    file.resize(0);
//    file.write(data.toUtf8());
//    file.close();
}
在这里插入代码片
#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()
{
    //qDebug() << "+++++++++++++"  ;
    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() << "------------"  ;
  // btn[curBtn]->setChecked(false);
     qDebug() << "-----++++------" <<btn[i]->isChecked()  ;

      //有阴影是true

      if( curBtn == i &&btn[i]->isChecked() == true)
      {
          //beforeBtn = curBtn;
          //btn[curBtn]->setChecked(false);
          curBtn = i;
      }
      else {
          //若选中不是同一个,之前的阴影消失
          btn[curBtn]->setChecked(false);
          curBtn = i;
          btn[i]->setChecked(true);

      }

     // qDebug() << "-----++++------" << btn[curBtn]->isChecked() ;
   btn[curBtn]->setChecked(true);
      //qDebug() << "-----++++---++--" << btn[curBtn]->isChecked();
    std::string oriColor,oriRed,oriGreen,oriBlue;
    oriColor=btn[i]->styleSheet().toStdString();
    qDebug() << btn[i]->styleSheet();
    /*提取原来存放的RGB只*/
    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];
    }

    /* string to int */
    int R=stoi(oriRed);
    int G=stoi(oriGreen);
    int B=stoi(oriBlue);




//    if(R==255&&G==255&&B==255)
//    {
//        int red=ui->redLdt->text().toInt();
//        int green=ui->greenLdt->text().toInt();
//        int blue=ui->blueLdt->text().toInt();
//        QString color=QString("background-color:rgb(%1,%2,%3);").arg(red).arg(green).arg(blue);
//        btn[i]->setStyleSheet(color);
//    }
//    else {
        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);  //按下确定后,阴影消失,自动curBtn++
    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)






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值