qt-C++笔记之按行读取文件并切换复选框打印复选框拼接出的字符串
code review!
文章目录
- qt-C++笔记之按行读取文件并切换复选框打印复选框拼接出的字符串
- 1.运行
- 2.文件结构
- 3.main.cc
- 4.main.pro
- 5.a.txt
- 6.b.txt
1.运行
2.文件结构
3.main.cc
代码
#include <QApplication>
#include <QCheckBox>
#include <QDebug>
#include <QFile>
#include <QPushButton>
#include <QTextStream>
#include <QVBoxLayout>
#include <QWidget>int main(int argc, char *argv[]) {QApplication app(argc, argv);// 创建主窗口QWidget window;window.setWindowTitle("文件读取示例");// 创建按钮1QPushButton button1("读取文件1");// 创建按钮2QPushButton button2("读取文件2");// 创建按钮3(用于拼接并打印选中的复选框内容)QPushButton button3("拼接并打印选中内容");// 创建一个 QVBoxLayout 用于显示 QCheckBoxQVBoxLayout *layout = new QVBoxLayout(&window);window.setLayout(layout);bool layoutIsEmpty = true; // 用于标记布局是否为空// 存储选中的复选框的文本内容QString selectedText;// 连接按钮1的点击事件QObject::connect(&button1, &QPushButton::clicked, [&]() {// 如果布局不为空,清空 QVBoxLayout 中的内容// 方法1:使用QLayout::removeWidget方法if (!layoutIsEmpty) {QLayoutItem *item;while ((item = layout->takeAt(0)) != nullptr) {QCheckBox *checkBox = qobject_cast<QCheckBox *>(item->widget());if (checkBox) {layout->removeWidget(checkBox);delete checkBox;}delete item;}}// 读取文件1内容并添加到 QVBoxLayoutQFile file("/home/user/qt_normal_test/mytest2/a.txt");if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {QTextStream in(&file);for (int i = 0; i < 10 && !in.atEnd(); ++i) {QString line = in.readLine();QCheckBox *checkBox = new QCheckBox(line);layout->addWidget(checkBox);}file.close();} else {qDebug() << "Error opening file 1: " << file.errorString();}layoutIsEmpty = false; // 布局不再为空});// 连接按钮2的点击事件QObject::connect(&button2, &QPushButton::clicked, [&]() {// 如果布局不为空,清空 QVBoxLayout 中的内容// 方法1:使用QLayout::removeWidget方法if (!layoutIsEmpty) {QLayoutItem *item;while ((item = layout->takeAt(0)) != nullptr) {QCheckBox *checkBox = qobject_cast<QCheckBox *>(item->widget());if (checkBox) {layout->removeWidget(checkBox);delete checkBox;}delete item;}}// 读取文件2内容并添加到 QVBoxLayoutQFile file("/home/user/qt_normal_test/mytest2/b.txt");if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {QTextStream in(&file);for (int i = 0; i < 10 && !in.atEnd(); ++i) {QString line = in.readLine();QCheckBox *checkBox = new QCheckBox(line);layout->addWidget(checkBox);}file.close();} else {qDebug() << "Error opening file 2: " << file.errorString();}layoutIsEmpty = false; // 布局不再为空});// 连接按钮3的点击事件QObject::connect(&button3, &QPushButton::clicked, [&]() {// 遍历 QVBoxLayout 中的复选框,拼接选中的文本内容selectedText.clear(); // 清空已存储的选中文本内容for (int i = 0; i < layout->count(); ++i) {QCheckBox *checkBox = qobject_cast<QCheckBox *>(layout->itemAt(i)->widget());if (checkBox && checkBox->isChecked()) {if (!selectedText.isEmpty()) {selectedText += " "; // 在文本之间插入一个空格}selectedText += checkBox->text();}}// 打印选中的文本内容qDebug() << "选中的内容:" << selectedText;});// 将按钮添加到主窗口layout->addWidget(&button1);layout->addWidget(&button2);layout->addWidget(&button3);window.show();return app.exec();
}
4.main.pro
代码
QT += widgetsTARGET = FileContentReader
TEMPLATE = appSOURCES += main.cppHEADERS +=FORMS +=DISTFILES += \
5.a.txt
代码
66666666
77777777
88888888
99999999
10101010
6.b.txt
代码
111111111111
222222222222
333333333333