Qt仿制Windows10记事本

使用Qt做的一个仿制windows10的简易记事本,只有部分功能和win10记事本有一点像.

先看效果图吧:

先使用Qt创建一个GUI widget Application 的 QMainWindow 项目,工程目录如下所示:

   main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QTranslator>
#include <QCoreApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTranslator* pTranslator = new QTranslator();
    pTranslator->load(":/res/qm/qt_zh_CN.qm");              //资源里需要有这个文件
    QCoreApplication::installTranslator(pTranslator);       //将字体对话框翻译成中文

    MainWindow w;
    w.show();

    return a.exec();
}

  mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>
#include <QLabel>
#include <QCoreApplication>
#include <QTranslator>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    bool saved;
    QString savePath;
    QLabel* first_statusLabel;      //声明两个标签对象,用于显示状态信息
    QLabel* second_statusLabel;
    QLabel* third_statusLabel;

    QString file_path;
    bool ctrl,ctrl2,ctrl3;
    QStringList s_str_list,s_str_list2,s_str_list3;
    bool m_bDisplayChinese;

    void init_statusBar();          //初始化状态栏

    void WriteIni(QString);                //创建ini文件
    void WriteIni(int,QString,bool,bool);                //创建ini文件
    void ReadIni();                 //读取ini文件

signals:
    void opened();

    void mySignal(QString title);

public slots:
    void onActionNewTriggered(bool checked);    //新建
    void onActionOpenTriggered(bool checked);   //打开
    void onActionSaveTriggered(bool checked);   //保存
    void onActionSaveasTriggered(bool checked); //另存为
    void onActionExitTriggered(bool checked);   //退出

    void onActionUndoTriggered(bool checked);   //撤销
    void onActionPasteTriggered(bool checked);  //粘贴
    void onActionRedoTriggered(bool checked);   //重做

    void onActionRemoveSpaceTriggered(bool checked);    //去掉换行
    void onActionSpaceTriggered(bool checked);          //去掉空格
    void onActionFontTriggered(bool checked);           //修改字体
    void onActionColorTriggered(bool checked);          //字体颜色
    void onActionBackGroundTriggered(bool checked);     //背景颜色
    void onActionCenterTriggered(bool checked);         //居中显示

    void onActionViewHelpTriggered(bool checked);       //查看帮助
    void switchLanguage();                              //切换语言
    void onActionAboutNotepadTriggered(bool checked);   //关于记事本

    void mySlot(QString title2);

private slots:
    void onEdited();
    void do_cursorChanged();        //获取光标位置信息
    void closeEvent(QCloseEvent* e);//关闭窗口事件
};

#endif // MAINWINDOW_H

  mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QTextCodec>
