2019年8月2日 星期五

【Qt】開啟文件

【Qt】開啟文件

如何在視窗中設置開啟檔案:

1. 使用QFileDialog
#include<QFileDialog>

2. 在事件中加入
QString Filename = QFileDialog::getOpenFileName(this,
                      "Open File","C:\\", "Text Files (*.txt *.NC *.CNC)");
觸發時會呼叫一個開啟檔案的視窗



QFileDialog::getOpenFileName(this,"視窗名稱","開啟檔案位置","允許檔案類型")
允許當按類型用 *加上副檔名(*.txt),多類型時使用空格區分(*.txt *.CNC)

※注意  此方式只會把檔案的路徑存成String


3.開啟選取的文件QFile Files(Filename);             //帶入當按路徑
Files.open(QIODevice::ReadOnly);   //打開檔案,讀取模式
QString tmpChar;                   //宣告字串
while(!Files.atEnd()){             //文件是否結束
    tmpChar += Files.readLine();   //將檔案逐行加到tmpChar
}
ui->textEdit->setText(tmpChar);    //使用textEdit顯示
顯示結果:

沒有留言:

張貼留言

【Arduino】 RS485 實現Modbus Slave

 這次使用Arduino UNO + RS485 的模組,來實現Modbus Slave 的通訊 Pin Name Pin Description VCC 5V A Non-inverting Receiver Input Non-Inverting Driver Output ...