QT程式設計-一、QT簡單控制元件使用
QT程式設計-一、QT簡單控制元件使用
使用linux 總是覺得shell命令難記憶,返回記錄也難看,沒有滾動條。有要寫個shell命令視窗工具的衝動。有QT正好可以寫個小工具練手- shell命令返回獲取視窗工具。
使用控制元件:
QTextEdit
QLineEdit
QPushButton
QComboBox
QPushButton 新增事件處理
設計ui檔案,選擇QPushButton
,右鍵選單中選擇“轉到槽”,再選擇clicked()事件。
例如:
void MainWindow::on_pbtn_run_clicked()
{
ui->textEdit->clear();
//runfork(); //ok
runsystem(); //ok2
}
QTextEdit
清除記錄clear()函式
ui->textEdit->clear();
新增記錄append 函式
ui->textEdit->append(QString::fromLocal8Bit(buf,i));
QComboBox
新增記錄addItem
char g_cmddscstr[128][128];
strcpy(g_cmddscstr[0],"釋放刪除軟體的空間");
ui->comboBox->addItem(g_cmddscstr[0]);
呵呵!一上來就用二維陣列,例子有點難度,希望看官有較強的程式設計基本功。
選擇記錄的處理函式on_comboBox_currentIndexChanged
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
ui->ledt_cmd->setText(QString::fromLocal8Bit(g_cmdstr[index],strlen(g_cmdstr[index])));
ui->ledt_para->setText(QString::fromLocal8Bit(g_parastr[index],strlen(g_parastr[index])));
}
QString型別
QString 轉char* QString成員函式toLocal8Bit().data()
char cmdbuf[200],parabuf[200],tmp[512];
QString cmdstr= ui->ledt_cmd->text();
strcpy(cmdbuf ,cmdstr.toLocal8Bit().data() );
使用PIPO 程式設計
執行shell命令取得命令的返回,我首先想到的使用pipo,使用fork()函式建立子程序,在子程序裡面執行shell命令,命令的返回重定向到pipo中,再從pipo中讀出寫到介面上。
fork()函式
fork()函式,Linux系統呼叫
標頭檔案:
#include <unistd.h>
函式定義:
int fork( void );
返回值:
子程序中返回0,父程序中返回子程序ID,出錯返回-1
函式說明:
一個現有程序可以呼叫fork函式建立一個新程序。由fork建立的新程序被稱為子程序(child process)。fork函式被呼叫一次但返回兩次。兩次返回的唯一區別是子程序中返回0值而父程序中返回子程序ID。
子程序是父程序的副本,它將獲得父程序資料空間、堆、棧等資源的副本。注意,子程序持有的是上述儲存空間的“副本”,這意味著父子程序間不共享這些儲存空間,它們之間共享的儲存空間只有程式碼段。
示例程式碼:
#include <unistd.h>
#include <stdio.h>
int main(int argc, void ** argv )
{
int pid = fork();
if(pid < 0 ) {
// print("error!");
} else if( pid == 0 ) {
// print("This is the child process!");
} else {
// print("This is the parent process! child process id = %d", pid);
}
return 0;
}
參考Linux環境程式設計--waitpid與fork與execlp
https://blog.csdn.net/21aspnet/article/details/6740184
pipe 建立管道
標頭檔案: #include<unistd.h>
原型:result = pipe(int array[2]);
引數:array 包含兩個int型別的陣列
返回值:-1錯誤,0成功
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- int main(){
- int apipe[2],i,len;
- char buf[BUFSIZ];
- if(pipe(apipe) == -1){ // 建立一個管道:apipe[0]讀,apipe[1]寫
- // 資料流向:讀 <--- 寫, 即apipe[1]流向apipe[0]
- perror("pipe"); // 如果建立失敗,輸出錯誤原因,退出
- exit(0);
- }
- fgets(buf,BUFSIZ,stdin); // 從輸入端讀取資料
- len = strlen(buf); // 獲取讀入的字串長度
- write(apipe[1],buf,len); // 將資料寫入到apipe[1],寫-->讀
- for(i=0;i<len;i++)
- buf[i]='-'; // 將buf裡面的資料全部變為'-'
- read(apipe[0],buf,len); // 從apipe[0]中讀取資料,apipe[1]和apipe[0]
- // 是連線在一起的apipe[1] ---> apipe[0]
- // 而前面已經在apipe[1]中寫入了資料buf,buf的
- // 資料來自stdin.
- write(1,buf,len); // 1即代表stdout,向stdout寫資料,則輸出到螢幕
- // 所以整個一圈下來,輸入的資料將會輸出到螢幕
- return 0;
- }
參考 PIPO管道通訊範列(linux)
https://blog.csdn.net/u012704911/article/details/51982654
參考linux i-o重定向與管道程式設計
https://blog.csdn.net/fulianzhou/article/details/48895327
使用popen()函式
發現一個好函式popen(),可以返回shell 命令的返回。
char* MainWindow::cmd_system(const char* command)
{
char* result = "";
FILE *fpRead;
fpRead = popen(command, "r");
char buf[1024];
memset(buf,'\0',sizeof(buf));
while(fgets(buf,1024-1,fpRead)!=NULL)
{
//buf[strlen(buf)-1]=0; //去掉0x0a
if(buf[strlen(buf)-1]==0x0a) buf[strlen(buf)-1]=0; //去掉0x0a
ui->textEdit->append(buf);
result = buf;
}
if(fpRead!=NULL)
pclose(fpRead);
return result;
}
也是採用pipo原理的,好用。這樣不用自己寫pipo程式設計了。
參考Linux的system()和popen()差異
https://blog.csdn.net/liuxingen/article/details/47057539
程式執行圖:
原始碼地址:https://download.csdn.net/download/mabaoyes/10874919