#include <QDesktopServices>
#include <QUrl>
#include <QFont>
#include <QFontDialog>
#include <QColorDialog>
#include <QTextStream>
#include <QSettings>
#pragma execution_character_set("utf-8");
#include "ColorDlg.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    saved(true),
    ctrl(false),
    ctrl2(false),
    ctrl3(false),
    m_bDisplayChinese(false)
{
    ui->setupUi(this);

    init_statusBar();                                       //初始化底部的状态栏
    this->setWindowTitle(tr("新建文本文档.txt - 记事本"));
    this->ui->textEdit->setStyleSheet("padding: -1");

    this->setCentralWidget(ui->textEdit);                   //去掉TextEdit的外边蓝色线
    ReadIni();                                              //读取配置文件

    this->connect(ui->actionNew,SIGNAL(triggered(bool)),this,SLOT(onActionNewTriggered(bool)));                 //新建
    this->connect(ui->actionOpen,SIGNAL(triggered(bool)),this,SLOT(onActionOpenTriggered(bool)));               //打开
    this->connect(ui->actionSave,SIGNAL(triggered(bool)),this,SLOT(onActionSaveTriggered(bool)));               //保存
    this->connect(ui->actionSaveas,SIGNAL(triggered(bool)),this,SLOT(onActionSaveasTriggered(bool)));           //另存为
    this->connect(ui->actionExit,SIGNAL(triggered(bool)),this,SLOT(onActionExitTriggered(bool)));               //退出
    this->connect(ui->actionPaste,SIGNAL(triggered(bool)),this,SLOT(onActionPasteTriggered(bool)));             //粘贴
    this->connect(ui->actionRedo,SIGNAL(triggered(bool)),this,SLOT(onActionRedoTriggered(bool)));               //重做
    this->connect(ui->actionUndo,SIGNAL(triggered(bool)),this,SLOT(onActionUndoTriggered(bool)));               //撤销
    this->connect(ui->actionRemoveSpace,SIGNAL(triggered(bool)),this,SLOT(onActionRemoveSpaceTriggered(bool))); //删除空格
    this->connect(ui->actionSpace,SIGNAL(triggered(bool)),this,SLOT(onActionSpaceTriggered(bool)));             //去掉空格
    this->connect(ui->actionFont,SIGNAL(triggered(bool)),this,SLOT(onActionFontTriggered(bool)));               //字体设置
    this->connect(ui->actionColor,SIGNAL(triggered(bool)),this,SLOT(onActionColorTriggered(bool)));             //字体颜色设置
    this->connect(ui->actionBackGround,SIGNAL(triggered(bool)),this,SLOT(onActionBackGroundTriggered(bool)));   //背景颜色设置
    this->connect(ui->actionCenter,SIGNAL(triggered(bool)),this,SLOT(onActionCenterTriggered(bool)));           //字体居中

    this->connect(ui->actionHelp,SIGNAL(triggered(bool)),this,SLOT(onActionViewHelpTriggered(bool)));           //帮助
    this->connect(ui->actionLanguage,SIGNAL(triggered(bool)),this,SLOT(switchLanguage()));                      //语言切换
    this->connect(ui->actionAboutNotepad,SIGNAL(triggered(bool)),this,SLOT(onActionAboutNotepadTriggered(bool)));//版本信息(随便写的)

    this->connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(do_cursorChanged()));                  //获得光标所在位置
    this->connect(ui->textEdit,SIGNAL(textChanged()),this,SLOT(onEdited()));                                    //断是否编辑了文件,若编辑了,退出时提示保存

    this->connect(this,SIGNAL(mySignal(QString)),SLOT(mySlot(QString)));                                        //为记事本添加标题
}

MainWindow::~MainWindow()
{
    delete ui;
    if(first_statusLabel)
        delete first_statusLabel;
    if(second_statusLabel)
        delete second_statusLabel;
    if(third_statusLabel)
        delete third_statusLabel;

    s_str_list.clear();
    s_str_list2.clear();
}

void MainWindow::init_statusBar()                           //底部的状态显示栏
{
    QStatusBar* bar = ui->statusBar;                        //获取状态栏
    first_statusLabel = new QLabel;                         //新建标签
    first_statusLabel->setMinimumSize(150,25);              //设置标签最小尺寸
//    first_statusLabel->setFrameShape(QFrame::WinPanel);   //设置便签形状(外边加一个黑框)
//    first_statusLabel->setFrameShadow(QFrame::Sunken);    //设置便签阴影

    second_statusLabel = new QLabel;
    second_statusLabel->setMinimumSize(80,25);

    third_statusLabel = new QLabel;
    third_statusLabel->setMinimumSize(100,25);

    bar->addPermanentWidget(first_statusLabel);
    bar->addWidget(first_statusLabel);
    bar->addPermanentWidget(second_statusLabel);
    bar->addWidget(second_statusLabel);
    bar->addPermanentWidget(third_statusLabel);
    bar->addWidget(third_statusLabel);

    first_statusLabel->setText(tr(" Windows(CRLF) "));    //初始化内容
    second_statusLabel->setText(tr(" 欢迎使用文本编辑器 "));
    third_statusLabel->setText(tr(" 慕华思弦 "));
}

void MainWindow::WriteIni(QString background)               //写入配置文件 背景颜色的设置...暂时未写
{
    QSettings *pIni = new QSettings(QCoreApplication::applicationDirPath()+"/config.ini",QSettings::IniFormat);

//    pIni->setValue("/color/text",colorText);
//    pIni->setValue("/color/highlight",highlight);
    pIni->setValue("/color/background",background);
//    pIni->setValue("/color/select",select);

    delete pIni;
}

void MainWindow::WriteIni(int pointsize, QString family, bool bold, bool italic)        //字体设置 写入配置文件
{
    QSettings *pIni = new QSettings(QCoreApplication::applicationDirPath()+"/config.ini",QSettings::IniFormat);
    pIni->setValue("/font/size",pointsize);
    pIni->setValue("/font/family",family);
    pIni->setValue("/font/bold",bold);
    pIni->setValue("/font/italic",italic);

    delete pIni;
}

