QT制作一个简单的记事本

一、布局界面,如图所示的界面及对象名称:

blob.png


二、引入头文件

#include <QMessageBox>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QDir>
#include <QTextStream>
#include <QFontDialog>
#include <QFont>
#include <QColorDialog>
#include <QColor>
#include <QDate>
#include <QUrl>
#include <QDesktopServices>


三、声明槽函数

private slots:
    void calcSlot();
    //新建文件
    void createNewFile();
    //打开文件
    void openFileSlot();
    //保存文件
    void saveFileSlot();
    //设置字体
    void setFontSlot();
    //设置颜色
    void setColorSlot();
    //设置当前时间
    void setCurrentDateSlot();
    //关于我们,打开一个网站,使用的是默认浏览器
    void aboutWebsiteSlot();
    //弹出子对话框
    void aboutSoftwareSlot();

三、实现槽函数:

//计算的槽
void MainWindow::calcSlot(){
    double firstNum = ui->firstNum->text().toDouble();
    double secondNum = ui->secondNum->text().toDouble();
    QString resultNum  = "0";
    QString ComboBoxVal = ui->countComboBox->currentText();
    if(ComboBoxVal == "+"){
        resultNum = QString::number(firstNum + secondNum);
    }else if(ComboBoxVal == "-"){
        resultNum = QString::number(firstNum - secondNum);
    }else if(ComboBoxVal == "*"){
        resultNum = QString::number(firstNum * secondNum);
    }else{
        resultNum = QString::number(firstNum / secondNum);
    }
    QMessageBox::information(this,"结果",resultNum);
    ui->addNum->setText(resultNum);
}

//新建文件
void MainWindow::createNewFile(){
    //如果当前文档的内容已经改变了
    if(ui->textAreaInput->document()->isModified()){
        qDebug()<<"is aready";
        return;
    }else{
        //qDebug()<<"is null for ecit";
        ui->textAreaInput->clear();
        this->setWindowTitle("新文档.txt");
    }
}

//打开文件
void MainWindow::openFileSlot(){
    QString fileName = QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
    //qDebug()<<"文件名是:"<<fileName;
    if(fileName.isEmpty()){
        QMessageBox::information(this,"错误消息","请选择一个文件");
        return;
    }
    QFile *file = new QFile;
    file->setFileName(fileName);
    bool openFileOk = file->open(QIODevice::ReadOnly);
    if(openFileOk){
        //文件与文本流相关联
        QTextStream in(file);
        ui->textAreaInput->setText(in.readAll());
        file->close();
        delete file;
    }else{
        QMessageBox::information(this,"错误提示","打开文件失败:"+file->errorString());
        return;
    }
}

//保存文件
void MainWindow::saveFileSlot(){
    QString fileName = QFileDialog::getSaveFileName(this,"Save File",QDir::currentPath());
    if(fileName.isEmpty()){
        QMessageBox::information(this,"错误提示","请选择一个文件");
        return ;
    }
    QFile *file = new QFile;
    file->setFileName(fileName);
    bool saveFileOk = file->open(QIODevice::WriteOnly);
    if(saveFileOk){
        QTextStream out(file);
        out<<ui->textAreaInput->toPlainText(); //这里是取出textEdit中的纯文本
        file->close();
        delete file;
        QMessageBox::information(this,"提示","保存成功");
    }else{
        QMessageBox::information(this,"错误提示","保存文件失败");
        return;
    }
}

//设置字体
void MainWindow::setFontSlot(){
    bool ok ;
    QFont font = QFontDialog::getFont(&ok,this);
    if(ok){
        ui->textAreaInput->setFont(font);
    }else{
        QMessageBox::information(this,"错误提示","设置字体错误");
        return;
    }
}

//设置颜色
void MainWindow::setColorSlot(){
    QColor color = QColorDialog::getColor(Qt::red,this);
    if(color.isValid()){
        ui->textAreaInput->setTextColor(color);
    }else{
        QMessageBox::information(this,"错误提示","设置字体颜色错误");
        return;
    }
}

//设置当前时间
void MainWindow::setCurrentDateSlot(){
    QDateTime current = QDateTime::currentDateTime();
    QString time = current.toString("yyyy-MM-dd hh:mm:ss");
    ui->textAreaInput->append(time);
}

