基于qt的智能家居系统

文章正文
发布时间:2025-01-07 12:53

          通过TCP/IP协议实现客户端和和服务端的链接,服务端和下位机通过串口通信的方式链接,传递信息,客户端通过账号登录进入进入智能家居服务系统,账号登录和QQ登录类似,我采用的是数据库的方式实现数据的存储和调用,界面使用qt-ui的方式进行建立,有摄像头操作,音乐播放器操作和视频操作,客户端通过按下开关的操作将json数据发送到服务端再经过串口的方式对下位机进行操作。

二、项目技术

qt-UI界面,c++,json,TCP/IP协议,摄像头,音乐播放器,视频操作,串口通信,stm32f407vet6。

三、项目内容

1.服务端头文件:需要在pro文件中添加 network(TCP数据链接) serialport(串口通信)

#ifndef SERVERWINDOW_H #define SERVERWINDOW_H #include <QMainWindow> #include <QTcpServer> #include <QTcpSocket> #include <QHostAddress> // 引入串口通信的两个头文件(第一步) #include <QSerialPort> // 提供访问串口的功能 #include <QSerialPortInfo> // 提供系统中存在的串口信息 namespace Ui { class serverwindow; } class serverwindow : public QMainWindow { Q_OBJECT public: explicit serverwindow(QWidget *parent = 0); ~serverwindow(); QTcpServer* m_server; //服务器对象 QTcpSocket* m_tcp; //客户端对象 QSerialPort m_serial;//串口 private slots: //点击连接服务器 void on_pushButton_clicked(); //点击发送按钮 void on_pushButton_2_clicked(); //客户端连接上 void ClientConnect(); //有数据来了,则响应readyread信号 void readData(); //客户端断开连接 void Clientdisconnect(); //打开串口 void on_pushButton_3_clicked(); //关闭串口 void on_pushButton_4_clicked(); //串口接受信息的处理函数 void messlot(); //刷新串口 void on_pushButton_5_clicked(); private: Ui::serverwindow *ui; }; #endif // SERVERWINDOW_H

服务端.c文件:

#include "serverwindow.h" #include "ui_serverwindow.h" serverwindow::serverwindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::serverwindow) { ui->setupUi(this); setWindowTitle(tr("服务端")); } serverwindow::~serverwindow() { delete ui; } //启动服务器 void serverwindow::on_pushButton_clicked() { //第一步:创建对象 QTcpserver对象 m_server= new QTcpServer(this); //第二步:监听 listen函数 unsigned short port = ui->lineEdit->text().toShort(); m_server->listen(QHostAddress::AnyIPv4 ,port); //第三步:处理信号槽函数处理 (槽函数中获取客户端的socket) QObject::connect(m_server,&QTcpServer::newConnection, this,&serverwindow::ClientConnect); // nextpendingconnction函数可以获取到客户端的socket,也就是QTCPsocket对象 //第四步:如果连接上了,设置按钮为灰色 ui->pushButton->setEnabled(false); } //发送数据 void serverwindow::on_pushButton_2_clicked() { //使用write函数,QTcpsocket对象 QString temp = ui->textEdit_2->toPlainText();//获取textedit的文本 m_tcp->write(temp.toUtf8()); if(!temp.isEmpty() ) { ui->textEdit->append(temp); } ui->textEdit_2->clear(); } //客户端连接后响应 void serverwindow::ClientConnect() { //返回客户端的tcp m_tcp = m_server->nextPendingConnection(); //打印下客户端的ip地址 QHostAddress addr = m_tcp->peerAddress(); ui->textEdit->append(addr.toString()); connect(m_tcp,&QTcpSocket::readyRead,this,&serverwindow::readData ); connect(m_tcp,&QTcpSocket::disconnected ,this,&serverwindow::Clientdisconnect ); } //服务器接受数据 void serverwindow::readData() { QString temp = m_tcp->readAll(); if(!temp.isNull()) { ui->textEdit->append(temp); } } //客户端断开连接 void serverwindow::Clientdisconnect() { ui->textEdit->append("客户端断开连接"); m_tcp->close(); ui->pushButton->setEnabled(true); ui->pushButton->setText("启动服务器"); } //刷新串口 void serverwindow::on_pushButton_5_clicked() { //清空串口名 ui->comboBox->clear(); //遍历串口信息 foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) { ui->comboBox->addItem(info.portName()); } } //打开串口 void serverwindow::on_pushButton_3_clicked() { m_serial.setPortName(ui->comboBox->currentText());//设置串口名 m_serial.setBaudRate(ui->comboBox_2->currentText().toInt());//设置波特率 m_serial.open(QIODevice::ReadWrite); if(m_serial.isOpen()) { ui->label_7->setText("串口打开成功"); connect(&m_serial,&QSerialPort::readyRead,this,&serverwindow::messlot); } else { ui->label_7->setText("串口打开失败"); } } //串口接受信息的处理函数 void serverwindow::messlot() { QByteArray array = m_serial.readAll(); ui->textEdit->insertPlainText(array); } //关闭串口 void serverwindow::on_pushButton_4_clicked() { m_serial.close(); if(m_serial.isOpen()) { ui->label_7->setText("串口关闭失败"); connect(&m_serial,&QSerialPort::readyRead,this,&serverwindow::messlot); } else { ui->label_7->setText("串口已关闭"); } }

