|
本帖最后由 Reachy 于 2017-4-28 15:37 编辑
硬件:CubieAIO-A20 开发板
固件:CubieAIO-A20固件地址
这个只是一个简单的demo,界面没有优化,比较粗糙,简单,但是效果是可以的,实现扫描周围的
wifi热点,做了刷新的按钮,想做到和安卓手机用wifi上网的效果一样.不多说了,直接上代码,
这是main.ccp程序- #include "widget.h"
- #include <QApplication>
- //#include <QtQuick/QtQuick>
- int main(int argc, char *argv[])
- {
- qputenv("QT_IM_MODULE",QByteArray("qtvirtualkeyboard"));
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
复制代码 主要实现的程序:- #include "widget.h"
- #include "ui_widget.h"
- #include <QProcess>
- #include <QDebug>
- #include <QItemDelegate>
- #include <QMessageBox>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- this->resize(350,300);
- this->setWindowTitle("wifi 一览表");
- conStateLayout = new QHBoxLayout;
- conState = new QLabel;
- conState->setText("wifi 未连接");
- stateImage = new QLabel;
- stateImage->resize(25,25);
- conState->setMargin(10);
- stateImage->setMargin(10);
- conStateLayout->addWidget(conState);
- conStateLayout->addWidget(stateImage);
- hLayout = new QHBoxLayout;
- connects = new QPushButton("连接");
- scan = new QPushButton("扫描");
- quit = new QPushButton("退出");
- listView = new QListView;
- model = new QStringListModel;
- scanWifi();
- listView->setModel(model);
- listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
- listView->setCurrentIndex(model->index(0));
- layout = new QVBoxLayout;
- layout->addWidget(listView);
- hLayout->addWidget(connects);
- hLayout->addWidget(scan);
- hLayout->addWidget(quit);
- layout->addLayout(conStateLayout);
- layout->addLayout(hLayout);
- this->setLayout(layout);
- //button connect
- connect(connects,SIGNAL(clicked(bool)),this,SLOT(jumpConnectWifiDialog()));
- //button scan
- connect(scan,SIGNAL(clicked(bool)),this,SLOT(scanWifi()));
- //button quit
- connect(quit,SIGNAL(clicked(bool)),this,SLOT(close()));
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::jumpConnectWifiDialog()
- {
- qDebug()<<":"<<isDialogOpen;
- if(model->stringList().size()<1)
- {
- QMessageBox::warning(this,tr("Warning"),
- tr("no wifi connect"),
- QMessageBox::Cancel);
- return;
- }
- if(isDialogOpen)
- {
- return;
- }
- uname = listView->currentIndex().data().toString();
- ConnectDialog *myDialog = new ConnectDialog(uname);
- myDialog->setGeometry(30,20,200,100);
- myDialog->show();
- //class ConnectDialog connection
- connect(myDialog,SIGNAL(conResult(QString)),this,SLOT(theConResult(QString)));
- isDialogOpen = true;
- }
- void Widget::scanWifi()
- {
- qDebug()<<":"<<isDialogOpen;
- if(isDialogOpen)
- {
- return;
- }
- QProcess pro;
- pro.start("/usr/local/wifiScript/startScanWifi.sh");
- pro.waitForFinished(30000);
- QByteArray output =pro.readAllStandardOutput();
- result = output;
- qDebug()<<result;
- pro.close();
- QStringList list;
- QStringList wifiList = result.split(""");
- for(int i=0;i<wifiList.length()-1;i++)
- {
- if(i%2==1)
- {
- list<<wifiList[i];
- }
- }
- model->setStringList(list);
- listView->setCurrentIndex(model->index(0));
- }
- void Widget::theConResult(QString result)
- {
- qDebug()<<"theConReuslt"<<uname;
- conState->setText(uname+" "+result);
- if(QString("正在连接...")==result)
- {
- QImage image;
- image.load(":/ico/wait.jpeg");
- QImage image1 = image.scaled(25,25);
- stateImage->setPixmap(QPixmap::fromImage(image1));
- }else if(QString("连接失败")==result)
- {
- QImage image;
- image.load(":/ico/wrong.jpeg");
- QImage image1 = image.scaled(25,25);
- stateImage->setPixmap(QPixmap::fromImage(image1));
- }else if(QString("连接成功")==result)
- {
- QImage image;
- image.load(":/ico/17.jpeg");
- QImage image1 = image.scaled(25,25);
- stateImage->setPixmap(QPixmap::fromImage(image1));
- }
- }
- //-------------------------------class ConnectDialog----------------------------
- ConnectDialog::ConnectDialog(QString usrname)
- {
- this->resize(200,100);
- this->setWindowTitle(usrname+" password:");
- this->setWindowFlags(Qt::WindowMinMaxButtonsHint);
- u = usrname;
- cHLayout = new QHBoxLayout;
- cVLayout = new QVBoxLayout;
- passwordEdit = new QLineEdit(this);
- passwordEdit->setGeometry(QRect(20,20,160,25));
- con = new QPushButton(this);
- con->setText("连接");
- cancel = new QPushButton(this);
- cancel->setText("取消");
- cHLayout->addWidget(con);
- cHLayout->addWidget(cancel);
- cVLayout->addWidget(passwordEdit);
- cVLayout->addLayout(cHLayout);
- this->setLayout(cVLayout);
- //button con
- connect(con,SIGNAL(clicked(bool)),this,SLOT(conWifi()));
- //button cancel
- connect(cancel,SIGNAL(clicked(bool)),this,SLOT(cancelWifi()));
- }
- void ConnectDialog::conWifi()
- {
- isDialogOpen = false;
- QString password = passwordEdit->text();
- CThread *t = new CThread(u,password);
- t->start();
- //class CThread connection
- connect(t,SIGNAL(conResult(QString)),this,SIGNAL(conResult(QString)));
- qDebug()<<":"<<isDialogOpen;
- this->close();
- }
- void ConnectDialog::cancelWifi()
- {
- isDialogOpen = false;
- qDebug()<<":"<<isDialogOpen;
- this->close();
- }
- //---------------------------------this is class CThread---------------
- CThread::CThread(QString u, QString p)
- {
- arguments.clear();
- arguments<<u<<p;
- }
- void CThread::run()
- {
- qDebug()<<"thread run: "<<arguments;
- QProcess *p = new QProcess;
- p->start("/usr/local/wifiScript/startWifi.sh",arguments);
- conResult("正在连接...");
- p->waitForFinished(30000);
- p->close();
- p->start("/usr/local/wifiScript/ping.sh");
- p->waitForFinished(30000);
- QByteArray outPut = p->readAllStandardOutput();
- QString result = outPut;
- p->close();
- if(result==""){
- conResult("连接失败");
- }else{
- conResult("连接成功");
- }
- }
复制代码 程序中调用的许多脚本实现的,如:connectWifi.sh , scanWifi.sh , ping.sh 等等见我们附件
实现的效果图如下:
整个实现的文件打包在下面链接中.
工程文件压缩包 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|