【Qt学习】一个简易的记事本

1.项目环境:       Qt 5.14.2  编译器MinGW 64-bit                windows 11

2.运行效果图:

3.这个记事本可以实现新建编辑文本,并将文本保存,也可以打开原有文件(这里只能打开.cpp文件)修改之后另存为。

项目.pro文件

QT       += core gui
 
 
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
 
CONFIG += c++11
 
 
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
 
 
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
 
SOURCES += \
    main.cpp \
    mainwindow.cpp
 
 
HEADERS += \
    mainwindow.h
 
 
FORMS += \
    mainwindow.ui
 
 
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
 
#include <QMainWindow>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QKeyEvent>
#include <QMouseEvent>
 
 
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
 
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
 
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void keyPressEvent(QKeyEvent *k);
    void mousePressEvent(QMouseEvent *m);
 
 
private slots:
    void action_NSlot();
    void action_OSlot();
    void action_SSlot();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
 
    setWindowTitle("记事本");
    connect(ui->action_N, &QAction::triggered,this,&MainWindow::action_NSlot);
    connect(ui->action_O, &QAction::triggered,this,&MainWindow::action_OSlot);
    connect(ui->action_S, &QAction::triggered,this,&MainWindow::action_SSlot);
}
 
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
 
void MainWindow::action_NSlot()
{
    ui->textEdit->clear();
    this->setWindowTitle("新建文本文档.txt");
}
 
 
void MainWindow::action_OSlot()
{
   QString filename = QFileDialog::getOpenFileName(this, "选择一个文件",
                                 QCoreApplication::applicationFilePath(),"*.cpp");
   if (filename.isEmpty())
   {
       QMessageBox::warning(this, "警告", "请选择一个文件");
   }
   else {
       qDebug() << filename;
       QFile file(filename);
       file.open(QIODevice::ReadOnly);
       QByteArray ba = file.readAll();
       ui->textEdit->setText(QString(ba));
       file.close();
   }
}
 
 
void MainWindow::action_SSlot()
{
   QString filename = QFileDialog::getSaveFileName(this, "选择一个文件",
                                 QCoreApplication::applicationFilePath());
   if (filename.isEmpty())
   {
       QMessageBox::warning(this, "警告", "请选择一个文件");
   }
   else {
       QFile file(filename);
       file.open(QIODevice::WriteOnly);
       QByteArray ba;
       ba.append(ui->textEdit-> toPlainText());
       file.write(ba);
       file.close();
   }
}
 
 
void MainWindow::keyPressEvent(QKeyEvent *k)
{
    if(k->modifiers() == Qt::ControlModifier && k->key() == Qt::Key_S)
    {
        action_SSlot();
    }
}
 
 
void MainWindow::mousePressEvent(QMouseEvent *m)
{
    QPoint pt = m->pos();
    qDebug() << pt;
    if(m->button() == Qt::LeftButton)
    {
        qDebug() << "左键被按下";
    }
    if(m->button() == Qt::RightButton)
    {
        qDebug() << "右键被按下";
    }
}
  • 23
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值