前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Qt学习(二)------实例mynotepad制作

Qt学习(二)------实例mynotepad制作

作者头像
致Great
发布2023-08-26 19:05:05
发布2023-08-26 19:05:05
19900
代码可运行
举报
文章被收录于专栏:自然语言处理自然语言处理
运行总次数:0
代码可运行

一、Qt学习

最近在看高老师的视频教程,自己跟着做了一个txt编辑器,功能很简单,能实现编辑,保存,撤销,复制粘贴等基础功能;关于那边有一些特效

二、关键代码

1、mainwindow.cpp

代码语言:javascript
代码运行次数:0
运行
复制
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("Untitled.txt-------------------yanqiang");
    this->setWindowIcon(QIcon(":new/icon/txt"));
    //File Menu
    QObject::connect(ui->newAction,SIGNAL(triggered()),this ,SLOT(newFileSlot()));
    QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));
    QObject::connect(ui->saveAction,SIGNAL(triggered()),this,SLOT(saveFileSlot()));
    QObject::connect(ui->exitAction,SIGNAL(triggered()),this,SLOT(close()));
    //Edit Menu
    QObject::connect(ui->undoAction,SIGNAL(triggered()),ui->textEdit,SLOT(undo()));
    QObject::connect(ui->redoAction,SIGNAL(triggered()),ui->textEdit,SLOT(redo()));
    QObject::connect(ui->copyAction,SIGNAL(triggered()),ui->textEdit,SLOT(copy()));
    QObject::connect(ui->cutAction,SIGNAL(triggered()),ui->textEdit,SLOT(cut()));
    QObject::connect(ui->pasteAction,SIGNAL(triggered()),ui->textEdit,SLOT(paste()));
    QObject::connect(ui->selectAllAction,SIGNAL(triggered()),ui->textEdit,SLOT(selectAll()));


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


    //Help Menu
    QObject::connect(ui->aboutQtAction,SIGNAL(triggered()),qApp,SLOT(aboutQt()));
    QObject::connect(ui->aboutWebAction,SIGNAL(triggered()),this,SLOT(aboutWebSiteSlot()));
    QObject::connect(ui->aboutSoftWareAction,SIGNAL(triggered()),this,SLOT(aboutSoftWareSlot()));


}


