qt-C++笔记之按行读取文件并切换复选框打印复选框拼接出的字符串

qt-C++笔记之按行读取文件并切换复选框打印复选框拼接出的字符串

code review!

文章目录

1.运行

2.文件结构

3.main.cc

代码

cpp 复制代码
#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("文件读取示例");

    // 创建按钮1
    QPushButton button1("读取文件1");
    // 创建按钮2
    QPushButton button2("读取文件2");
    // 创建按钮3(用于拼接并打印选中的复选框内容)
    QPushButton button3("拼接并打印选中内容");

    // 创建一个 QVBoxLayout 用于显示 QCheckBox
    QVBoxLayout *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内容并添加到 QVBoxLayout
        QFile 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内容并添加到 QVBoxLayout
        QFile 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

代码

bash 复制代码
QT += widgets

TARGET = FileContentReader
TEMPLATE = app

SOURCES += main.cpp

HEADERS +=

FORMS +=

DISTFILES += \

5.a.txt

代码

bash 复制代码
66666666
77777777
88888888
99999999
10101010

6.b.txt

代码

bash 复制代码
111111111111
222222222222
333333333333
相关推荐
其美杰布-富贵-李9 分钟前
门控模型与Mixture of Experts (MOE) 学习笔记
笔记·学习·moe·门控神经网络
从此不归路9 分钟前
Qt5 进阶【10】应用架构与插件化设计实战:从「单体窗口」走向「可扩展框架」
开发语言·c++·qt·架构
瓦特what?10 分钟前
C++编程防坑指南(小说版)
android·c++·kotlin
sjjhd65212 分钟前
C++模拟器开发实践
开发语言·c++·算法
Queenie_Charlie16 分钟前
素数(线性筛法)
c++·线性筛法·质数·简单数论
求真求知的糖葫芦17 分钟前
RF and Microwave Coupled-Line Circuits射频微波耦合线电路4.3 均匀非对称耦合线学习笔记(上)(自用)
笔记·学习·射频工程
莹莹学编程—成长记18 分钟前
TCP/IP五层模型+网络传输基本流程
网络·c++
ajole21 分钟前
C++学习笔记——C++11
数据结构·c++·笔记·学习·算法·stl
凯子坚持 c26 分钟前
Qt常用控件指南(6)
开发语言·qt
少控科技30 分钟前
QT第三个程序 - 表达式计算器
开发语言·qt