void MainWindow::ReadIni()          //字体设置 读取配置信息
{
    QSettings *pIni = new QSettings(QCoreApplication::applicationDirPath()+"/config.ini",QSettings::IniFormat);

    int pointSize2 = pIni->value("/font/size").toInt();
    QString family = pIni->value("/font/family").toString();
    bool bold = pIni->value("/font/bold").toBool();
    bool italic = pIni->value("/font/italic").toBool();

    QString background = pIni->value("/color/background").toString();

    QFont font;
    font.setPointSize(pointSize2);
    font.setFamily(family);
    font.setBold(bold);
    font.setItalic(italic);
    ui->textEdit->setFont(font);

    if(!background.isEmpty())
    {
        ui->textEdit->setStyleSheet("QTextEdit{background:#" + background + "};");
    }

    delete pIni;

    this->ui->textEdit->setStyleSheet("padding: -1");
    this->setCentralWidget(ui->textEdit);           //去掉TextEdit的外边蓝色线
}

void MainWindow::onActionNewTriggered(bool checked)         //新建一个文件
{
    this->ui->textEdit->setStyleSheet("padding: -1");
    if(this->saved)
    {
        ui->textEdit->clear();
        this->saved = true;
    }
    else
    {
        int tag = QMessageBox::question(NULL,tr("记事本"),tr("是否保存文件?"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(tag == QMessageBox::Yes)
        {
            this->onActionSaveTriggered(false);
        }
        ui->textEdit->clear();
        this->saved = true;
    }
}

void MainWindow::onActionOpenTriggered(bool checked)        //打开一个文件
{
    QString displayString;
    bool center = false;

    if(saved == false)
    {
        int tag = QMessageBox::question(NULL,tr("记事本"),tr("是否保存文件?"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(tag == QMessageBox::Yes)
        {
            this->onActionSaveTriggered(false);
        }
        ui->textEdit->clear();
        this->saved = true;
        return;
    }

    if(checked != true)
    {
        file_path = QFileDialog::getOpenFileName(this,tr("选择路径"),".","*.txt");
        if(file_path.isEmpty())
            return;
    }

    QSettings *pIni = new QSettings(QCoreApplication::applicationDirPath()+"/config.ini",QSettings::IniFormat);
    QString background = pIni->value("/color/background").toString();
    this->ui->textEdit->setStyleSheet("QTextEdit{background:#" + background + ";}");
    delete pIni;

    QFile R_txt(file_path);
    bool tag = R_txt.open(QIODevice::ReadOnly | QIODevice::Text);
    if(!tag && checked != true)
    {
        QMessageBox::information(this,tr("提示"),tr("文件打开失败"));
        return;
    }

    if(checked != true)
        emit mySignal(file_path);

    QTextCodec* codec = QTextCodec::codecForName("GBK");    //字段 别名为中文

    while(!R_txt.atEnd())
    {
        QByteArray line = R_txt.readLine();
        QString linestr = codec->toUnicode(line);

        if(linestr.left(10)=="#<center>#")
        {
            center = true;
            linestr = linestr.right(linestr.length() - 10);
        }

        //QString linestr = codec->toUnicode(line).trimmed();   //读取文件的时候就去掉了换行
        if(linestr.isEmpty())
            continue;

        displayString.append(linestr);
    }
    ui->textEdit->clear();

    ui->textEdit->setPlainText(displayString);

    if(center)
        ui->textEdit->document()->setDefaultTextOption(QTextOption(Qt::AlignHCenter));

    this->saved = true;

    this->ui->textEdit->setStyleSheet("padding: -1");
    this->setCentralWidget(ui->textEdit);           //去掉TextEdit的外边蓝色线
}

void MainWindow::onActionSaveTriggered(bool checked)            //保存一个文件
{
    if(savePath.isEmpty())
    {
        QString sfile_path;
        while(sfile_path.isEmpty())
        {
            QString TitleName = this->windowTitle();
            int Title_lengrh = TitleName.length();
            int File_length  = Title_lengrh-10;
            QString FileName = TitleName.left(File_length);

            sfile_path = QFileDialog::getSaveFileName(this,tr("另存为"),tr("%1").arg(FileName),tr("(文本文档)*.txt"));
            if(sfile_path.isEmpty())
                return;
        }
        this->savePath = sfile_path;
    }

    QFile txt(this->savePath);
    bool tag = txt.open(QIODevice::WriteOnly);
    if(tag)
    {
        QTextStream out(&txt);
        out.setCodec(QTextCodec::codecForName("unicode"));
        QString str = ui->textEdit->toPlainText();
        if(ctrl3)
        {
            str = "#<center>#" + str;
        }
        out << str;
        txt.close();
        //txt.write(ui->textEdit->toPlainText().toStdString().data());  //另一种写入文件方式
    }
    else
    {
        QMessageBox::critical(NULL,tr("错误"),tr("无法打开文件,请检查对文件目录是否具有读写权限"),QMessageBox::Yes,QMessageBox::Yes);
    }
    this->saved = true;
}

void MainWindow::onActionSaveasTriggered(bool checked)          //另存为
{
    this->savePath.clear();
    this->onActionSaveTriggered(checked);
}

void MainWindow::onActionExitTriggered(bool checked)            //退出
{
    this->close();
}

void MainWindow::onActionUndoTriggered(bool checked)            //撤消
{
    ui->textEdit->undo();
}

void MainWindow::onActionPasteTriggered(bool checked)           //粘贴
{
    ui->textEdit->paste();
}

void MainWindow::onActionRedoTriggered(bool checked)            //重做
{
    ui->textEdit->redo();
}

void MainWindow::onActionRemoveSpaceTriggered(bool checked)     //删除空格
{
    QString s_str = ui->textEdit->toPlainText();
    s_str_list.append(s_str);
    if(ctrl == false)
    {
        s_str.replace("\n","");
        ctrl = true;
    }
    else if(ctrl == true)
    {
        s_str = s_str_list.at(0);
        ctrl = false;
    }

    if(s_str.isEmpty())
        return;

    ui->textEdit->setPlainText(s_str);
}

void MainWindow::onActionSpaceTriggered(bool checked)       //去掉空格
{
    QString s_str2 = ui->textEdit->toPlainText();
    s_str_list2.append(s_str2);
    if(ctrl2 == false)
    {
        s_str2.replace(" ","");
        ctrl2 = true;
    }
    else if(ctrl2 == true)
    {
        s_str2 = s_str_list2.at(0);
        ctrl2 = false;
    }

    if(s_str2.isEmpty())
        return;

    ui->textEdit->setPlainText(s_str2);
}

void MainWindow::onActionFontTriggered(bool checked)            //字体设置
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok,this);
    if(ok)
    {
        ui->textEdit->setFont(font);

        int pointSize = font.pointSize();
        QString family = font.family();
        bool bold = font.bold();
        bool italic = font.italic();

        WriteIni(pointSize,family,bold,italic);
    }
    else
        return;

    onActionOpenTriggered(true);

    ui->textEdit->setFont(font);
}

void MainWindow::onActionColorTriggered(bool checked)           //字体颜色设置
{
    ColorDlg dlg;           //打开新对话框
    dlg.exec();

    QColor cur_color1 = dlg.color1;     //文字颜色
    QColor cur_color2 = dlg.color2;     //高亮颜色
    QColor cur_color4 = dlg.color4;     //选中颜色

    QPalette palette = ui->textEdit->palette();
    if(cur_color1.isValid())
        palette.setColor(QPalette::Text,cur_color1);

    if(cur_color2.isValid())
        palette.setColor(QPalette::HighlightedText,cur_color2);

    if(cur_color4.isValid())
        palette.setColor(QPalette::Highlight,cur_color4);

    if(cur_color1.isValid() || cur_color2.isValid() || cur_color4.isValid())
        ui->textEdit->setPalette(palette);

//    if(cur_color3.isValid())
//    {
//        QRgb r_rgb = cur_color3.rgb();
//        sRgbStr_3 = QString::number(r_rgb,16);
//        QString mRgbStr = "QTextEdit{background:#" + sRgbStr_3 + "};";
//        ui->textEdit->setStyleSheet(mRgbStr);
//    }
//    WriteIni(sRgbStr_3);

//    QColor color = QColorDialog::getColor(Qt::red,this,tr("颜色对话框"));
//    QRgb r_rgb = color.rgb();
//    QString mRgbStr = "QTextEdit{background:#" + QString::number(r_rgb,16) + "};";
//    ui->textEdit->setStyleSheet(mRgbStr);

//    QPalette palette = ui->textEdit->palette();
//    palette.setColor(QPalette::Text,QColor(222,180,180));
//    palette.setColor(QPalette::HighlightedText,QColor(111,70,70));
//    palette.setColor(QPalette::Highlight,QColor(111,200,200));
//    ui->textEdit->setPalette(palette);

//    int start = ui->textEdit->textCursor().selectionStart();
//    int end   = ui->textEdit->textCursor().selectionEnd();
//    QTextCursor cursor = ui->textEdit->textCursor();
//    ui->textEdit->setTextBackgroundColor(color);
}

void MainWindow::onActionBackGroundTriggered(bool checked)          //背景颜色设置
{
    QString sRgbStr;
    QColor color = QColorDialog::getColor(Qt::red,this,tr("颜色对话框"));
    if(color.isValid())
    {
        QRgb r_rgb = color.rgb();
        sRgbStr = QString::number(r_rgb,16);

        ui->textEdit->setStyleSheet("QTextEdit{background:#" + sRgbStr + "};");

    }

    WriteIni(sRgbStr);

    this->ui->textEdit->setStyleSheet("padding: -1");
    this->setCentralWidget(ui->textEdit);           //去掉TextEdit的外边蓝色线
}

void MainWindow::onActionCenterTriggered(bool checked)              //字体居中
{
    if(ctrl3 == false)
    {
        ui->textEdit->document()->setDefaultTextOption(QTextOption(Qt::AlignHCenter));
        ctrl3 = true;
    }
    else if(ctrl3 == true)
    {
        ui->textEdit->document()->setDefaultTextOption(QTextOption(Qt::AlignLeft));
        ctrl3 = false;
    }
}

void MainWindow::onActionViewHelpTriggered(bool checked)            //帮助
{
    const QUrl url("https://blog.csdn.net/Superman___007");
    QDesktopServices::openUrl(url);
}

void MainWindow::switchLanguage()                                   //语言切换功能
{
    QTranslator* sTranslator = new QTranslator();
    m_bDisplayChinese = !m_bDisplayChinese;
    if (m_bDisplayChinese)
    {
        sTranslator->load(":/res/qm/language/langEnglish.qm");              //资源里需要有这个文件
    }
    else
    {
        sTranslator->load(":/res/qm/language/langChinese.qm");
    }
    qApp->installTranslator(sTranslator);
    ui->retranslateUi(this);
}

void MainWindow::onActionAboutNotepadTriggered(bool checked)        //随便写的
{
    QMessageBox::information(this,tr("提示"),tr("当前记事本信息\n版本:12.121.46"));
    return;
}

void MainWindow::mySlot(QString title2)     //为记事本添加标题
{
    QString title = title2 + tr(" - 记事本");
    this->setWindowTitle(title);
}

void MainWindow::onEdited()             //判断是否编辑了文件,若编辑了,退出时提示保存
{
    if(ui->textEdit->toPlainText().isEmpty())
        return;
    saved = false;
}

void MainWindow::do_cursorChanged()     //获得光标所在位置
{
    //获取textEdit的所有行数
    //int rowNum = ui->textEdit->document()->blockCount();
    //获取光标所在列的列号
    const QTextCursor cursor = ui->textEdit->textCursor();
    int colNum = cursor.columnNumber()+ 1;
    int rowNum = cursor.blockNumber() + 1;
    //在状态栏显示光标位置
    second_statusLabel->setText(tr(" 第 %1 行 第 %2 列 ").arg(rowNum).arg(colNum));
}

void MainWindow::closeEvent(QCloseEvent *e)         //关闭对话框事件
{
    if(saved == false)
    {
        int tag = QMessageBox::question(NULL,tr("记事本"),tr("是否保存文件?"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(tag == QMessageBox::Yes)
        {
            this->onActionSaveTriggered(false);
        }
        ui->textEdit->clear();
        this->saved = true;
    }
    this->close();
}

colordlg.h

#ifndef COLORDLG_H
#define COLORDLG_H

#include <QDialog>

namespace Ui {
class ColorDlg;
}

class ColorDlg : public QDialog
{
    Q_OBJECT

public:
    explicit ColorDlg(QWidget *parent = 0);
    ~ColorDlg();

    QColor color1,color2,color3,color4;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

private:
    Ui::ColorDlg *ui;
};

#endif // COLORDLG_H

colordlg.cpp

#include "colordlg.h"
#include "ui_colordlg.h"
#include <QColorDialog>

ColorDlg::ColorDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ColorDlg)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);

    this->setWindowIcon(QIcon(":/res/image/Paint.ico"));

    this->ui->pushButton->setStyleSheet("QPushButton{background-color: rgb(225, 225, 225);border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
                                        "QPushButton:hover{background-color:rgb(255,236,139); color: black;}"
                                        "QPushButton:pressed{background-color:rgb(255, 210, 139);border-style: inset;}");

//    this->ui->pushButton_2->setStyleSheet("QPushButton{background-color: rgb(225, 225, 225);border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
//                                          "QPushButton:hover{background-color:rgb(135,206,250); color: black;}"
//                                          "QPushButton:pressed{background-color:rgb(135,196,240);border-style: inset;}");

    this->ui->pushButton_3->setStyleSheet("QPushButton{background-color: rgb(225, 225, 225);border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
                                          "QPushButton:hover{background-color:rgb(255,182,193); color: black;}"
                                          "QPushButton:pressed{background-color:rgb(255,152,183);border-style: inset;}");

    this->ui->pushButton_4->setStyleSheet("QPushButton{background-color: rgb(225, 225, 225);border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
                                          "QPushButton:hover{background-color:rgb(144,238,144); color: black;}"
                                          "QPushButton:pressed{background-color:rgb(144,218,134);border-style: inset;}");

    QPalette palette = this->palette();
    palette.setColor(QPalette::Window,QColor(255,255,240));
    palette.setColor(QPalette::ButtonText,QColor(0,0,0));
    setPalette(palette);
}

ColorDlg::~ColorDlg()
{
    delete ui;
}

void ColorDlg::on_pushButton_clicked()
{
    QColor color = QColorDialog::getColor(Qt::red,this,QString::fromLocal8Bit("颜色对话框"));
    if(color.isValid())
    {
        color1 = color;
        QRgb r_rgb = color.rgb();
        QString s_Rgb = QString::number(r_rgb,16);

        QString sheet = QString("QPushButton{background-color: #%1;border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
                      "QPushButton:hover{background-color:rgb(255,236,139); color: black;}"
                      "QPushButton:pressed{background-color:rgb(255, 210, 139);border-style: inset;}").arg(s_Rgb);
        this->ui->pushButton->setStyleSheet(sheet);
    }
}

void ColorDlg::on_pushButton_3_clicked()
{
    QColor color = QColorDialog::getColor(Qt::red,this,QString::fromLocal8Bit("颜色对话框"));
    if(color.isValid())
    {
        color2 = color;
        QRgb r_rgb = color.rgb();
        QString s_Rgb = QString::number(r_rgb,16);

        QString sheet = QString("QPushButton{background-color: #%1;border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
                                "QPushButton:hover{background-color:rgb(255,182,193); color: black;}"
                                "QPushButton:pressed{background-color:rgb(255,152,183);border-style: inset;}").arg(s_Rgb);
        this->ui->pushButton_3->setStyleSheet(sheet);
    }
}

//void ColorDlg::on_pushButton_2_clicked()
//{
//    QColor color = QColorDialog::getColor(Qt::red,this,QString::fromLocal8Bit("颜色对话框"));
//    if(color.isValid())
//    {
//        color3 = color;
//        QRgb r_rgb = color.rgb();
//        QString s_Rgb = QString::number(r_rgb,16);

//        QString sheet = QString("QPushButton{background-color: #%1;border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
//                                "QPushButton:hover{background-color:rgb(135,206,250); color: black;}"
//                                "QPushButton:pressed{background-color:rgb(135,196,240);border-style: inset;}").arg(s_Rgb);
//        this->ui->pushButton_2->setStyleSheet(sheet);
//    }
//}

void ColorDlg::on_pushButton_4_clicked()
{
    QColor color = QColorDialog::getColor(Qt::red,this,QString::fromLocal8Bit("颜色对话框"));
    if(color.isValid())
    {
        color4 = color;
        QRgb r_rgb = color.rgb();
        QString s_Rgb = QString::number(r_rgb,16);

        QString sheet = QString("QPushButton{background-color: #%1;border:1px groove gray;border-radius:4px;padding:1px 4px;border-style: outset;}"
                                "QPushButton:hover{background-color:rgb(144,238,144); color: black;}"
                                "QPushButton:pressed{background-color:rgb(144,218,134);border-style: inset;}").arg(s_Rgb);
        this->ui->pushButton_4->setStyleSheet(sheet);
    }
}

void ColorDlg::on_pushButton_5_clicked()
{
    QDialog::reject();
}

void ColorDlg::on_pushButton_6_clicked()
{
    QColor color;
    color1 = color;
    color2 = color;
    color3 = color;
    color4 = color;

    QDialog::reject();
}

 

项目下载 : https://download.csdn.net/download/superman___007/11377262

 

天灰灰会不会,让我忘了你是谁 《世界末日》

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页