Qt SD卡 文件系统挂载、文件预览
生活随笔
收集整理的這篇文章主要介紹了
Qt SD卡 文件系统挂载、文件预览
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*********************************************************************************** Qt SD卡 文件系統掛載、文件預覽* 聲明:* 1. 驗證掛載SD卡;* 2. QTreeView顯示文件系統文件;* 3. UI線程、普通線程通信,以及理解其工作分配;* 4. static const的使用;* 5. QString與const char *的轉換;** 2015-10-20 晴 深圳 南山平山村 曾劍鋒*********************************************************************************/\\\\\\\\\\-*- 目錄 -*-//| 一、cat main.cpp| 二、cat mountthread.h| 三、cat mountthread.cpp| 四、cat mainwidow.h| 五、cat mainwindow.cpp--------------------------------
一、cat main.cpp#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);/*** 1. 創建窗口* 2. 設置標題* 3. 設置無最大最小化按鈕* 4. 顯示窗口*/MainWindow w;w.setWindowTitle("fileSystem");w.setWindowFlags(w.windowFlags()& ~Qt::WindowMaximizeButtonHint& ~Qt::WindowMinimizeButtonHint);w.show();return a.exec();}二、cat mountthread.h#ifndef MOUNTTHREAD_H#define MOUNTTHREAD_H#include <QThread>#include <QString>#include <QMessageBox>#include <QFileInfo>/*** @brief The MountThread class* 掛載文件系統線程,需要創建這個線程的原因如下:* 1. 當mountSD按鈕被按下的時候,需要使按鈕處于無效狀態;* 2. 當SD卡文件系統掛載完畢時,按鈕要處于有效狀態;* 3. 這么做主要是防止一些誤操作,或者也可當作為一種狀態提示;* 4. 基于以上原因,就出現了preMount()、postMount()這兩個信號;* 5. preMount()在處理mount之前發出的信號,UI線程可以更新按鈕到無效狀態;* 6. postMout()在處理mount之后發出的信號,UI先生可以更新按鈕到有效狀態;** 其實之所以要這么做,是因為如果這些在UI線程中做,一般在一個函數里完成,UI線程采用* 從上到下的程序執行流程,無法更新UI控件的狀態,所以目前只能采用這種方式來做。*/class MountThread : public QThread{Q_OBJECTpublic:/*** @param exsdNode 擴展sd卡生成的設備節點* @param mountNode 要將sd卡掛載到那個文件系統節點上* @param parent*/explicit MountThread(QString exsdNode, QString mountNode, QObject *parent = 0);/*** 信號通過傳參的方式,后續由UI線程dealWithUi()槽統一處;*/static const int PRE_MOUNT = 1;static const int POST_MOUNT = 2;static const int DEVICE_UNEXIST = 3;/*** @brief SLEEP_DELAY_MS* 設置mount后等待的時間,這里其實可以不需要,但是本人還是設置了,沒有原因 :)*/static const int SLEEP_DELAY_MS = 1000;signals:void preMount(int mesg);void postMount(int mesg);void deviceUnExist(int mesg);private:/*** 重寫run方法*/void run();private:/*** 擴展sd卡生成的設備節點*/QString exsdNode;/*** 要將sd卡掛載到那個文件系統節點上*/QString mountNode;};#endif // MOUNTTHREAD_H三、cat mountthread.cpp#include "mountthread.h"MountThread::MountThread(QString exsdNode, QString mountNode, QObject *parent) :QThread(parent){/* 獲取sd卡設備節點,mount需要掛載到的文件系統節點 */this->exsdNode = exsdNode;this->mountNode = mountNode;}void MountThread::run(){/* 發送開始mount信號 */emit preMount( PRE_MOUNT );QFileInfo fileInfo( exsdNode );if( fileInfo.exists() ) {/*** 1. 先卸載一下,保證當前次掛載* 2. 重新掛載在目標文件節點上* 3. 等待一下,這里貌似可以不等待的,沒有理由 :)*/system( QString( "umount " ).append( exsdNode ).toLocal8Bit() );system( QString( "mount " ).append( exsdNode ).append( " " ).append( mountNode ).toLocal8Bit() );msleep( SLEEP_DELAY_MS );} else {/* 設備節點不存在,彈出提示框 *//* 2015-11-12 modify : move this to UI thread// QMessageBox::warning(NULL, "WARNING", "Please check your SD card has plugin slot.");emit deviceUnExist(DEVICE_UNEXIST);}/* 發送結束mount信號 */emit postMount( POST_MOUNT );}四、cat mainwidow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QFileSystemModel>#include <QThread>#include <mountthread.h>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();protected:void moveEvent(QMoveEvent *);void resizeEvent(QResizeEvent *);void closeEvent(QCloseEvent *);private slots:void on_detectButton_clicked();void on_umountButton_clicked();/*** 處理MountThread線程發送過來的preMount()和postMount()信號*/void dealwithUi(int mesg);private:QFileSystemModel model;Ui::MainWindow *ui;MountThread *mountThread; // 掛載線程
};#endif // MAINWINDOW_H五、cat mainwindow.cpp#include "mainwindow.h"#include "ui_mainwindow.h"#include <sys/ioctl.h>#include <unistd.h>#include <fcntl.h>#include <QFileInfo>#include <QMessageBox>#include <QTreeView>#include <QDebug>/* sd卡生成的設備節點 */#define EXSD_NODE "/dev/mmcblk1p1"/* sd卡掛載的文件系統節點 */#define MOUNT_NODE "/mnt/exsd"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);/*** 這里主要是設置treeview的一些參數*/ui->showDir->setModel( &model );model.setRootPath( MOUNT_NODE );ui->showDir->setRootIndex( model.index( MOUNT_NODE ) );// Demonstrating look and feel featuresui->showDir->setAnimated( false );ui->showDir->setIndentation( 20 );ui->showDir->setSortingEnabled( true );ui->showDir->setColumnWidth( 0, 250 );/*** 1. 創先mount線程;* 2. 綁定信號與槽。*/mountThread = new MountThread( EXSD_NODE, MOUNT_NODE );connect( mountThread, SIGNAL(preMount(int)), this, SLOT(dealwithUi(int)) );connect( mountThread, SIGNAL(postMount(int)), this, SLOT(dealwithUi(int)) );}void MainWindow::dealwithUi(int mesg){if( MountThread::PRE_MOUNT == mesg ) {/* 將button設置為無效效果 */ui->detectButton->setEnabled( false );ui->umountButton->setEnabled( false );qDebug() << "premount." << endl;} else if ( MountThread::POST_MOUNT == mesg ) {/*** 1. 這里一定需要:* model.setRootPath( "/mnt" );* model.setRootPath( MOUNT_NODE );* 2. /mnt不是固定的,隨便什么值都行,這里主要是為了觸發rootPath改變了,在設置回來,* 要不然,treeview不會顯示。*/model.setRootPath( "/mnt" );model.setRootPath( MOUNT_NODE );ui->showDir->setRootIndex( model.index( MOUNT_NODE ) );/* 恢復按鈕有效效果 */ui->detectButton->setEnabled( true );ui->umountButton->setEnabled( true );qDebug() << "postmount." << endl;/* 2015-11-12 add this for in UI thread */} else if ( MountThread::DEVICE_UNEXIST == mesg ) {QMessageBox::warning(NULL, "WARNING", "Please check your SD card has plugin slot.");}}void MainWindow::on_detectButton_clicked(){/*** 1. 開啟線程,看似無關緊要的,只有短短一行,卻包暗含著UI線程與普通線程的區別;* 2. UI線程維護UI界面的更新;* 3. UI界面不宜做時間很長、耗費資源的事;* 4. 普通線程通過發送信號與UI線程進行溝通,處理UI顯示更新。*/mountThread->start();}void MainWindow::on_umountButton_clicked(){/* 卸載sd卡 */system( QString( "umount " ).append( EXSD_NODE ).toLocal8Bit() );}MainWindow::~MainWindow(){delete ui;}void MainWindow::moveEvent(QMoveEvent *){this->move( QPoint( 0, 0 ) );}void MainWindow::resizeEvent(QResizeEvent *){this->showMaximized();}void MainWindow::closeEvent(QCloseEvent *){exit(0);}
?
總結
以上是生活随笔為你收集整理的Qt SD卡 文件系统挂载、文件预览的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习第二课--图像识别与KNN
- 下一篇: Walle 瓦力 web部署系统