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成功

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <unistd.h>  
  5.   
  6. int main(){  
  7.     int apipe[2],i,len;  
  8.     char buf[BUFSIZ];  
  9.       
  10.     if(pipe(apipe) == -1){  // 创建一个管道:apipe[0]读,apipe[1]写  
  11.                 // 数据流向:读 <---  写, 即apipe[1]流向apipe[0]  
  12.         perror("pipe"); // 如果创建失败,输出错误原因,退出  
  13.         exit(0);  
  14.     }  
  15.   
  16.     fgets(buf,BUFSIZ,stdin);   // 从输入端读取数据  
  17.       
  18.     len = strlen(buf);         // 获取读入的字符串长度      
  19.   
  20.     write(apipe[1],buf,len);   // 将数据写入到apipe[1],写-->读  
  21.   
  22.     for(i=0;i<len;i++)  
  23.          buf[i]='-';       // 将buf里面的数据全部变为'-'  
  24.       
  25.     read(apipe[0],buf,len);    // 从apipe[0]中读取数据,apipe[1]和apipe[0]  
  26.                    // 是连接在一起的apipe[1] ---> apipe[0]    
  27.                    // 而前面已经在apipe[1]中写入了数据buf,buf的  
  28.                    // 数据来自stdin.  
  29.     write(1,buf,len);          // 1即代表stdout,向stdout写数据,则输出到屏幕  
  30.                                    // 所以整个一圈下来,输入的数据将会输出到屏幕  
  31.     return 0;  
  32. }  

 

参考 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

 

猜你喜欢

转载自blog.csdn.net/mabaoyes/article/details/85246319
今日推荐