void MainWindow::closeEvent(QCloseEvent *event){
    //忽略事件
    //event->ignore();
    //event->accept();
    if(ui->textEdit->document()->isModified()){
        QMessageBox msgBox;
        //显示的提示信息
        msgBox.setText("文本已经变更");
        msgBox.setInformativeText("您是否要保存文件");
        msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Save);
        int ret = msgBox.exec();

        switch (ret) {
          case QMessageBox::Save:
              // Save was clicked
              this->saveFileSlot();
              break;
          case QMessageBox::Discard:
              // Don't Save was clicked
              event->accept();
              break;
          case QMessageBox::Cancel:
              // Cancel was clicked
              event->ignore();
              break;
          default:
              // should never be reached
              break;
        }
    }
    else{
        event->accept();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::newFileSlot(){
    //Content is modified
    if(ui->textEdit->document()->isModified())
    {
        qDebug()<<"File is modified";
    }
    else{
        //qDebug()<<"File is not modified";
        ui->textEdit->clear();
        this->setWindowTitle("Untitled.txt------------yanqinag");
    }
}

void MainWindow::openFileSlot(){
    //Get File Name
    QString fileName=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
    //qDebug()<<"fileName is:"<<fileName;
    if(fileName.isEmpty()){
        QMessageBox::information(this,"Error Message","Select A File");
        return;
    }
    QFile *file=new QFile;
    file->setFileName(fileName);
    bool ok=file->open(QIODevice::ReadOnly);
    if(ok){
        //File With QTextStream
        QTextStream in(file);
        ui->textEdit->setText(in.readAll());//get content from file
        file->close();
        delete file;
    }
    else {
         QMessageBox::information(this,"Error Message","Fail To Open File"+file->errorString());
         return;
    }
}

//save file
void MainWindow::saveFileSlot(){
    if(saveFileName.isEmpty()){
        this->saveAsFileSlot();
    }
    else{
        QFile *file=new QFile;
        file->setFileName(saveFileName);
        bool ok=file->open(QIODevice::WriteOnly);
        if(ok){
            QTextStream out(file);
            out<<ui->textEdit->toPlainText();//这里去除textEdit的纯文本
            file->close();
            delete file;
        }
    }
}
//save as
void MainWindow::saveAsFileSlot(){
    //使用getSaveFileName方法
    QString saveFileName=QFileDialog::getSaveFileName(this,"Save File",QDir::currentPath());
    if(saveFileName.isEmpty()){
        QMessageBox::information(this,"Error Message","Select A File");
        return;
    }
    QFile *file=new QFile;
    file->setFileName(saveFileName);
    bool ok=file->open(QIODevice::WriteOnly);
    if(ok){
        QTextStream out(file);
        out<<ui->textEdit->toPlainText();//这里去除textEdit的纯文本
        file->close();
        this->setWindowTitle(saveFileName+"-----------------notepad");
        delete file;
    }
    else{
        QMessageBox::information(this,"Error Message","Save File Error");
        return;
    }
}

void MainWindow::setFontSlot(){
    //get selected font
    /*
    bool ok;
    QFont font = QFontDialog::getFont(
                    &ok, QFont("Helvetica [Cronyx]", 10), this);
    if (ok) {
        // the user clicked OK and font is set to the font the user selected
    } else {
        // the user canceled the dialog; font is set to the initial
        // value, in this case Helvetica [Cronyx], 10
    }
     */
    bool ok;
    QFont font=QFontDialog::getFont(&ok,this);
    if(ok){
        ui->textEdit->setFont(font);
    }
    else{
        QMessageBox::information(this,"Error Message","Error Set Font");
        return;
    }
}

void MainWindow::setColorSlot(){
    /*
    const QColorDialog::ColorDialogOptions options = QFlag(colorDialogOptionsWidget->value());
    const QColor color = QColorDialog::getColor(Qt::green, this, "Select Color", options);

    if (color.isValid()) {
        colorLabel->setText(color.name());
        colorLabel->setPalette(QPalette(color));
        colorLabel->setAutoFillBackground(true);
    }
    */
    QColor color=QColorDialog::getColor(Qt::red,this);
    if(color.isValid()){
        ui->textEdit->setTextColor(color);
    }
    else{
        QMessageBox::information(this,"Error Message","Color is invalid");
        return;
    }
}

void MainWindow::currentDateTimeSlot(){
    QDateTime current=QDateTime::currentDateTime();
    QString time=current.toString("yyyy-M-dd hh:mm:ss");
    ui->textEdit->append(time);
}

void MainWindow::aboutWebSiteSlot(){
    QDesktopServices::openUrl(QUrl("http://wwww.baidu.com"));

}

void MainWindow::aboutSoftWareSlot(){

    //创建about对象
    about *dialog=new about;
    dialog->show();//非模态对话框
    //dialog.exec()  modal dailog
}

2、main.cpp

代码语言:javascript
代码运行次数:0
运行
复制
#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QSplashScreen>

//延时
void sleep(unsigned int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

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



    //屏幕启动画面
    QPixmap pixmap("screen.png");
    QSplashScreen splash(pixmap);
    splash.show();

    sleep(5000);
   // for(long index=0;index<=4590500000;index++);
    MainWindow w;
    w.show();


    splash.finish(&w);
    return app.exec();



}

3、about.cpp

代码语言:javascript
代码运行次数:0
运行
复制
#include "about.h"
#include "ui_about.h"

about::about(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::about)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":new/icon/txt"));
    /*
    QLabel label;
    QMovie *movie = new QMovie("animations/fire.gif");

    label.setMovie(movie);
    movie->start();
    */
    this->movie=new QMovie("mynotepad.gif");
    ui->movieLabel->setMovie(movie);
    movie->start();

    QObject::connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(stopMovieSlot()));
    QObject::connect(ui->startButton,SIGNAL(clicked()),this,SLOT(startMovieSlot()));
}

about::~about()
{
    delete ui;
}
void about::startMovieSlot(){
     this->movie->start();//开始动画播放
}
void about::stopMovieSlot(){
    this->movie->stop();//停止动画播放
}

三、运行效果图

四、下面是源码下载链接

http://download.csdn.net/detail/yanqianglifei/9558722

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Qt学习
  • 二、关键代码
  • 三、运行效果图
  • 四、下面是源码下载链接
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档