服务端ui界面:

 2.客户端.h文件:需要在pro文件中加入serialport(TCP通信) sql(数据库)  multimedia和 multimediawidgets(媒体,摄像头,音乐播放器)

①客户端界面1

#ifndef CLIENT1_H #define CLIENT1_H #include <QWidget> #include <QSqlDataBase> #include <QDebug> #include <QSqlQuery> #include <QSqlError> #include <QMessageBox> #include<QMovie> #include"client2.h" #include"client3.h" namespace Ui { class client1; } class client1 : public QWidget { Q_OBJECT public: explicit client1(QWidget *parent = 0); ~client1(); private slots: //显示密码 void on_checkBox_2_clicked(); //记住密码 void on_checkBox_clicked(); //登录 void on_pushButton_clicked(); //注册 void on_pushButton_2_clicked(); private: Ui::client1 *ui; }; static bool createConnection2() { QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE","connection1"); //创建指定连接名称的数据库 db1.setDatabaseName("data.db"); //设置数据库名称 if(!db1.open()){ //打开数据库 qDebug() << "cannot open the database"; return false; } QSqlQuery query1(db1); //对指定连接的数据库执行sql语句 query1.exec("create table data1(id varchar(20),mima varchar(20))"); query1.exec("insert into data1(id,mima) values('123456','123456')"); query1.exec("insert into data1(id,mima) values('654321','654321')"); query1.exec("insert into data1(id,mima) values('4444','4444')"); return true; } #endif // CLIENT1_H

界面1.c文件

#include "client1.h" #include "ui_client1.h" client1::client1(QWidget *parent) : QWidget(parent), ui(new Ui::client1) { ui->setupUi(this); setWindowTitle(tr("登录")); ui->lineEdit_2->setEchoMode(QLineEdit::Password); QMovie *movie1 = new QMovie(":/new/prefix1/ies/ies1.gif"); ui->label_3->setMovie(movie1); ui->label_3->setScaledContents(true); movie1->start(); } client1::~client1() { delete ui; } void client1::on_checkBox_clicked() { static int flag1 =1; //------------------------------------------------------------------------------------- //数据库处理 QSqlDatabase db1 = QSqlDatabase::database("connection1"); //得到指定连接名称数据库 QSqlQuery query1(db1); query1.exec("select * from data1"); //执行sql语句 //--------------------------------------------------------------------------------------- if(flag1) { while (query1.next()) { if(query1.value(0).toString() == ui->comboBox->currentText()) { QString temp = query1.value(1).toString(); ui->lineEdit_2->setText(temp); flag1=0; break; } } } else { ui->lineEdit_2->setText(""); flag1=1; } } //显示密码 void client1::on_checkBox_2_clicked() { static int flag =1; if(flag) { ui->lineEdit_2->setEchoMode(QLineEdit::Normal); flag=0; } else { ui->lineEdit_2->setEchoMode(QLineEdit::Password); flag=1; } } //登录 void client1::on_pushButton_clicked() { //------------------------------------------------------------------------------------- //数据库处理 QSqlDatabase db1 = QSqlDatabase::database("connection1"); //得到指定连接名称数据库 QSqlQuery query1(db1); query1.exec("select * from data1"); //执行sql语句 //--------------------------------------------------------------------------------------- //密码判断对比 while (query1.next()) { if(query1.value(0).toString() == ui->comboBox->currentText()) { if(query1.value(1).toString() == ui->lineEdit_2->text()) { client3 *configWindow = new client3; configWindow->show(); close(); break; } } } } //注册 void client1::on_pushButton_2_clicked() { client2 *configWindow = new client2; configWindow->show(); }