//关于我们
void MainWindow::aboutWebsiteSlot(){
    QDesktopServices::openUrl(QUrl("http://www.sindsun.com/"));
}

//关于软件
void MainWindow::aboutSoftwareSlot(){
    aboutme *dlg =  new aboutme;
    //显示非模态对话框
    dlg->show();
    //显示模态圣诞框
    //dlg->exec();
}

四、添加信号与槽的连接:

    this->setWindowTitle("记事本");
    QObject::connect(ui->equalButton,SIGNAL(clicked()),this,SLOT(calcSlot()));
    QObject::connect(ui->createNewAction,SIGNAL(triggered()),this,SLOT(createNewFile()));
    QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));
    QObject::connect(ui->saveFileAction,SIGNAL(triggered()),this,SLOT(saveFileSlot()));
    QObject::connect(ui->exitAction,SIGNAL(triggered()),this,SLOT(close())); //关闭对话框

    QObject::connect(ui->undoAction,SIGNAL(triggered()),ui->textAreaInput,SLOT(undo()));
    QObject::connect(ui->redoAction,SIGNAL(triggered()),ui->textAreaInput,SLOT(redo()));
    QObject::connect(ui->copyAction,SIGNAL(triggered()),ui->textAreaInput,SLOT(copy()));
    QObject::connect(ui->pasteAction,SIGNAL(triggered()),ui->textAreaInput,SLOT(paste()));
    QObject::connect(ui->selectAllAction,SIGNAL(triggered()),ui->textAreaInput,SLOT(selectAll()));
    QObject::connect(ui->shearAction,SIGNAL(triggered()),ui->textAreaInput,SLOT(cut()));

    QObject::connect(ui->colorAction,SIGNAL(triggered()),this,SLOT(setColorSlot()));
    QObject::connect(ui->fontAction,SIGNAL(triggered()),this,SLOT(setFontSlot()));
    QObject::connect(ui->dateAction,SIGNAL(triggered()),this,SLOT(setCurrentDateSlot()));

    QObject::connect(ui->aboutAction,SIGNAL(triggered()),qApp,SLOT(aboutQt()));
    QObject::connect(ui->websiteAction,SIGNAL(triggered()),this,SLOT(aboutWebsiteSlot()));
    QObject::connect(ui->aboutSoftwareAction,SIGNAL(triggered()),this,SLOT(aboutSoftwareSlot()));

五、下面是实现添加图标的过程

    1、资源 -> 添加新文件 -> Qt -> Qt Resource File -> 选择、起名称 -> 完成

    2、提前准备一份徐囡楠文件,拷贝到项目根目录下

    3、右键资源文件,导入现有的资源

    4、在ui界面编辑动作,添加图标

    blob.png

    

六、关于我们的对话框,加载一个动态图片

    1、引入QMovie类

#include <QMovie>

    2、弹出的新对话框布局

    blob.png

    3、声明槽函数

private:
    QMovie *movie;
private slots:
    //开始播放
    void startMoviesSlot();
    //停止播放
    void stopMoviesSlot();

    4、实现槽函数

void aboutme::startMoviesSlot(){
    this->movie->start();
}

void aboutme::stopMoviesSlot(){
    this->movie->stop();
}

    5、在构造函数中添加代码:

    this->movie = new QMovie("flashPic.gif");
    ui->flashLabel->setMovie(this->movie);
    this->movie->start();

    QObject::connect(ui->yesBtn,SIGNAL(clicked()),this,SLOT(startMoviesSlot()));
    QObject::connect(ui->cancelBtn,SIGNAL(clicked()),this,SLOT(stopMoviesSlot()));


七、为程序添加启动界面,添加在main.cpp中


#include <QSplashScreen>

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

    QPixmap pixmap("flashPic.gif");
    QSplashScreen splash(pixmap);
    splash.show();
    for(long index=0; index<=100000000; index++);
    MainWindow w;
    w.show();
    splash.finish(&w);
    return a.exec();
}


版权声明: 此文为本站源创文章[或由本站编辑从网络整理改编],
转载请备注出处:
[狂码一生] https://www.sindsun.com/articles/16/66
[若此文确切存在侵权,请联系本站管理员进行删除!]


--THE END--