我自写了三个头文件QC,writefiles,QFunction
QC.hpp
#include <QtWidgets/qwidget.h>
#include <QtCore/qtextcodec.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qstring.h>
#include <String>
#include <vector>
#include <iostream>
#include <QtWidgets/qmessagebox.h>
class QC {
public:
std::vector<QString> contents;
QC(const std::vector<std::string>& content) {
for (auto indexcontent : content) {
QTextCodec* Codec = QTextCodec::codecForName("GBK");
QString Qcontent = Codec->toUnicode(indexcontent.c_str());
contents.push_back(Qcontent);
}
}
};
inline void SHOWERROR(QString content) {
QMessageBox::warning(nullptr, "error", content);
}
inline ::QString QCT(const char* content) {
QTextCodec* Codec = QTextCodec::codecForName("GBK");
QString Qcontent = Codec->toUnicode(content);
return Qcontent;
}
inline QStringList QCTLT(std::vector<const char*>& vcc) {
QStringList Result;
for (const auto& content : vcc) {
Result.append(QCT(content));
}
return Result;
}
inline QString SingleQRB(QString& qc,char tar) {
int check = 0;
//check
for (auto& qcr : qc) {
if (qcr == tar) {
check += 1;
}
}
if (check == 1) {
QString Result(QCT(""));
QString middle(QCT(""));
bool if_(true);
for (auto& per : qc) {
if (per != tar and if_) {
Result += per;
}
else if (per == tar) { Result = per + Result; if_ = false; }
else {
middle += per;
}
}
Result = middle + Result;
return Result;}
else {
QChar qc(tar);
QString response1 = QCT("Repeating \"") + tar + QCT("\"") + QCT(" or no \"") + tar + QCT("\"");
QString response2 = QCT("SingleQRB accepts a QString just includes one \"") + tar + QCT("\"");
QMessageBox::information(nullptr,response1 , response2);
return 0;
}
}
writefiles.hpp
#include <fstream>
#include <windows.h>
#include <vector>
inline void WriteCover(const char* path, const char* content) {
std::ofstream ofile(path);
if (ofile.is_open()) {
ofile << content;
ofile.close();
}
else {
MessageBoxA(NULL, "文件打开失败", "打开文件", MB_ICONEXCLAMATION);
}
}
inline void WriteAdd(const char* path, const char* content) {
std::ofstream ofile(path, std::ios::app);
if (ofile.is_open()) {
ofile << content;
ofile.close();
}
else {
MessageBoxA(NULL, "文件打开失败", "打开文件", MB_ICONEXCLAMATION);
}
}
inline std::string ReadAll(const char* path) {
std::ifstream ifile(path);
if (ifile.is_open()) {
std::string content;
char perchar;
while (ifile.get(perchar)) {
content += perchar;
}
ifile.close();
return content;
}
else {
MessageBoxA(NULL, "文件打开失败", "打开文件", MB_ICONEXCLAMATION);
}
return "";
}
inline std::string ReadCustom(const char* path, int top, int end) {
std::ifstream ifile(path);
if (ifile.is_open()) {
std::vector<char> content;
std::string CharContent;
char perchar;
while (ifile.get(perchar)) {
content.push_back(perchar);
}
if (top <= end) {
if (end > content.size()) {
MessageBoxA(NULL, "读取错误,右索引值过大", "读取文件", MB_ICONEXCLAMATION);
}
else if (top < 0) {
MessageBoxA(NULL, "读取错误,左索引值过小", "读取文件", MB_ICONEXCLAMATION);
}
else {
for (; top <= end; top++) {
CharContent += content[top];
}
return CharContent;
}
}
else {
MessageBoxA(NULL, "读取错误,请检查索引值", "读取文件", MB_ICONEXCLAMATION);
}
}
else {
MessageBoxA(NULL, "文件读取失败", "读取文件", MB_ICONEXCLAMATION);
}
return "";
}
QFunction.hpp
#include <QtWidgets/qpushbutton.h>
#include <functional>
#include <utility>
namespace QButton {
template <typename Func, typename... Args>
inline void connect(QPushButton* button, Func f, Args&&... args) {
QObject::connect(button, &QPushButton::clicked, [f, args...]() {
f(args...);
});
}
}
namespace QCBox {
template <typename Func, typename... Args>
inline void ClickedConnect(QComboBox* combobox, Func f, Args... args) {
QObject::connect(combobox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index) {f(args...);
});
}
}
我的源文件
#include <QtWidgets/qwidget.h>
#include <windows.h>
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qlabel.h>
#include <QtWidgets/qframe.h>
#include <QtWidgets/qlineedit.h>
#include <QtCore/qtextcodec.h>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/qcombobox.h>
#include <QtGui/qfont.h>
#include <QtCore/qstringlist>
#include <QtCore/qstring.h>
#include <QtGui/qpixmap.h>
#include <QtGui/qicon.h>
#include <QtWidgets/qtextedit.h>
#include <QtWidgets/qmessagebox.h>
#include "QFunction.hpp"
#include "writefiles.hpp"
#include <filesystem>
#include "QC.hpp"
#define Button QPushButton
#define Label QLabel
#define QHBox QHBoxLayout
#define QVBox QVBoxLayout
void change(QComboBox* combo, QTextEdit* text) {
if (combo->currentText() == QCT("追加")) {
text->setPlaceholderText(QCT("输入要追加的内容"));
}
else {
text->setPlaceholderText(QCT("输入新的内容"));
}
}
void writeinto(QComboBox* combo, QLineEdit* line, QTextEdit* text) {
if (combo->currentText() == QCT("追加")) {
std::string filePath = line->text().toStdString();
std::string content = text->toPlainText().toStdString();
if (std::filesystem::exists(filePath)) {
WriteAdd(filePath.c_str(), content.c_str());
QMessageBox::information(nullptr, QCT("追加"), QCT("追加内容成功"));
}
else {
QMessageBox::information(nullptr, QCT("路径错误"), QCT("请输入正确的文件路径"));
}
}
else {
std::string filePath = line->text().toStdString();
std::string content = text->toPlainText().toStdString();
if (std::filesystem::exists(filePath)) {
WriteCover(filePath.c_str(), content.c_str());
QMessageBox::information(nullptr, QCT("覆盖"), QCT("内容覆盖成功"));
}
else {
QMessageBox::information(nullptr, QCT("路径错误"), QCT("请输入正确的文件路径"));
}
}
}
// WinMain 入口函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
int argc = 0;
char** argv = nullptr;
QApplication app(argc, argv);//应用程序类
QWidget mainwindow; //主窗口类
mainwindow.setWindowTitle(QCT("文本内容追加"));
mainwindow.resize(600, 500);
QFont font("Afrail", 15);
//组件
QTextEdit* text = new QTextEdit(&mainwindow);
QComboBox* combo = new QComboBox(&mainwindow);
QLineEdit* line = new QLineEdit(&mainwindow);
Button* button = new Button(QCT("Write Into"), &mainwindow);
QVBox* vbox = new QVBox(&mainwindow);
text->setFont(font);
line->setFont(font);
combo->setFont(font);
combo->addItems({ QCT("追加"), QCT("覆盖") });
line->setPlaceholderText(QCT("文件路径"));
text->setPlaceholderText(QCT("输入要追加的内容"));
vbox->addWidget(combo);
vbox->addWidget(line);
vbox->addWidget(text);
vbox->addWidget(button);
QButton::connect(button, writeinto, combo, line, text);
QCBox::ClickedConnect(combo, change, combo, text);
//显示窗口
mainwindow.show();
return app.exec();
}
运行成功,但当我点击writeinto按钮时
出现内存访问的问题
我询问过许多AI包括Deepseek,但错误依旧在! 我打算使用QFile重写,但对于这个问题我依旧想得到解决