界面1ui界面

②界面2.h文件

#ifndef CLIENT2_H #define CLIENT2_H #include <QWidget> #include<QMovie> #include"client1.h" namespace Ui { class client2; } class client2 : public QWidget { Q_OBJECT public: explicit client2(QWidget *parent = 0); ~client2(); private slots: //记住密码 void on_checkBox_clicked(); //关闭界面 void on_pushButton_6_clicked(); //注册账号 void on_pushButton_5_clicked(); private: Ui::client2 *ui; }; #endif // CLIENT2_H

 界面2.c文件

#include "client2.h" #include "ui_client2.h" client2::client2(QWidget *parent) : QWidget(parent), ui(new Ui::client2) { ui->setupUi(this); setWindowTitle(tr("注册")); ui->lineEdit_5->setEchoMode(QLineEdit::Password); QMovie *movie1 = new QMovie(":/new/prefix1/ies/ies.gif"); ui->label->setMovie(movie1); ui->label->setScaledContents(true); movie1->start(); } client2::~client2() { delete ui; } //记住密码操作 void client2::on_checkBox_clicked() { static int flag =1; if(flag) { ui->lineEdit_5->setEchoMode(QLineEdit::Normal); flag=0; } else { ui->lineEdit_5->setEchoMode(QLineEdit::Password); flag=1; } } //关闭窗口 void client2::on_pushButton_6_clicked() { close(); } //调用数据库进行注册 void client2::on_pushButton_5_clicked() { //------------------------------------------------------------------------------------- //数据库处理 QSqlDatabase db1 = QSqlDatabase::database("connection1"); //得到指定连接名称数据库 QSqlQuery query1(db1); query1.exec("select * from data1"); //执行sql语句 //--------------------------------------------------------------------------------------- //将注册的账号存入数据库 query1.prepare("insert into data1(id,mima) values(?,?)"); QString idValue = ui->lineEdit_5->text(); query1.addBindValue(idValue); query1.exec(); //执行插入语句 query1.exec("select * from student"); //执行查询语句 query1.seek(-1); //回到初始位置,即第一个数据的前一个位置 close(); }

界面2UI界面

③界面智能家居服务系统

界面3.h文件

#ifndef CLIENT3_H #define CLIENT3_H #include <QWidget> #include<QTcpSocket> #include <QTcpSocket> #include <QHostAddress> #include"client1.h" #include"client2.h" #include<QTabWidget> #include<QPixmap> #include<QDebug> #include <QVideoWidget> #include<QCamera> #include<QCameraInfo> #include<QCameraImageCapture> #include<QMediaPlayer> #include<QMediaPlaylist> #include<QStringList> #include<QFileDialog> #include<QMovie> #include<QPixmap> #include<QPainter> #include <QTcpServer> #include <QTcpSocket> #include <QWidget> #include <QListWidgetItem> #include <QVideoWidget> #include<QKeyEvent> #include<QProcess> namespace Ui { class client3; } class client3 : public QWidget { Q_OBJECT public: explicit client3(QWidget *parent = 0); ~client3(); private slots: void on_pushButton_25_clicked(); void on_pushButton_14_clicked(); void on_pushButton_13_clicked(); void clientDisConnect(); //接受数据 void readData(); //连接后响应的槽函数 connected信号 void connectServer(); void on_pushButton_38_clicked(); void on_pushButton_51_clicked(); void on_pushButton_81_clicked(); void on_pushButton_82_clicked(); void on_pushButton_24_clicked(); void on_pushButton_83_clicked(); void on_pushButton_53_clicked(); void on_pushButton_54_clicked(); void on_pushButton_84_clicked(); void on_pushButton_85_clicked(); void on_pushButton_86_clicked(); void on_pushButton_87_clicked(); void on_pushButton_3_clicked(); void on_pushButton_4_clicked(); void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_5_clicked(); void on_pushButton_6_clicked(); void on_pushButton_7_clicked(); void on_pushButton_8_clicked(); void on_pushButton_9_clicked(); void on_pushButton_10_clicked(); void on_pushButton_11_clicked(); void on_pushButton_12_clicked(); void on_verticalSlider_sliderMoved(int position); void on_horizontalSlider_sliderMoved(int position); void on_pushButton_addvideo_clicked(); void on_pushButton_deletevideo_clicked(); void on_pushButton_clearvideo_clicked(); void on_pushButton_play_clicked(); void on_pushButton_cancel_clicked(); void on_pushButton_stop_cont_clicked(); void on_pushButton_prev_clicked(); void on_pushButton_stop_2_clicked(); void on_listw_doubleClicked(const QModelIndex &index); private: Ui::client3 *ui; QTcpSocket* m_tcp; QCamera* ca; QVideoWidget* vw; QCameraImageCapture* cap; QMediaPlayer* player; QMediaPlaylist* playlist; //记录当前媒体周期 QString music_duration; QString music_duratio; //记录正在播放的视频 QString current_video; QProcess *p; QMediaPlayer* player1; QMediaPlaylist* playlist1; QString video_duration; QString video_duratio; }; #endif

 界面3.c文件

