QT小项目-简单的记事本

这次用的配置好了QT环境的vs2022编写,与之前有些不同,比如QT creator里,ui界面是用指针编写,在vs则变为结构体,其次,由于vs是用的MSVC套件,一般作用与windows系统,不考虑跨平台,如需跨平台则用QT creator编写,项目编写与QT creator区别不是很大,注意细节,第一次使用VS编写随便写个简单的项目

.h

cpp 复制代码
#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_Notepad.h"
#include <QDebug>
#include <qfiledialog.h>
#include <qstandardpaths.h>
#include <qfile.h>
#include <qsavefile.h>
#include <qmessagebox.h>
#include <qtextstream.h>


class Notepad : public QMainWindow
{
    Q_OBJECT

public:
    Notepad(QWidget *parent = nullptr);
    ~Notepad();
public slots:
    void openfile();
    void savefile();

private:
    Ui::NotepadClass ui;
};

.cpp

cpp 复制代码
#include "Notepad.h"

Notepad::Notepad(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    connect(ui.open_action, &QAction::triggered, this, &Notepad::openfile);
    connect(ui.save_action, &QAction::triggered, this, &Notepad::savefile);
}

void Notepad::openfile()
{
    qDebug() << "open file";
    QString path = QFileDialog::getOpenFileName(this, tr("open file"), QStandardPaths::standardLocations
    (QStandardPaths::DocumentsLocation)[0]);
    if (path.isEmpty())
    {
        return;
    }

    QFile file(path);
    
    if (!file.open(QFile::ReadOnly))
    {
        qDebug() << "File opening failed";
        return;
    }
    QByteArray date = file.readAll();
    //QString str = QString::fromLocal8Bit(date);//适用于处理系统本地编码格式的字符串
    QString str = QString::fromUtf8(date);//用于明确知道数据是 UTF-8 编码的情况
    ui.plainTextEdit->clear();
    ui.plainTextEdit->setPlainText(str);
}

void Notepad::savefile()
{
    QString path = QFileDialog::getSaveFileName(this, "Saved location");
    if (path.isEmpty())
        return;
    QSaveFile file(path);
    if (!file.open(QFile::WriteOnly))
    {
        QMessageBox::critical(this, "ERROR", "File save failed");
        return;
    }
    QTextStream out(&file);//利用QT文本流,将内容输入到文本
    out << ui.plainTextEdit->toPlainText();
    if (!file.commit())//目的检测是否成功修改文本
    {
        QMessageBox::critical(this, "ERROR", "File save failed");
        return;
    }
    else
    {
        QMessageBox::information(this, "tips", "File saved successfully");
    }
}

Notepad::~Notepad()
{
}

.ui

cpp 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>NotepadClass</class>
 <widget class="QMainWindow" name="NotepadClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>600</width>
    <height>400</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Notepad</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QPlainTextEdit" name="plainTextEdit"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>600</width>
     <height>33</height>
    </rect>
   </property>
   <widget class="QMenu" name="fileMenu">
    <property name="title">
     <string>文件</string>
    </property>
    <addaction name="open_action"/>
    <addaction name="separator"/>
    <addaction name="save_action"/>
   </widget>
   <widget class="QMenu" name="editMenu">
    <property name="title">
     <string>编辑</string>
    </property>
   </widget>
   <widget class="QMenu" name="styleMenu">
    <property name="title">
     <string>格式</string>
    </property>
   </widget>
   <widget class="QMenu" name="viewMenu">
    <property name="title">
     <string>查看</string>
    </property>
   </widget>
   <widget class="QMenu" name="helpMenu">
    <property name="title">
     <string>帮助</string>
    </property>
   </widget>
   <addaction name="fileMenu"/>
   <addaction name="editMenu"/>
   <addaction name="styleMenu"/>
   <addaction name="viewMenu"/>
   <addaction name="helpMenu"/>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="open_action">
   <property name="text">
    <string>打开文件</string>
   </property>
  </action>
  <action name="save_action">
   <property name="text">
    <string>保存文件</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="Notepad.qrc"/>
 </resources>
 <connections/>
</ui>
相关推荐
二进制person1 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6661 小时前
C++讲解---创建日期类
开发语言·c++·算法
码农不惑2 小时前
2025.06.27-14.44 C语言开发:Onvif(二)
c语言·开发语言
Coding小公仔4 小时前
C++ bitset 模板类
开发语言·c++
菜鸟看点4 小时前
自定义Cereal XML输出容器节点
c++·qt
小赖同学啊4 小时前
物联网数据安全区块链服务
开发语言·python·区块链
shimly1234564 小时前
bash 脚本比较 100 个程序运行时间,精确到毫秒,脚本
开发语言·chrome·bash
漫步企鹅4 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
IT_10244 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle