【C/C++学院】(19)QT版:记事本

利用主窗口类实现,QT版:记事本。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCloseEvent>
#include <QTextEdit>
#include <QMenu>
#include <QMenuBar>
#include <QAction>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void openfile();
    void newfile();
    void savefile();
    void exitfile();
    void copyfile();
    void cutfile();
    void pastefile();

private:
    void createMenus();
    void createActions();
    void closeEvent(QCloseEvent *event);
    QTextEdit *text;
    QMenu *fileMenu;
    QMenu *editMenu;
    QAction *openFileAction;
    QAction *newFileAction;
    QAction *saveFileAction;
    QAction *exitFileAction;
    QAction *copyFileAction;
    QAction *cutFileAction;
    QAction *pasteFileAction;

};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QTextStream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("记事本");
    text = new QTextEdit(this);
    this->setCentralWidget(text);
    text->setFontPointSize(20);//设置默认字体大小
    createActions();
    createMenus();

}

MainWindow::~MainWindow()
{

}

void MainWindow::openfile()
{
    QString filename = QFileDialog::getOpenFileName(this, tr("打开文件"));
    if (filename.isEmpty())//如果没有选择要打开的文件名,函数退出
    {
        return ;
    }
    QFile data(filename);
    if (data.open(QFile::ReadOnly))//以只读方式打开文件
    {
        text->clear();//首先将text空间内容清空
        QTextStream stream(&data);
        while (!stream.atEnd())//循环读取文件按,直到文件尾结束循环
        {
            text->append(stream.readLine());//在text控件内容尾部追加从文件中读取的内容
        }
        data.close();//读完文件后关闭文件
    }else
    {
        QMessageBox::critical(this, tr("错误"), data.errorString());
    }
}

void MainWindow::newfile()
{
    text->clear();
}

void MainWindow::savefile()
{
    QString filename = QFileDialog::getSaveFileName(this, tr("保存文件"));
    if (filename.isEmpty())//如果没有选择文件,函数退出
    {
        return ;
    }
    QFile data(filename);
    if (data.open(QFile::WriteOnly | QFile::Truncate))//以写方式打开文件,如果关键存在,则是追加模式
    {
        QTextStream stream(&data);
        stream<<text->toPlainText();//将text控件内容转化为QString后,写入文件
        data.close();
    }
    else
    {
        QMessageBox::critical(this, tr("错误"), data.errorString());
    }
}

void MainWindow::exitfile()
{
    close();
    //exit(0);//直接退出
}

void MainWindow::copyfile()
{
    text->copy();
}

void MainWindow::cutfile()
{
    text->cut();
}

void MainWindow::pastefile()
{
    text->paste();
}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("文件"));//建立文件菜单
    fileMenu->addAction(openFileAction);
    fileMenu->addAction(newFileAction);
    fileMenu->addAction(saveFileAction);
    fileMenu->addSeparator();//加入一个分隔符
    fileMenu->addAction(exitFileAction);

    editMenu = menuBar()->addMenu(tr("编辑"));
    editMenu->addAction(copyFileAction);
    editMenu->addAction(cutFileAction);
    editMenu->addAction(pasteFileAction);
}

void MainWindow::createActions()
{
    openFileAction = new QAction(tr("打开"), this);
    openFileAction->setShortcut(tr("Ctrl+o"));
    connect(openFileAction, SIGNAL(triggered()), this, SLOT(openfile()));

    newFileAction = new QAction(tr("新建"), this);
    newFileAction->setShortcut(tr("Ctrl+N"));
    connect(newFileAction, SIGNAL(triggered()), this, SLOT(newfile()));

    saveFileAction = new QAction(tr("保存"), this);
    saveFileAction->setShortcut(tr("Ctrl+s"));
    connect(saveFileAction, SIGNAL(triggered()), this, SLOT(savefile()));

    exitFileAction = new QAction(tr("退出"), this);
    exitFileAction->setShortcut(tr("Ctrl+q"));
    connect(exitFileAction, SIGNAL(triggered()), this, SLOT(exitfile()));

    copyFileAction = new QAction(tr("复制"), this);
    copyFileAction->setShortcut(tr("Ctrl+c"));
    connect(copyFileAction, SIGNAL(triggered()), this, SLOT(copyfile()));

    cutFileAction = new QAction(tr("剪切"), this);
    cutFileAction->setShortcut(tr("Ctrl+x"));
    connect(cutFileAction, SIGNAL(triggered()), this, SLOT(cutfile()));

    pasteFileAction = new QAction(tr("粘贴"), this);
    pasteFileAction->setShortcut(tr("Ctrl+v"));
    connect(pasteFileAction, SIGNAL(triggered()), this, SLOT(pastefile()));
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton button;
    button = QMessageBox::question(this, tr("退出程序"), QString(tr("是否退出")),
                                   QMessageBox::Yes | QMessageBox::No);
    if (button == QMessageBox::Yes)
    {
        event->accept();//接受退出信号,程序退出
    }else
    {
        event->ignore();//忽略退出信息,程序继续运行
    }
}
#include "mainwindow.h"
#include <QApplication>

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

    w.resize(600, 400);
    w.show();

    return a.exec();
}