#include "client3.h" #include "ui_client3.h" client3::client3(QWidget *parent) : QWidget(parent), ui(new Ui::client3) { ui->setupUi(this); setWindowTitle(tr("小徐智能家居")); m_tcp = new QTcpSocket(this); ui->pushButton_14->setEnabled(true); ui->pushButton_13->setEnabled(false); //摄像头操作 QList<QCameraInfo> infos=QCameraInfo::availableCameras(); foreach (QCameraInfo info, infos) { ui->comboBox->addItem(info.deviceName()); } //音乐播放器操作 player=new QMediaPlayer(this); playlist=new QMediaPlaylist(this); playlist->setPlaybackMode(QMediaPlaylist::Loop); player->setPlaylist(playlist); //视频播放器操作 player1=new QMediaPlayer(this); playlist1=new QMediaPlaylist(this); playlist1->setPlaybackMode(QMediaPlaylist::Loop); player1->setPlaylist(playlist1); vw=new QVideoWidget(this); ui->horizontalLayout->addWidget(vw); player1->setVideoOutput(vw); //音乐播放器槽函数 connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64))); connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(onDurationChanged(qint64))); //链接信号和槽 QObject::connect(player,&QMediaPlayer::durationChanged,this,[&](quint64 duration){ //周期改变(歌曲时间显示出来) // 水平滑块 设置进度条范围 周期 ui->horizontalSlider->setRange(0,duration); //文本显示歌曲周期,以字符串打印 music_duration=QString("%1:%2").arg(duration/1000/60).arg(duration/1000%60); ui->label_5->setText(QString("%1").arg(music_duration)); //显示歌曲切换 当前歌曲 播放列表 当前时长 ui->listWidget->setCurrentRow(playlist->currentIndex()); }); // QObject::connect(player,&QMediaPlayer::positionChanged,this,[&](quint64 position){ //进度改变(进度条的更新) 设置值 位置 ui->horizontalSlider->setValue(position); //文本显示歌曲实时进度以字符串的形式打印 music_duratio=QString("%1:%2").arg(position/1000/60).arg(position/1000%60); ui->label_12->setText(QString("%1").arg(music_duratio)); }); //视频播放器槽函数 connect(player1,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64))); connect(player1,SIGNAL(durationChanged(qint64)),this,SLOT(onDurationChanged(qint64))); //链接信号和槽 QObject::connect(player1,&QMediaPlayer::durationChanged,this,[&](quint64 duration){ //周期改变(歌曲时间显示出来) // 水平滑块 设置进度条范围 周期 ui->horizontalSlider->setRange(0,duration); //文本显示歌曲周期,以字符串打印 video_duration=QString("%1:%2:%3").arg(duration/1000/60/60) .arg(duration/1000/60%60) .arg(duration/1000%60); ui->label_13->setText(QString("00:00:00/%1").arg(video_duration)); //显示歌曲切换 当前歌曲 播放列表 当前时长 ui->listWidget1->setCurrentRow(playlist1->currentIndex()); }); // QObject::connect(player,&QMediaPlayer::positionChanged,this,[&](quint64 position){ //进度改变(进度条的更新) 设置值 位置 ui->horizontalSlider->setValue(position); //文本显示歌曲实时进度以字符串的形式打印 // video_duratio=QString("%1:%2").arg(position/1000/60).arg(position/1000%60); ui->label_12->setText(QString("%1;%2;%3/%4").arg(position/1000/60/60) .arg(position/1000/60%60) .arg(position/1000%60) .arg(video_duratio)); }); p=new QProcess(this); } client3::~client3() { if(p->state()==QProcess::Running) { p->kill(); p->waitForFinished(); } delete ui; } //连接服务器 void client3::on_pushButton_14_clicked() { //第二步:处理信号和槽断开的信号和槽 connect(m_tcp,&QTcpSocket::disconnected,this,clientDisConnect ); //第三步:调用connecttohost连接服务器 unsigned short port = ui->lineEdit_6->text().toShort(); QString ip = ui->lineEdit_5->text(); m_tcp->connectToHost(QHostAddress(ip),port ); // connectToHost(QHostAddress(ip),port); //第四步:当连接成功会响应connected信号。 connect(m_tcp,&QTcpSocket::connected,this,&client3::connectServer ); //第五步: 响应readyread信号。准备接受数据。 connect(m_tcp,&QTcpSocket::readyRead,this,&client3::readData ); ui->pushButton_14->setEnabled(false); ui->pushButton_13->setEnabled(true); } //断开连接 void client3::on_pushButton_13_clicked() { m_tcp->close(); ui->pushButton_14->setEnabled(true); ui->pushButton_13->setEnabled(false); } //接受数据 void client3::readData() { QByteArray msg = m_tcp->readAll(); ui->textEdit->append(msg); } //连接后响应的槽函数 connected信号 void client3::connectServer() { ui->textEdit->append(tr("连接服务器成功...")); } //响应disconnect信号 void client3::clientDisConnect() { ui->textEdit->append("断开服务器..."); } void client3::on_pushButton_25_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_16->setPixmap(img); ui->pushButton_25->setText(tr("关")); QString msg = "厨房灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_16->setPixmap(omg); ui->pushButton_25->setText(tr("开")); QString msg = "厨房灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_38_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/fsk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/fsg.png"); static int a=1; if(a==1) { ui->label_33->setPixmap(img); ui->pushButton_38->setText(tr("关")); QString msg = "厨房风扇打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_33->setPixmap(omg); ui->pushButton_38->setText(tr("开")); QString msg = "厨房风扇关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_51_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_17->setPixmap(img); ui->pushButton_51->setText(tr("关")); QString msg = "餐厅灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_17->setPixmap(omg); ui->pushButton_51->setText(tr("开")); QString msg = "餐厅灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_81_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_34->setPixmap(img); ui->pushButton_81->setText(tr("关")); QString msg = "卫生间灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_34->setPixmap(omg); ui->pushButton_81->setText(tr("开")); QString msg = "卫生间灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_82_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/fsk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/fsg.png"); static int a=1; if(a==1) { ui->label_35->setPixmap(img); ui->pushButton_82->setText(tr("关")); QString msg = "卫生间风扇打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_35->setPixmap(omg); ui->pushButton_82->setText(tr("开")); QString msg = "卫生间风扇关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_24_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_38->setPixmap(img); ui->pushButton_24->setText(tr("关")); QString msg = "次卧1灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_38->setPixmap(omg); ui->pushButton_24->setText(tr("开")); QString msg = "次卧1灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_83_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_40->setPixmap(img); ui->pushButton_83->setText(tr("关")); QString msg = "次卧2灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_40->setPixmap(omg); ui->pushButton_83->setText(tr("开")); QString msg = "次卧2灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_53_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_18->setPixmap(img); ui->pushButton_53->setText(tr("关")); QString msg = "书房灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_18->setPixmap(omg); ui->pushButton_53->setText(tr("开")); QString msg = "书房灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_54_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/fsk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/fsg.png"); static int a=1; if(a==1) { ui->label_19->setPixmap(img); ui->pushButton_54->setText(tr("关")); QString msg = "卫生间风扇打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_19->setPixmap(omg); ui->pushButton_54->setText(tr("开")); QString msg = "卫生间风扇关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_84_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_42->setPixmap(img); ui->pushButton_84->setText(tr("关")); QString msg = "客厅灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_42->setPixmap(omg); ui->pushButton_84->setText(tr("开")); QString msg = "客厅灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_85_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/fsk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/fsg.png"); static int a=1; if(a==1) { ui->label_43->setPixmap(img); ui->pushButton_85->setText(tr("关")); QString msg = "客厅风扇打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_43->setPixmap(omg); ui->pushButton_85->setText(tr("开")); QString msg = "客厅风扇关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_86_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/dpk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/dpg.png"); static int a=1; if(a==1) { ui->label_47->setPixmap(img); ui->pushButton_86->setText(tr("关")); QString msg = "主卧灯打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_47->setPixmap(omg); ui->pushButton_86->setText(tr("开")); QString msg = "主卧灯关闭"; m_tcp->write(msg.toUtf8()); a=1; } } void client3::on_pushButton_87_clicked() { QPixmap img=QPixmap(":/new/prefix1/ies/fsk.png"); QPixmap omg=QPixmap(":/new/prefix1/ies/fsg.png"); static int a=1; if(a==1) { ui->label_48->setPixmap(img); ui->pushButton_87->setText(tr("关")); QString msg = "主卧风扇打开"; m_tcp->write(msg.toUtf8()); a=0; } else { ui->label_48->setPixmap(omg); ui->pushButton_87->setText(tr("开")); QString msg = "主卧风扇关闭"; m_tcp->write(msg.toUtf8()); a=1; } } //打开摄像头 void client3::on_pushButton_3_clicked() { ca=new QCamera(ui->comboBox->currentText().toUtf8(),this); cap=new QCameraImageCapture(ca,this); QObject::connect(cap,&QCameraImageCapture::imageCaptured,this,[&](int id,const QImage &preview) { QPixmap map=QPixmap::fromImage(preview); map=map.scaled(ui->label_4->size()); ui->label_4->setPixmap(map); }); vw=new QVideoWidget(ui->widget); vw->resize((ui->widget->size())); ca->setViewfinder(vw); vw->show(); ca->start(); ui->pushButton_3->setEnabled(false); ui->pushButton_4->setEnabled(true); ui->pushButton->setEnabled(true); } //关闭摄像头 void client3::on_pushButton_4_clicked() { ca->stop(); vw->hide(); delete ca; delete vw; ui->pushButton_3->setEnabled(true); ui->pushButton_4->setEnabled(false); ui->pushButton->setEnabled(false); } //拍照 void client3::on_pushButton_clicked() { cap->capture(); } //保存 void client3::on_pushButton_2_clicked() { QString lj=ui->lineEdit_4->text(); cap->capture(lj); } //添加音乐 void client3::on_pushButton_5_clicked() { QStringList music_files = QFileDialog::getOpenFileNames(this,"选择音乐文件","C:/Users/86186/Documents/client/mucia"); //去重 for(int i = 0;i<ui->listWidget->count();i++) { for(int j = 0;j<music_files.count();j++) { if(ui->listWidget->item(i)->text()==music_files.at(j)) { music_files.removeAt(j); break; } } } if(music_files.isEmpty()) return ; for(int i = 0;i<music_files.count();i++) { QString file = music_files.at(i); //文件加到播放列表 playlist->addMedia(QUrl::fromLocalFile(file)); } ui->listWidget->addItems(music_files); ui->listWidget->setCurrentRow(0); } //删除音乐 void client3::on_pushButton_6_clicked() { int row = ui->listWidget->currentRow(); QListWidgetItem* item = ui->listWidget->takeItem(row); if(item) { delete item; } playlist->removeMedia(row); } //清空音乐 void client3::on_pushButton_7_clicked() { ui->listWidget->clear(); playlist->clear(); } //播放 void client3::on_pushButton_8_clicked() { int row=ui->listWidget->currentRow(); playlist->setCurrentIndex(row); player->play(); player->setVolume(ui->verticalSlider->value()); ui->pushButton_10->setText(tr("暂停")); ui->pushButton_10->setEnabled(true); } //重播 void client3::on_pushButton_9_clicked() { player->stop(); ui->pushButton_10->setText(tr("暂停")); ui->pushButton_10->setEnabled(false); } //暂停 void client3::on_pushButton_10_clicked() { if(player->state()==QMediaPlayer::PlayingState) { player->pause(); ui->pushButton_10->setText(tr("继续")); } else if(player->state()==QMediaPlayer::PausedState) { player->play(); ui->pushButton_10->setText(tr("暂停")); } } //上一首 void client3::on_pushButton_11_clicked() { int row=ui->listWidget->currentRow(); if(row==0) row=ui->listWidget->count(); else row--; ui->listWidget->setCurrentRow(row); playlist->setCurrentIndex(row); player->play(); player->setVolume(ui->verticalSlider->value()); ui->pushButton_10->setText(tr("暂停")); ui->pushButton_10->setEnabled(true); } //下一首 void client3::on_pushButton_12_clicked() { int row=ui->listWidget->currentRow(); if(row==ui->listWidget->count()-1) row=0; else row++; ui->listWidget->setCurrentRow(row); playlist->setCurrentIndex(row); player->play(); player->setVolume(ui->verticalSlider->value()); ui->pushButton_10->setText(tr("暂停")); ui->pushButton_10->setEnabled(false); } //音乐音量大小 void client3::on_verticalSlider_sliderMoved(int position) { player->setVolume(position); ui->label_6->setText(QString("音量:%1").arg(position)); } //音乐进度条 void client3::on_horizontalSlider_sliderMoved(int position) { player->setPosition(position); } //添加视频 void client3::on_pushButton_addvideo_clicked() { QStringList video_files = QFileDialog::getOpenFileNames(this,"选择视频文件","C:/Users/86186/Documents/client/video","*.mp4 *.avi"); //去重 for(int i = 0;i<ui->listWidget1->count();i++) { for(int j = 0;j<video_files.count();j++) { if(ui->listWidget1->item(i)->text()==video_files.at(j)) { video_files.removeAt(j); break; } } } if(video_files.isEmpty()) return ; for(int i = 0;i<video_files.count();i++) { QString file =video_files.at(i); //文件加到播放列表 playlist1->addMedia(QUrl::fromLocalFile(file)); } ui->listWidget1->addItems(video_files); ui->listWidget1->setCurrentRow(0); } //删除视频 void client3::on_pushButton_deletevideo_clicked() { int row = ui->listWidget1->currentRow(); QListWidgetItem* item = ui->listWidget1->takeItem(row); if(item) { delete item; } playlist1->removeMedia(row); } //清空视频 void client3::on_pushButton_clearvideo_clicked() { ui->listWidget1->clear(); playlist1->clear(); } //播放视频 void client3::on_pushButton_play_clicked() { int row=ui->listWidget1->currentRow(); playlist1->setCurrentIndex(row); player1->play(); player1->setVolume(ui->verticalSlider->value()); ui->pushButton_stop_cont->setText(tr("暂停")); ui->pushButton_stop_cont->setEnabled(true); } //重播视频 void client3::on_pushButton_cancel_clicked() { player1->stop(); ui->pushButton_stop_cont->setText(tr("暂停")); ui->pushButton_stop_cont->setEnabled(false); //ui->lablee->clear(); } //暂停视频 void client3::on_pushButton_stop_cont_clicked() { QString cmd="killall"; QStringList args; if(ui->pushButton_stop_cont->text()=="暂停") { args<<"-STOP"<<"mplayer"; ui->pushButton_stop_cont->setText("继续"); } else { args<<"-CONT"<<"mplayer"; ui->pushButton_stop_cont->setText("暂停"); } p->execute(cmd,args); } //上一个视频 void client3::on_pushButton_prev_clicked() { int row=ui->listWidget1->currentRow(); if(row==0) row=ui->listWidget1->count(); else row--; ui->listWidget1->setCurrentRow(row); playlist1->setCurrentIndex(row); player1->play(); player1->setVolume(ui->verticalSlider->value()); ui->pushButton_stop_cont->setText(tr("暂停")); ui->pushButton_stop_cont->setEnabled(true); } //下一个视频 void client3::on_pushButton_stop_2_clicked() { int row=ui->listWidget1->currentRow(); if(row==ui->listWidget1->count()-1) row=0; else row++; ui->listWidget1->setCurrentRow(row); playlist1->setCurrentIndex(row); player1->play(); player1->setVolume(ui->verticalSlider->value()); ui->pushButton_stop_cont->setText(tr("暂停")); ui->pushButton_stop_cont->setEnabled(false); } //双击播放 void client3::on_listw_doubleClicked(const QModelIndex &index) { int row = ui->listWidget1->currentRow(); playlist1->setCurrentIndex(row); player->play(); player->setVolume(ui->verticalSlider->value()); ui->pushButton_stop_cont->setText(tr("暂停")); ui->pushButton_stop_cont->setEnabled(true); }

界面3UI界面

 

 

 

此处注意需要配置资源文件

四、启动后界面

我所使用的在代码中的都是动图 

 

 

 

总体的项目就是这样,希望对于一些人有所帮助! 

首页
评论
分享
Top