QT5-文件分割工具

前言
因为工作需求,需要对文件进行截取操作,找到了一款简单的文件截取工具
工作流程
读取源文件->计算起始位置和结束位置,进行文件偏移->定义文件输出格式(或可以不写)->设定输出路径->开始转换
优点
1.界面简单,操作逻辑明确,便于用户直接操作
2.可以直接指定截取大小,确认后设置编辑框为disable规避非法操作的风险
3.输出路径可以选择软件目录/源文件目录/自定义目录,方便用户查找截取后的文件

缺点
1.不支持单个文件分割为多个文件的处理
2.分割文件时需要自己计算文件大小
优化
1.截取时可以定义分割文件数目以及单个文件大小
2.支持指定块大小进行分割
3.显示源文件大小
4.加入输出转换完成提示以及跳转输出文件路径快捷按键
优化后
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O1bn2u49-1607519836704)(https://uploadfiles.nowcoder.com/images/20201209/559088196_1607493139805/50A04C2865580F0EDC1212F67B8A1EDC "图片标题")]

在原有的基础上进行了修改,支持了多文件分割操作,支持了文件大小显示,加入了操作完成提示,对于特定场景下可以支持到分割出N个固定大小的数据文件到某个文件,且不需要计算文件偏移,但是需要数据按照前四个字节存放数据长度的操作来存放
如果是通用的软件的设计应该更加考虑通用性,应该增加增加固定文件大小的设置选项,或者采用多个模式的设计来区分操作
主要代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include "qdebug.h"
#include<iostream>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton_6->setEnabled(false);
    ui->label_11->setText("");
}

MainWindow::~MainWindow()
{
    delete ui;
}

int key_number = 0;
int begin_index = 0;
int end_index = 0;
int copies_number = 0;
int file_number = 0;
QString format = "bin";
QString org_file_path;
QString output_file_path;
bool redundant_data_falg = false;

void MainWindow::on_pushButton_clicked()
{
    key_number = 0;
    unsigned char temp_buf[4] = {0};
    org_file_path = QFileDialog::getOpenFileName(this,tr("file"),"",tr("all(**)"));
    ui->lineEdit->setText(org_file_path);
    QFile fp(org_file_path);//直接打开指定路径的文件
    if(!fp.open(QIODevice::ReadOnly))
       qDebug()<<"open falid";
    else
       qDebug()<<"open success";//判断打开文件是否成功
    QTextStream stream(&fp);
    fp.read((char *)temp_buf,4);
    char temp[1024];
    for(int i = 0;i < 4;i++)
    {
        key_number = (key_number * 10) + (temp_buf[i]&0xF0);
        key_number = (key_number * 10) + (temp_buf[i]&0x0F);
    }
    sprintf(temp,"%d",key_number);
    ui->label_10->setText(temp);
    fp.close();
    redundant_data_falg = false;
    ui->label_11->setText("");
    ui->pushButton_6->setEnabled(false);
}

