CubieBoard博客-HomeCubieTech公司网站

cubie.cc CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 4487|回复: 0

CubieAIO-A20 用QT做简单的wifi扫描界面

[复制链接]

18

主题

0

好友

7655

积分

论坛元老

Rank: 8Rank: 8

贡献
1417
金钱
3317
威望
1417
买家信用
卖家信用
积分
7655
发表于 2017-4-28 14:50:19 |显示全部楼层
本帖最后由 Reachy 于 2017-4-28 15:37 编辑

硬件:CubieAIO-A20 开发板
固件:CubieAIO-A20固件地址

这个只是一个简单的demo,界面没有优化,比较粗糙,简单,但是效果是可以的,实现扫描周围的
wifi热点,做了刷新的按钮,想做到和安卓手机用wifi上网的效果一样.不多说了,直接上代码,

这是main.ccp程序
  1. #include "widget.h"
  2. #include <QApplication>
  3. //#include <QtQuick/QtQuick>

  4. int main(int argc, char *argv[])
  5. {
  6.     qputenv("QT_IM_MODULE",QByteArray("qtvirtualkeyboard"));
  7.     QApplication a(argc, argv);
  8.     Widget w;
  9.     w.show();

  10.     return a.exec();
  11. }
复制代码
主要实现的程序:
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QProcess>
  4. #include <QDebug>
  5. #include <QItemDelegate>
  6. #include <QMessageBox>

  7. Widget::Widget(QWidget *parent) :
  8.     QWidget(parent),
  9.     ui(new Ui::Widget)
  10. {
  11.     ui->setupUi(this);
  12.     this->resize(350,300);
  13.     this->setWindowTitle("wifi 一览表");

  14.     conStateLayout = new QHBoxLayout;
  15.     conState = new QLabel;
  16.     conState->setText("wifi 未连接");
  17.     stateImage = new QLabel;
  18.     stateImage->resize(25,25);
  19.     conState->setMargin(10);
  20.     stateImage->setMargin(10);
  21.     conStateLayout->addWidget(conState);
  22.     conStateLayout->addWidget(stateImage);


  23.     hLayout = new QHBoxLayout;
  24.     connects = new QPushButton("连接");
  25.     scan = new QPushButton("扫描");
  26.     quit = new QPushButton("退出");
  27.     listView = new QListView;
  28.     model = new QStringListModel;
  29.     scanWifi();

  30.     listView->setModel(model);
  31.     listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
  32.     listView->setCurrentIndex(model->index(0));
  33.     layout = new QVBoxLayout;
  34.     layout->addWidget(listView);
  35.     hLayout->addWidget(connects);
  36.     hLayout->addWidget(scan);
  37.     hLayout->addWidget(quit);
  38.     layout->addLayout(conStateLayout);
  39.     layout->addLayout(hLayout);

  40.     this->setLayout(layout);

  41.     //button connect
  42.    connect(connects,SIGNAL(clicked(bool)),this,SLOT(jumpConnectWifiDialog()));

  43.    //button scan
  44.    connect(scan,SIGNAL(clicked(bool)),this,SLOT(scanWifi()));

  45.    //button quit
  46.    connect(quit,SIGNAL(clicked(bool)),this,SLOT(close()));

  47. }

  48. Widget::~Widget()
  49. {
  50.     delete ui;
  51. }

  52. void Widget::jumpConnectWifiDialog()
  53. {

  54.     qDebug()<<":"<<isDialogOpen;

  55.     if(model->stringList().size()<1)
  56.     {
  57.         QMessageBox::warning(this,tr("Warning"),
  58.         tr("no wifi connect"),
  59.         QMessageBox::Cancel);
  60.         return;
  61.     }

  62.     if(isDialogOpen)
  63.     {
  64.         return;
  65.     }

  66.     uname = listView->currentIndex().data().toString();
  67.     ConnectDialog *myDialog = new ConnectDialog(uname);
  68.     myDialog->setGeometry(30,20,200,100);
  69.     myDialog->show();

  70.     //class ConnectDialog connection
  71.     connect(myDialog,SIGNAL(conResult(QString)),this,SLOT(theConResult(QString)));
  72.     isDialogOpen = true;
  73. }

  74. void Widget::scanWifi()
  75. {

  76.     qDebug()<<":"<<isDialogOpen;
  77.     if(isDialogOpen)
  78.     {
  79.         return;
  80.     }

  81.     QProcess pro;
  82.     pro.start("/usr/local/wifiScript/startScanWifi.sh");
  83.     pro.waitForFinished(30000);
  84.     QByteArray output =pro.readAllStandardOutput();
  85.     result = output;
  86.     qDebug()<<result;
  87.     pro.close();

  88.     QStringList list;
  89.      QStringList wifiList = result.split("\"");
  90.      for(int i=0;i<wifiList.length()-1;i++)
  91.      {
  92.          if(i%2==1)
  93.          {
  94.              list<<wifiList[i];
  95.          }
  96.      }
  97.      model->setStringList(list);
  98.      listView->setCurrentIndex(model->index(0));
  99. }

  100. void Widget::theConResult(QString result)
  101. {
  102.     qDebug()<<"theConReuslt"<<uname;
  103.     conState->setText(uname+" "+result);
  104.     if(QString("正在连接...")==result)
  105.     {
  106.         QImage image;
  107.         image.load(":/ico/wait.jpeg");
  108.         QImage image1 = image.scaled(25,25);
  109.         stateImage->setPixmap(QPixmap::fromImage(image1));
  110.     }else if(QString("连接失败")==result)
  111.     {
  112.         QImage image;
  113.         image.load(":/ico/wrong.jpeg");
  114.         QImage image1 = image.scaled(25,25);
  115.         stateImage->setPixmap(QPixmap::fromImage(image1));
  116.     }else if(QString("连接成功")==result)
  117.     {
  118.         QImage image;
  119.         image.load(":/ico/17.jpeg");
  120.         QImage image1 = image.scaled(25,25);
  121.         stateImage->setPixmap(QPixmap::fromImage(image1));
  122.     }
  123. }

  124. //-------------------------------class ConnectDialog----------------------------
  125. ConnectDialog::ConnectDialog(QString usrname)
  126. {

  127.     this->resize(200,100);
  128.     this->setWindowTitle(usrname+" password:");
  129.     this->setWindowFlags(Qt::WindowMinMaxButtonsHint);
  130.     u = usrname;
  131.     cHLayout = new QHBoxLayout;
  132.     cVLayout = new QVBoxLayout;
  133.     passwordEdit = new QLineEdit(this);
  134.     passwordEdit->setGeometry(QRect(20,20,160,25));
  135.     con = new QPushButton(this);
  136.     con->setText("连接");
  137.     cancel = new QPushButton(this);
  138.     cancel->setText("取消");
  139.     cHLayout->addWidget(con);
  140.     cHLayout->addWidget(cancel);
  141.     cVLayout->addWidget(passwordEdit);
  142.     cVLayout->addLayout(cHLayout);
  143.     this->setLayout(cVLayout);

  144.     //button con
  145.     connect(con,SIGNAL(clicked(bool)),this,SLOT(conWifi()));
  146.     //button cancel
  147.     connect(cancel,SIGNAL(clicked(bool)),this,SLOT(cancelWifi()));

  148. }

  149. void ConnectDialog::conWifi()
  150. {
  151.     isDialogOpen = false;
  152.     QString password = passwordEdit->text();
  153.     CThread *t = new CThread(u,password);
  154.     t->start();
  155.     //class CThread connection
  156.     connect(t,SIGNAL(conResult(QString)),this,SIGNAL(conResult(QString)));
  157.     qDebug()<<":"<<isDialogOpen;
  158.     this->close();
  159. }

  160. void ConnectDialog::cancelWifi()
  161. {
  162.     isDialogOpen = false;
  163.     qDebug()<<":"<<isDialogOpen;
  164.     this->close();
  165. }

  166. //---------------------------------this is class CThread---------------
  167. CThread::CThread(QString u, QString p)
  168. {
  169.     arguments.clear();
  170.     arguments<<u<<p;
  171. }

  172. void CThread::run()
  173. {
  174.     qDebug()<<"thread run: "<<arguments;
  175.         QProcess *p = new QProcess;
  176.         p->start("/usr/local/wifiScript/startWifi.sh",arguments);
  177.         conResult("正在连接...");
  178.         p->waitForFinished(30000);
  179.         p->close();
  180.         p->start("/usr/local/wifiScript/ping.sh");
  181.         p->waitForFinished(30000);
  182.         QByteArray outPut = p->readAllStandardOutput();
  183.         QString result = outPut;
  184.         p->close();
  185.         if(result==""){
  186.             conResult("连接失败");
  187.         }else{
  188.             conResult("连接成功");
  189.         }
  190. }
复制代码
程序中调用的许多脚本实现的,如:connectWifi.sh , scanWifi.sh , ping.sh 等等见我们附件
实现的效果图如下:


整个实现的文件打包在下面链接中.
工程文件压缩包

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|邮件群发|cubie.cc---深刻的嵌入式技术和应用讨论中文社区 ( 粤ICP备13051116号-1  

GMT+8, 2019-8-24 22:14 , Processed in 0.024906 second(s), 8 queries , Apc On.

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc. | Style by Coxxs

回顶部