时间: 2024-08-02 02:48:33

【C/C++学院】(19)QT版:记事本的相关文章

Qt版音乐播放器

    Qt版音乐播放器 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 一.关于Qt 1.1 什么是Qt Qt是一个跨平台应用程序和UI开发框架.使用Qt只需一次性开发应用程序,无需重新编写源代码,便可跨不同桌面和嵌入式操作系统部署这些应用程序. Qt Creator是全新的跨平台Qt IDE,可单独使用,也可与Qt库和开发工具组成一套完整的SDK,其中包括:高级C++代码编辑器,项目和集成管理工具,集成的上下文相关的帮助系统,图形化调试器,代码管理

Qt版贪吃蛇游戏

Qt版贪吃蛇游戏 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 最近在学习Qt,用了一个多月的时间掌握了Qt中最基本的知识,也完成了<Qt版音乐播放器>.<Qt版贪吃蛇游戏>.<Qt版双人俄罗斯方块>以及<Qt版科学计算器>等,之前在VC下写过这些程序,所以在Qt下只是改变了显示等语句,我写过<C++版贪吃蛇游戏>.<VC版贪吃蛇游戏>,当时将与显示等无关的东西封装起来,在Qt下直接用,只

Qt版科学计算器

Qt版科学计算器 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 之前做过<VC版科学计算器>,这也是我学VC++时的第一个大作业,通过科学计算器的开发使用我学到了很多东西,也让我逐渐喜欢上了编程.最近在学习Qt,所以将当时在VC下写过的一些东西在Qt下重写了一遍,其实主要还是与显示等有关的东西需要重写,要使用Qt的显示方式,而其他的核心的算法等都还是使用VC下C++的源码. 下面是Qt版的运行截图: 标准版: 科学版: 头文件中变量和槽的声明: c

Qt版双人俄罗斯方块游戏

Qt版双人俄罗斯方块游戏 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 之前写过<VC版双人俄罗斯方块>,将其在Qt下又重写了一遍,核心的算法还是采用VC下曾经用过的C++源码,直接用的是当时封装好的类,写在一个文件中,所以直接拿来用.不同的仍然是与显示有关的东西需要采用Qt下的显示方式. 以下是Qt版运行截图: 先是按键响应槽的实现代码: void Tetris::keyPressEvent(QKeyEvent *event) { if(even

单机版wiki:网页版记事本和网页版时间管理GTD软件

TiddlyWiki:可以用来做个人记事本,只需要下载一个empty.html便可以使用,搬家到不同电脑也很方便.如果能解决安全性问题,便可以搬到网络上在线使用.和Blog的差别在于这里是不想公开的只给自己看的记事本. 时间管理GTD: MonkeyGTD,也是只要下载一个mgsd.html文件即可使用.对工作事项有很多种分类方法,如场合/时间/处理途径/...,也可以添加自己需要的分类方法.每天进来更新一下每个事项的状态,就可以很有信心的做好自己时间管理,不用担心什么事情又忘记处理了. 这两个

Mysql5.7.19压缩版安装步骤及踩过的坑

安装Mysql5.7.19压缩版 一:下载压缩包 1.从MySQL官网下载MySQL Community Server 5.7.19,此版本为免费版. 2.下载完成之后解压缩,打开之后文件夹如下: 二:配置mysql 打开文件夹D:\programs\MySQL\mysql-5.7.19-winx64,新建文件夹data,新建配置文件my.ini,文件夹目录如下: 配置文件my.ini添加内容如下: [client] port=3306 default-character-set=utf8 [m

【C/C++学院】(16)QT版:幸运大抽奖

程序效果: #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QTimer> #include <QStringList> class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialo

课程设计(部分代码)之java版(记事本)

1 /* 2 *java课程设计之记事本(coder @Gxjun) 3 * 编写一个记事本程序 4 * 要求: 5 * 用图形用户界面实现. 6 * 能实现编辑.保存.另存为.查找替换等功能. 7 * 提示:使用文件输入输出流. 8 */ 9 package project; 10 11 import javax.swing.*; 12 13 import java.awt.event.KeyEvent; 14 import java.awt.event.InputEvent; 15 impo

Qt版helloworld

跟学别的编程语言一样,Qt也不例外,一开始就想写一个helloworld.初学Qt十几天,看了一点关于Qt视频的介绍和书上的基础知识,对于Qt写工程的概念有了初步的认识,就代码的形式来说,Qt Creator可以让用户选择新建一个Gui项目和空的Qt项目.Gui项目从代码的形式上来看就跟C/C++多文件编译时差不多,分为一个类声明的.h文件.类函数实现的.cpp文件和主函数的.cpp文件,而且编译器根据我们创建项目时的选择和对于类名的命名给出了一些基本的代码框架.空的QT项目打开后就只有一个工程