void MainWindow::on_pushButton_3_clicked()
{
    if(ui->lineEdit_2->text().size() && ui->lineEdit_3->text().size() && ui->lineEdit_6->text().size())
    {
        begin_index = atoi( ui->lineEdit_2->text().toStdString().c_str());
        end_index = atoi( ui->lineEdit_3->text().toStdString().c_str());
        copies_number = atoi( ui->lineEdit_6->text().toStdString().c_str());
        file_number = (end_index - begin_index + 1) / copies_number;
        if((end_index - begin_index + 1) % copies_number != 0)
        {
            redundant_data_falg = true;
            file_number++;
        }
        ui->pushButton_6->setEnabled(true);
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    output_file_path = QFileDialog::getExistingDirectory(this,"open dir","");
    ui->lineEdit_5->setText(output_file_path);
}

void MainWindow::on_pushButton_4_clicked()
{
    QString strCurrentPath=QDir::currentPath();
    output_file_path = strCurrentPath;
    ui->lineEdit_5->setText(output_file_path);
}

void MainWindow::on_pushButton_5_clicked()
{
    if(org_file_path.size() != 0)
    {
        std::cout<<"org is no null"<<std::endl;
        for(int i = org_file_path.size() - 1;i >= 0;i--)
        {

            if(org_file_path[i] == '/')
            {
                output_file_path = org_file_path.mid(0,i);
                ui->lineEdit_5->setText(output_file_path);
                qDebug()<<output_file_path<<endl;
                break;
            }
        }
    }
}

void MainWindow::on_pushButton_6_clicked()
{
    if(org_file_path.size())
    {
        QFile fp(org_file_path);//直接打开指定路径的文件
        if(!fp.open(QIODevice::ReadOnly))
        {
           qDebug()<<"open falid";
        }
        else
        {
           qDebug()<<"open success";//判断打开文件是否成功
        }
        QTextStream stream(&fp);
        fp.seek((begin_index - 1)*308 + 4);
        unsigned char temp_buf[308] = {0};

        for(int i = 0 ;i < file_number;i++)
        {

            if(ui->lineEdit_4->text().size() != 0)
            {
                 format = ui->lineEdit_4->text();
            }
            if(i == file_number - 1 && redundant_data_falg)
            {
                QString file_path = output_file_path+"/"+"keyfile_"+QString::number(i+1)+"_keysize_"+QString::number((end_index - begin_index + 1)%copies_number)+"."+format;
                QFile key_fp(file_path);
                if(!key_fp.exists())
                {
                    key_fp.open(QIODevice::Append);
                }
                else
                {
                    key_fp.open(QIODevice::ReadWrite);
                }
                for(int j = 0;j < (end_index - begin_index + 1)%copies_number;j++)
                {
                    fp.read((char *)temp_buf,308);
                    key_fp.write((char *)temp_buf,308);
                }
                key_fp.close();
            }
            else
            {
                QString file_path = output_file_path+"/"+"keyfile_"+QString::number(i+1)+"_keysize_"+QString::number(copies_number)+"."+format;
                QFile key_fp(file_path);
                if(!key_fp.exists())
                {
                    key_fp.open(QIODevice::Append);
                }
                else
                {
                    key_fp.open(QIODevice::ReadWrite);
                }
                for(int j = 0;j < copies_number;j++)
                {
                    fp.read((char *)temp_buf,308);
                    key_fp.write((char *)temp_buf,308);
                }
                key_fp.close();
            }
        }
        fp.close();
        ui->label_11->setText("分割完成!");
        ui->pushButton_6->setEnabled(false);
    }
}

UI文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>520</x>
      <y>90</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>浏览</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>90</y>
      <width>301</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>90</y>
      <width>51</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>1.源文件</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>160</y>
      <width>81</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>2.key区间选择</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>210</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>起始数目</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_2">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>210</y>
      <width>81</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>210</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>终止数目</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_3">
    <property name="geometry">
     <rect>
      <x>350</x>
      <y>210</y>
      <width>71</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>250</y>
      <width>61</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>3.输出格式</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_4">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>250</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_6">
    <property name="geometry">
     <rect>
      <x>340</x>
      <y>250</y>
      <width>191</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>(任意格式,不填则默认bin格式)</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_7">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>300</y>
      <width>71</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>4.输出路径</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_5">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>300</y>
      <width>301</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>520</x>
      <y>300</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>浏览</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>640</x>
      <y>210</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>确认</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_4">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>340</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>软件目录</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_5">
    <property name="geometry">
     <rect>
      <x>420</x>
      <y>340</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>源文件目录</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_6">
    <property name="geometry">
     <rect>
      <x>270</x>
      <y>380</y>
      <width>161</width>
      <height>71</height>
     </rect>
    </property>
    <property name="text">
     <string>开始分割</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_8">
    <property name="geometry">
     <rect>
      <x>453</x>
      <y>210</y>
      <width>71</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>每份文件数目</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_6">
    <property name="geometry">
     <rect>
      <x>530</x>
      <y>210</y>
      <width>81</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_9">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>130</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>key总数:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_10">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>130</y>
      <width>91</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QLabel" name="label_11">
    <property name="geometry">
     <rect>
      <x>330</x>
      <y>480</y>
      <width>51</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>分割完成</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 对于您的问题,我可以为您提供以下建议: 首先,您需要安装onnxruntime库,这是在Qt中部署ONNX分割模型所必需的库。 其次,您需要将您的ONNX模型转换为ONNX Runtime格式。您可以使用ONNX Runtime Tools中的onnxruntime_converter工具来执行此操作。将ONNX模型转换为ONNX Runtime格式将使其更容易在Qt中加载和使用。 最后,在Qt中加载ONNX分割模型,您可以使用以下代码示例: ``` #include <onnxruntime_cxx_api.h> // Load the model Ort::SessionOptions session_options; session_options.SetIntraOpNumThreads(1); session_options.SetGraphOptimizationLevel(OrtGraphOptimizationLevel::ORT_ENABLE_ALL); Ort::Session session(env, model_path, session_options); // Get the input tensor Ort::Value input_tensor = Ort::Value::CreateTensor<float>(input_data, input_tensor_size, input_shape.data(), input_shape.size()); // Get the output tensor std::vector<const char*> output_names{ output_name }; std::vector<Ort::Value> output_tensors = session.Run(Ort::RunOptions{ nullptr }, input_names.data(), &input_tensor, 1, output_names.data(), 1); // Process the output tensor float* output_data = output_tensors[0].GetTensorMutableData<float>(); ``` 请注意,上面的代码示例仅供参考,您需要根据您的具体情况进行修改。 希望这可以帮助您成功在Qt中部署ONNX分割模型。 ### 回答2: 在Qt中部署ONNX分割模型的步骤如下: 1. 首先,确保你已经安装了Qt开发环境,并正确配置了编译器和构建工具。 2. 从ONNX模型库或其他来源获取所需的分割模型文件(通常为.onnx文件),并将其放置在你的Qt项目目录下的某个文件夹中。 3. 在Qt项目的.pro文件中添加相关依赖库和路径。例如,在CONFIG部分添加 "c++11" 以确保正确编译C++代码。在INCLUDEPATH部分添加相关的头文件路径,并在LIBS部分添加所需的库文件。 4. 创建一个Qt界面,用于显示分割结果。可以使用Qt的图像显示控件(如QLabel)来展示分割后的图像。 5. 在Qt中使用ONNX Runtime库来加载和运行ONNX模型。首先,将ONNX Runtime库文件(.dll文件或.so文件)添加到Qt项目中。然后,在代码中引入相关的头文件并使用ONNX Runtime提供的API来加载和运行模型。 6. 在代码中获取输入图像,并将其转换为ONNX模型所需的格式。根据模型的要求,可能需要将图像缩放、裁剪或调整通道顺序。 7. 在代码中调用ONNX模型进行图像分割。将输入图像传递给模型,并获取分割结果。 8. 将分割结果显示在Qt界面中的图像显示控件上。你可以将ONNX模型输出的结果转换为Qt所需的图像格式(如QImage)并显示出来。 9. 编译和构建你的Qt项目,并运行它。你应该能够看到输入图像被成功地分割,并在界面上显示出来。 请注意,以上只是一个大致的步骤指导,具体实现可能因项目和模型的不同而有所差异。在实际操作中,可能需要调整和优化代码以适应你的具体需求和环境。 ### 回答3: 在Qt上部署ONNX分割模型可以按照以下步骤进行: 1. 准备好Qt开发环境:确保已经安装并配置好了Qt开发环境,具备构建和运行Qt应用程序的能力。 2. 下载ONNX运行时库:从ONNX的官方网站下载ONNX运行时库,这是一个C++库,用于加载和运行ONNX模型。 3. 导入ONNX运行时库:在Qt项目中添加ONNX运行时库的头文件和库文件,以便在代码中使用。 4. 加载ONNX模型:使用ONNX运行时库提供的函数,加载预先训练好的ONNX分割模型,并将其加载到内存中。 5. 准备输入数据:根据模型的输入要求,准备输入数据。一般来说,输入数据是一个图像,可以将图像转换为模型所需的格式。 6. 运行分割模型:调用ONNX运行时库提供的函数,将输入数据传递给加载的模型,并运行模型进行分割。 7. 处理输出结果:从运行模型之后获取分割结果,根据需要对结果进行后处理和可视化。 8. 显示结果:使用Qt的GUI控件,将处理后的结果显示在应用程序的用户界面上。 需要注意的是,Qt开发环境需要与编译ONNX模型的工具链(如CMake)兼容,并且要根据具体的Qt版本和操作系统进行适配。 以上是一个基本的流程,具体的实现细节可能因为使用的Qt版本、操作系统等因素而有所不同。在实际操作中,可能还需要处理加载模型的错误、性能优化等其他问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值