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


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

    code review!

    1.运行

    请添加图片描述

    2.文件结构

    在这里插入图片描述

    3.main.cc

    在这里插入图片描述

    代码

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    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();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126

    4.main.pro

    代码

    QT += widgets
    
    TARGET = FileContentReader
    TEMPLATE = app
    
    SOURCES += main.cpp
    
    HEADERS +=
    
    FORMS +=
    
    DISTFILES += \
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.a.txt

    代码

    66666666
    77777777
    88888888
    99999999
    10101010
    
    • 1
    • 2
    • 3
    • 4
    • 5

    6.b.txt

    代码

    111111111111
    222222222222
    333333333333
    
    • 1
    • 2
    • 3
  • 相关阅读:
    Navigation 组件(二) Fragment 跳转带参数,动画
    Android自定义控件(二) Android下聚光灯实现
    How to Debug the Eclipse C/C++ Indexer
    相似度系列-5:语义方法:BERTSCORE: EVALUATING TEXT GENERATION WITH BERT
    WinHex使用方法详解
    6.3 Cookie对象操作
    类加载器ClassLoader
    微信小程序/uni-app tabBar 页面传参问题
    内边距(padding会影响盒子内边距大小)
    【JavaWeb】火车票管理系统 (三)用户删除之在jsp页面上显示数据
  • 原文地址:https://blog.csdn.net/weixin_43297891/article/details/133846179