Qt SD卡 文件系统挂载、文件预览
生活随笔
收集整理的這篇文章主要介紹了
Qt SD卡 文件系统挂载、文件预览
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*********************************************************************************** Qt SD卡 文件系統(tǒng)掛載、文件預(yù)覽* 聲明:* 1. 驗(yàn)證掛載SD卡;* 2. QTreeView顯示文件系統(tǒng)文件;* 3. UI線程、普通線程通信,以及理解其工作分配;* 4. static const的使用;* 5. QString與const char *的轉(zhuǎn)換;** 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. 創(chuàng)建窗口* 2. 設(shè)置標(biāo)題* 3. 設(shè)置無(wú)最大最小化按鈕* 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* 掛載文件系統(tǒng)線程,需要?jiǎng)?chuàng)建這個(gè)線程的原因如下:* 1. 當(dāng)mountSD按鈕被按下的時(shí)候,需要使按鈕處于無(wú)效狀態(tài);* 2. 當(dāng)SD卡文件系統(tǒng)掛載完畢時(shí),按鈕要處于有效狀態(tài);* 3. 這么做主要是防止一些誤操作,或者也可當(dāng)作為一種狀態(tài)提示;* 4. 基于以上原因,就出現(xiàn)了preMount()、postMount()這兩個(gè)信號(hào);* 5. preMount()在處理mount之前發(fā)出的信號(hào),UI線程可以更新按鈕到無(wú)效狀態(tài);* 6. postMout()在處理mount之后發(fā)出的信號(hào),UI先生可以更新按鈕到有效狀態(tài);** 其實(shí)之所以要這么做,是因?yàn)槿绻@些在UI線程中做,一般在一個(gè)函數(shù)里完成,UI線程采用* 從上到下的程序執(zhí)行流程,無(wú)法更新UI控件的狀態(tài),所以目前只能采用這種方式來(lái)做。*/class MountThread : public QThread{Q_OBJECTpublic:/*** @param exsdNode 擴(kuò)展sd卡生成的設(shè)備節(jié)點(diǎn)* @param mountNode 要將sd卡掛載到那個(gè)文件系統(tǒng)節(jié)點(diǎn)上* @param parent*/explicit MountThread(QString exsdNode, QString mountNode, QObject *parent = 0);/*** 信號(hào)通過(guò)傳參的方式,后續(xù)由UI線程dealWithUi()槽統(tǒng)一處;*/static const int PRE_MOUNT = 1;static const int POST_MOUNT = 2;static const int DEVICE_UNEXIST = 3;/*** @brief SLEEP_DELAY_MS* 設(shè)置mount后等待的時(shí)間,這里其實(shí)可以不需要,但是本人還是設(shè)置了,沒(méi)有原因 :)*/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:/*** 擴(kuò)展sd卡生成的設(shè)備節(jié)點(diǎn)*/QString exsdNode;/*** 要將sd卡掛載到那個(gè)文件系統(tǒng)節(jié)點(diǎn)上*/QString mountNode;};#endif // MOUNTTHREAD_H三、cat mountthread.cpp#include "mountthread.h"MountThread::MountThread(QString exsdNode, QString mountNode, QObject *parent) :QThread(parent){/* 獲取sd卡設(shè)備節(jié)點(diǎn),mount需要掛載到的文件系統(tǒng)節(jié)點(diǎn) */this->exsdNode = exsdNode;this->mountNode = mountNode;}void MountThread::run(){/* 發(fā)送開(kāi)始mount信號(hào) */emit preMount( PRE_MOUNT );QFileInfo fileInfo( exsdNode );if( fileInfo.exists() ) {/*** 1. 先卸載一下,保證當(dāng)前次掛載* 2. 重新掛載在目標(biāo)文件節(jié)點(diǎn)上* 3. 等待一下,這里貌似可以不等待的,沒(méi)有理由 :)*/system( QString( "umount " ).append( exsdNode ).toLocal8Bit() );system( QString( "mount " ).append( exsdNode ).append( " " ).append( mountNode ).toLocal8Bit() );msleep( SLEEP_DELAY_MS );} else {/* 設(shè)備節(jié)點(diǎn)不存在,彈出提示框 *//* 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);}/* 發(fā)送結(jié)束mount信號(hào) */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線程發(fā)送過(guò)來(lái)的preMount()和postMount()信號(hào)*/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卡生成的設(shè)備節(jié)點(diǎn) */#define EXSD_NODE "/dev/mmcblk1p1"/* sd卡掛載的文件系統(tǒng)節(jié)點(diǎn) */#define MOUNT_NODE "/mnt/exsd"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);/*** 這里主要是設(shè)置treeview的一些參數(shù)*/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. 創(chuàng)先mount線程;* 2. 綁定信號(hào)與槽。*/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設(shè)置為無(wú)效效果 */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不是固定的,隨便什么值都行,這里主要是為了觸發(fā)rootPath改變了,在設(shè)置回來(lái),* 要不然,treeview不會(huì)顯示。*/model.setRootPath( "/mnt" );model.setRootPath( MOUNT_NODE );ui->showDir->setRootIndex( model.index( MOUNT_NODE ) );/* 恢復(fù)按鈕有效效果 */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. 開(kāi)啟線程,看似無(wú)關(guān)緊要的,只有短短一行,卻包暗含著UI線程與普通線程的區(qū)別;* 2. UI線程維護(hù)UI界面的更新;* 3. UI界面不宜做時(shí)間很長(zhǎng)、耗費(fèi)資源的事;* 4. 普通線程通過(guò)發(fā)送信號(hào)與UI線程進(jìn)行溝通,處理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);}
?
總結(jié)
以上是生活随笔為你收集整理的Qt SD卡 文件系统挂载、文件预览的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 深度学习第二课--图像识别与KNN
- 下一篇: Walle 瓦力 web部署系统