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>
相关推荐
SNAKEpc121382 小时前
Qt开源控件库(qt-material-widgets)的编译及使用
c++·qt·开源
拓端研究室TRL3 小时前
R软件线性模型与lmer混合效应模型对生态学龙类智力测试数据层级结构应用
开发语言·r语言
于慨4 小时前
计算机考研C语言
c语言·开发语言·数据结构
GGGGGGGGGGGGGG.4 小时前
使用dockerfile创建镜像
java·开发语言
请为小H留灯4 小时前
Python中很常用的100个函数整理
开发语言·python
轩宇^_^4 小时前
C++ 类与对象的实际应用案例详解
开发语言·c++
oioihoii4 小时前
从零到多页复用:我的WPF MVVM国际化实践
开发语言·c#·wpf
Source.Liu5 小时前
【学写LibreCAD】 2.1 pdf_print_loop文件
qt·rust·pdf·cad·dxf
c7_ln5 小时前
编程视界:C++命名空间
开发语言·c++·笔记