qt实现智能家居系统

一、项目介绍

          通过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_OBJECTpublic: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()
{//返回客户端的tcpm_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_OBJECTpublic: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_OBJECTpublic: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_OBJECTpublic: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();elserow--;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;elserow++;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();elserow--;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;elserow++;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界面

 

 

 

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

四、启动后界面

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

 

 

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/65551.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

语音识别智能家居控制设计

目录 一、方案流程及技术规格书设计 二、系统硬件电路设计 三、软件编写及调试 四、系统调试测试与分析 前言 随着科学技术的快速发展&#xff0c;人们对生活品质的要求也不断提高&#xff0c;开始追求更好更方便的生活方式。因此&#xff0c;智能家居系统应运而生。智能家…

智能家居之网关

网关诞生的背景 很多物联网终端设备在设计之初就考虑了低功耗、低成本的需求&#xff0c;因此大量的物联网终端设备是靠电池来工作并且需要运行相当长的一段时间&#xff0c;比如油田、农业相关的传感器&#xff0c;且这些终端设备不需要实时与物联网平台通讯&#xff0c;甚至…

智能家居是否可与ChatGPT深度融合?

​ ChatGPT自2022年面世以来&#xff0c;已为亿万网民提供智能问答服务。然而我们是否曾想到&#xff0c;这一人工智能驱动的聊天机器人&#xff0c;是否可为智能家居赋能? 要实现ChatGPT与智能家居设备之间的无缝对话&#xff0c;单单依靠一台终端是远远不够的。ChatGPT必须…

调查报告数据分析怎么做?

调查报告数据分析怎么做&#xff1f;拿到一份调查报告看选题、选题内容、调查的目的&#xff0c;然后清洗数据&#xff0c;分析数据提炼观点&#xff0c;过数据变化规律&#xff0c;揭示事物内在发展变化和趋势&#xff0c;抓住问题的本质和关键。 一、分析数据提炼观点 数据分…

pyqt5制作翻译软件+爬虫

VSCpythonmysql&#xff0c; 文章目录 目录 文章目录 前言 一、详细代码 第一个文件Ui_untitled.py 第二个fy_main.py文件&#xff1a; 第三个 &#xff1a;bd_pachong.py 二、登录界面&#xff1a; 总结 前言 通过pyqt5实现了翻译界面的可视化&#xff0c;利用python爬虫实现了…

给 30 岁左右的你三个职场跃迁/改变命运的建议!

见字如面&#xff0c;我是军哥&#xff01; 通过公众号的后台用户画像数据&#xff0c;我发现我的读者在 26 到 32 岁的年龄阶段的人数最多&#xff0c;所以今天来聊一聊这个话题&#xff0c;你一定要看到最后&#xff0c;会让你少走很多弯路&#xff01; 核心观点主要包括三个…

38岁程序员和你分享4个人生经验!

蓝色关注&#xff0c;回复“1”获取知名公司程序员和产品经理职级 这是我的第「123」篇原创文章 见字如面&#xff0c;我是军哥。 我最近一直想写一篇稍微长点的文章&#xff0c;毕竟内容篇幅有时候决定内容广度和深度。 距离我之前写的那篇文章《一位互联网老兵的五次认知升级…

【我命由我不由天】30多岁的大龄程序员,应该如何保持职场竞争力?

公众号后台回复“学习”&#xff0c;获取作者独家秘制精品资料 扫描下方海报二维码&#xff0c;试听课程&#xff1a; &#xff08;课程详细大纲&#xff0c;请参见文末&#xff09; 目录 回首往事&#xff1a;自己竟没有任何核心优势&#xff01;公司遇到危机时&#xff0c;大…

写给互联网人35岁之后的建议

作者介绍 李凯东 某大厂大数据总监&#xff0c;管理经验丰富&#xff1b; 负责数据、用户、搜索、推荐、AI等&#xff1b; 曾有10年连续创业经历&#xff1b; 产研运全能&#xff0c;国内知名比赛获奖无数&#xff1b; “数据人创作者联盟”成员。 又到了春节&#xff0c…

程序员如何跨越35岁危机?这篇给点干货建议!

职场&认知洞察 丨 作者 / findyi 这是findyi公众号的第83篇原创文章 这两天在我的读者群里做了一个职业小调研&#xff0c;发现关注我公众号的70%以上都是程序员。 毕竟程序员吸引程序员&#xff0c;这也算猿粪吧&#xff0c;哈哈。 这个小调研也引发大家对程序员行业的激烈…

35岁是人生分水岭?一定要做这7件事

在网上看到这样的帖子&#xff0c;说的是35岁以上的员工&#xff0c;有人脉的卖保险&#xff0c;没人脉的跑滴滴&#xff0c;实在不行送外卖。招聘时&#xff0c;35岁以下优先。调整时&#xff0c;35岁以上优化。对绝大数人来说&#xff0c;35岁是一道坎&#xff0c;35岁是人生…

32岁大龄女程序员的职场经历总结,分享重回职场的感受

不知不觉已经三十过二&#xff0c;我2016年研究生毕业就来到了苏州一家国企。我对自己并没有清晰的职业规划&#xff0c;还记得来这家公司面试时&#xff0c;面试官问我&#xff0c;为啥选择我们公司。我说因为他是国企又有互联网公司的性质&#xff0c;即稳定又能写代码&#…

麦肯锡:给20、30岁职场年轻人的14条建议!条条珍贵

点击“技术领导力”关注∆ 每天早上8:30推送 来源&#xff1a; 《麦肯锡决断力》 1、 选择工作的标准: 选择有利于自己成长的公司&#xff1b; 选择可以从事国际性工作的公司&#xff1b; 选择可以充分发挥自身能力&#xff0c;创造出价值&#xff0c;并且可以为社会做出贡献…

给还没到35岁中年危机的程序员们一些建议

前言 上周&#xff0c;我一个同学被柔性辞退了&#xff0c;是一个外企&#xff0c;给了n 1的补偿&#xff0c;十来万人民币。他在那家公司干了三年多&#xff0c;毕业十年换过四家单位&#xff0c;都是国企或者外企。他今年三十四岁&#xff0c;年前就知道要走的消息&#xff…

30岁程序员回顾人生、展望未来

活动地址&#xff1a;http://blog.csdn.net/blogdevteam/article/details/42172979 一、回顾毕业以来这么多年走过的路 从2007年7月份走出校门到今天2014年12月份&#xff0c;已经整整7年半了。想想7年半之前的自己&#xff0c;再看看现在的我&#xff0c;这么多年走过的路&a…

作为40岁的IT过来人,我有3个建议想对年轻程序员说!

见字如面&#xff0c;我是军哥&#xff01; 我刚毕业的时候也认为技术可以改变世界&#xff0c;如今 40 岁了&#xff0c;N 多年过去了觉得太可笑了&#xff01; 我总结了三个点&#xff0c;分享给年轻的程序员&#xff0c;请一定要看到最后哦&#xff5e; 1、 大部分公司的程序…

35岁+的大龄程序员:面对“中年危机”,这份书单或许能帮到你

摘要&#xff1a;对于多数程序员来说&#xff0c;40岁之前赚到别人60岁的钱是普遍目标&#xff0c;不过职业的进阶也印证着人生的进阶&#xff0c;所以何时开始思考未来的职业规划&#xff0c;都不算早。 知乎上有个话题叫做“程序员的悲哀是什么&#xff1f;”&#xff0c;有个…

冯唐:我给20、30岁IT职场年轻人的建议

说起冯唐&#xff0c;多数人的印象只停留在“春风十里不如你”、“如何避免成为油腻腻的中年人”上&#xff0c;以为他只是个当代作家。 其实&#xff0c;冯唐曾就职于麦肯锡公司&#xff0c;从事旧时被称为军师、幕僚或师爷的工作。也曾担任华润集团战略管理部总经理、中信资…

2020年,冯唐49岁:我给20、30岁IT职场年轻人的建议

点击“技术领导力”关注∆ 每天早上8:30推送 作者| Mr.K 编辑| Emma 来源| 微信公众号 技术领导力(ID&#xff1a;jishulingdaoli) 前天的推文《冯唐&#xff1a;职场人35岁以后&#xff0c;方法论比经验重要》&#xff0c;收到了不少读者的反馈&#xff0c;觉得挺受启发。其…

如何优化供应商采购系统,提升供应商管理和采购流程效能

随着企业采购向数字化转型的发展&#xff0c;供应商采购系统的使用也越来越广泛。如何优化供应商采购系统&#xff0c;提升供应商管理和采购流程效能&#xff0c;已成为企业面临的重要问题。本文将为大家介绍一些优化供应商采购系统的方法&#xff0c;以提升采购效率和管理水平…