- 登录模块
源码请移步
//登录验证按钮函数
void MainWindow::on_loginBtn_clicked()
{//获得userNameLEd输入框的文本userNameLEd->text();//trimmed()去掉前后空格//tr()函数,防止设置中文时乱码if(ui->userNameLEd->text().trimmed() == tr("jiang") && ui->pwdLEd->text() == tr("123")){//跳转到农业的主界面NongYeMainWindow *nongYeMainWindow = new NongYeMainWindow(this);this->close();nongYeMainWindow->show();}else {QMessageBox::warning(this, tr("警告!"), tr("用户名或密码错误!"), QMessageBox::Yes);// 清空内容ui-> userNameLEd->clear();ui-> pwdLEd->clear();//定位光标ui-> userNameLEd->setFocus();}}
- 电子时钟
//显示时间
void MainWindow::update_time()
{//QString cur_time = QTime::currentTime().toString("hh:mm:ss");//ui->label_4->setText(cur_time);QString cur_date = QDate::currentDate().toString("yyyy-MM-dd");//qDebug()<<cur_date;ui->lcdNumber->display(cur_date);QDateTime time = QDateTime::currentDateTime();//QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");QString str = time.toString("hh:mm:ss dddd");ui->label_4->setText(str);
}
主界面:
//跳转到播放器
void NongYeMainWindow::on_pushButton_3_clicked()
{Bofangqi *bafangqi =new Bofangqi();this->close();bafangqi->show();
}
4:电子相册
#include "photo.h"
#include "ui_photo.h"
#include "nongyemainwindow.h"
#include <QFileDialog>
#include <QMessageBox>#include <QScrollBar>#include <time.h>int i = 0, j = 0;//定义全局变量Photo::Photo(QWidget *parent) :QMainWindow(parent),ui(new Ui::Photo)
{ui->setupUi(this);//QPixmap pix(800,480);// ui->label->setPixmap(pix);connect(&mtimer, &QTimer::timeout, this, &Photo::update_time);// 设置样式ui->lcdNumber->setStyleSheet("background: blue;");mtimer.start(1000);//居中ui->label->setAlignment(Qt::AlignCenter);connect(&tutimer, &QTimer::timeout, this, &Photo::img_show);}Photo::~Photo()
{delete ui;
}//返回主界面
void Photo::on_pushButton_clicked()
{NongYeMainWindow *nyM = new NongYeMainWindow(this);this->close();nyM->show();
}//显示时间
void Photo::update_time()
{QString cur_time = QTime::currentTime().toString("hh:mm:ss");ui->label_2->setText(cur_time);QString cur_date = QDate::currentDate().toString("yyyy-MM-dd");//qDebug()<<cur_date;ui->lcdNumber->display(cur_date);
}//获取文件路径
void Photo::on_FileButton_clicked()
{//槽函数当中,获取一个QString filename = QFileDialog::getOpenFileName(this);ui->FileEdit->setText(filename);/* //获取多个文件QStringList filenames = QFileDialog::getOpenFileNames(this);qDebug()<<filenames;*///获取即将要保存的文件路径// QString savefile = QFileDialog::getSaveFileName(this);qDebug()<<filename;
}void Photo::on_startshow_clicked()
{tutimer.start(1000);ui->label_4->setText("正在播放图片!");// i++;ui->FileEdit->setText(imgpath[i]);//图片路径显示ui->FileEdit->setText("正在播放图片》》》》》》》》》》》》》》》》》》》》》》》》》》》》");}void Photo::on_stopshow_clicked()
{tutimer.stop();ui->label_4->setText("已停止播放图片!");
}void Photo::img_show()
{//打开文件夹是imgList为空//pix.load(imgList[i]); //下载图片pix.load(imgpath[i]);//设置图片大小为Label的大小,否则就会出现滑动条pix = pix.scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio);ui->label->setPixmap(pix);i++;if (i > j) {i = 0;}
}//打开图片文件
void Photo::on_filesOpen_clicked_clicked()
{int k = 0;
/*QStringList list = QFileDialog::getOpenFileNames(this, "select img", "C:\\Users\\Mature\\Desktop\\project\\project", "*.*");for (it = list.begin(); it < list.end();it++) {imgpath[k] = *it;k++;}j = k-1;if (j < 0) {j = 0;}*///指定文件夹过滤,非图片文件//获取文件所在位置QString path = QFileDialog::getExistingDirectory(this, tr("open dir"), QDir::currentPath(), QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);//qDebug()<<path;QDir dir(path);if (!dir.exists()) {qDebug()<<path<<"不存在";}QStringList filters;//设置过滤规则filters<<QString("*.jpg")<<QString("*.jpeg")<<QString("*.bmp")<<QString("*.png");dir.setFilter(QDir::Files|QDir::NoSymLinks);dir.setNameFilters(filters);QStringList list = dir.entryList();for (it = list.begin(); it < list.end();it++) {//qDebug()<<*it;//文件的所有图片imgpath[k] = QString("%1/%2").arg(path).arg(*it);k++;}j = k-1;if (j < 0) {j = 0;}ui->label_4->setText("请播放图片!");QMessageBox::information(this,"消息提示","你指定的文件夹所有图片打开成功!",QMessageBox::Ok);ui->FileEdit->setText(imgpath[i]);//图片路径显示
}
//前进
void Photo::on_Speed_clicked()
{tutimer.stop();i=i+1;if(i>j){i=0;}pix.load(imgpath[i]);pix = pix.scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio);//设置图片大小为Label的大小,否则就会出现滑动条//pix.scaled(ui->label->width(),ui->label->height(),Qt::IgnoreAspectRatio);//设置图片大小为Label的大小,否则就会出现滑动条ui-> label->setPixmap(pix);//显示图片ui->label_4->setText("当前打开的图片为:");ui->FileEdit->setText(imgpath[i]);//图片路径显示}//后退
void Photo::on_back_clicked()
{tutimer.stop();i=i-1;if(i<0){i=j;}pix.load(imgpath[i]);pix = pix.scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio);//设置图片大小为Label的大小,否则就会出现滑动条ui->label->setPixmap(pix);ui->label_4->setText("当前打开的图片为:");ui->FileEdit->setText(imgpath[i]);//图片路径显示
}void Photo::on_big_clicked()
{//放大tutimer.stop();double w = pix.width();double h = pix.height();pix = pix.scaled(w*1.2,h*1.2,Qt::KeepAspectRatio);ui-> label->setPixmap(pix);ui->label_4->setText("当前打开的图片为:");ui->FileEdit->setText(imgpath[i]);//图片路径显示
}void Photo::on_small_clicked()
{ //缩小tutimer.stop();double w = pix.width();double h = pix.height();pix = pix.scaled(w*0.8,h*0.8,Qt::KeepAspectRatio);ui-> label->setPixmap(pix);ui->label_4->setText("当前打开的图片为:");ui->FileEdit->setText(imgpath[i]);//图片路径显示}void Photo::on_left_clicked()
{//向左旋转tutimer.stop();QMatrix matrix;matrix.rotate(90);pix = pix.transformed(matrix,Qt::FastTransformation);ui-> label->setPixmap(pix);ui->label_4->setText("当前打开的图片为:");ui->FileEdit->setText(imgpath[i]);//图片路径显示
}void Photo::on_right_clicked()
{//向右旋转tutimer.stop();QMatrix matrix;matrix.rotate(-90);pix = pix.transformed(matrix,Qt::FastTransformation);ui-> label->setPixmap(pix);ui->label_4->setText("当前打开的图片为:");ui->FileEdit->setText(imgpath[i]);//图片路径显示}
//用鼠标点击放大和缩小指定位置,这个是扩展功能
5:监控系统
#include "camera.h"
#include "ui_camera.h"
#include "nongyemainwindow.h"int sta_ca=0;//判断摄像头的开启状态Camera::Camera(QWidget *parent) :QMainWindow(parent),ui(new Ui::Camera)
{ui->setupUi(this);connect(&mtimer, &QTimer::timeout, this, &Camera::update_time);// 设置样式ui->lcdNumber->setStyleSheet("background: blue;");mtimer.start(1000);/*camera=new QCamera;//摄像头viewfinder=new QCameraViewfinder(this);imageCapture=new QCameraImageCapture(camera);//截图部件camera->setViewfinder(viewfinder);QObject::connect(ui->aButton,SIGNAL(clicked()),this,SLOT(ShowTheCapture()));QObject::connect(ui->saveButton,SIGNAL(clicked()),this,SLOT(SavePicture()));QObject::connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);camera->setCaptureMode(QCamera::CaptureStillImage);camera->setViewfinder(viewfinder);*///this->setWindowIcon(QPixmap(":/img/camera.ico"));ui->imglabel->setAlignment(Qt::AlignCenter);ui->capButton->setEnabled(false);//获取活动的摄像头设备名字(当然也可以获取设备描述)QList<QCameraInfo> infos = QCameraInfo::availableCameras();foreach (QCameraInfo info, infos) {ui->deviceBox->addItem(info.deviceName());}// connect(ui->backBt, &QPushButton::clicked, this, &Camera::backBt_clicked);//connect(ui->startBt, &QPushButton::clicked, this, &Camera::startBt_clicked);//connect(ui->captureBt, &QPushButton::clicked, this, &Camera::captureBt_clicked);}Camera::~Camera()
{delete qCapture;delete qCfinder;delete qCamera;// qCamera->stop();delete ui;
}//返回主菜单
void Camera::on_pushButton_clicked()
{if(sta_ca==1)//摄像头开启,就关闭{qCamera->stop();sta_ca=0;//判断摄像头的开启状态}NongYeMainWindow *nyM = new NongYeMainWindow(this);this->close();nyM->show();
}//显示时间
void Camera::update_time()
{QString cur_time = QTime::currentTime().toString("hh:mm:ss");ui->label_2->setText(cur_time);QString cur_date = QDate::currentDate().toString("yyyy-MM-dd");//qDebug()<<cur_date;ui->lcdNumber->display(cur_date);
}/*
void Camera::ShowTheCapture()
{imageCapture->capture();
}
void Camera::displayImage(int ,QImage image)
{//QImage image1(ui->label->x(),ui->label->y(),QImage::Format_RGB888);// image=new QImage(220,220,QImage::Format_RGB888);// image.setPixel(,Format_RGB888);// QColor color(QImage::Format_RGB888);// image.fill(color);//QImage image2;//QImage::Format format = image1.format();//qDebug() << (int)format;image=image.convertToFormat(QImage::Format_RGB888);ui->label->setPixmap(QPixmap::fromImage(image));QImage::Format format = image.format();qDebug() << (int)format;}
void Camera::SavePicture()
{const QPixmap *pixmap = ui->imglabel->pixmap();if(pixmap){pixmap->save("C:\\Users\\Lamchiu\\Desktop\\hallo.jpg");qDebug()<<"保存上了"<<endl;}}*/void Camera::on_stopCa_clicked()
{qCamera->stop();sta_ca=0;//判断摄像头的开启状态
}void Camera::on_startCa_clicked()
{qCamera = new QCamera(ui->deviceBox->currentText().toUtf8(), this);qCapture = new QCameraImageCapture(qCamera);qCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);qCamera->setCaptureMode(QCamera::CaptureStillImage);connect(qCapture, &QCameraImageCapture::imageCaptured, this, &Camera::img_save);qCfinder = new QCameraViewfinder(ui->widget);qCfinder->resize(ui->widget->size());qCamera->setViewfinder(qCfinder);qCfinder->show();qCamera->start();sta_ca=1;//判断摄像头的开启状态qCamera->searchAndLock(); //锁定,以便在图像捕捉发生时对焦并且设置与取景器没有不同ui->capButton->setEnabled(true);
}void Camera::on_capButton_clicked()
{mtimer.stop();QString pathname = QString("%1_%2.jpeg").arg(cur_date).arg(cur_time);qDebug()<<pathname;qCapture->capture(pathname); //捕捉图像qCamera->unlock(); //解锁相机准备下一张照片
}
void Camera::img_save(int id, const QImage &preview)
{qDebug()<<id;QPixmap pix = QPixmap::fromImage(preview);pix.scaled(ui->imglabel->size());ui->imglabel->setPixmap(pix);QString fileName = QFileDialog::getSaveFileName(this, tr("save file"), QDir::currentPath(), tr("jpegfile(*.jpg)"));pix.save(fileName);QMessageBox::information(this,"消息提示","照片保存成功!",QMessageBox::Ok);// pix.save("C:\\Users\\Lamchiu\\Desktop\\hllo.jpg");if(fileName.isEmpty()) {QMessageBox::warning(this, "消息提示", "未检测到要保存的文件名字,已取消!", QMessageBox::Ok);}qDebug()<<"保存上了"<<endl;mtimer.start(1000);
}
6:音乐播放器:
#include "bofangqi.h"
#include "ui_bofangqi.h"
#include "nongyemainwindow.h"
#include "QSlider"
#include "QMessageBox"int current_x = 0, current_y = 0;Bofangqi::Bofangqi(QWidget *parent) :QMainWindow(parent),ui(new Ui::Bofangqi)
{ui->setupUi(this);connect(&mtimer, &QTimer::timeout, this, &Bofangqi::update_time);// 设置样式ui->lcdNumber->setStyleSheet("background: blue;");mtimer.start(1000);this->setWindowTitle("播放器");this->setWindowIcon(QPixmap(":/img/player.ico"));ui->playBt->setEnabled(false);//connect(ui->backBt, &QPushButton::clicked, this, &Bofangqi::backBt_clicked);connect(ui->openBt, &QPushButton::clicked, this, &Bofangqi::openFiles_clicked);connect(ui->playBt, &QPushButton::clicked, this, &Bofangqi::playBt_clicked);
}Bofangqi::~Bofangqi()
{delete ui;
}//返回主界面
void Bofangqi::on_pushButton_clicked()
{NongYeMainWindow *nyM = new NongYeMainWindow(this);this->close();nyM->show();//关闭正在播放的音乐qMediaPlayer.stop();
}
//显示时间
void Bofangqi::update_time()
{QString cur_time = QTime::currentTime().toString("hh:mm:ss");ui->label_2->setText(cur_time);QString cur_date = QDate::currentDate().toString("yyyy-MM-dd");//qDebug()<<cur_date;ui->lcdNumber->display(cur_date);
}void Bofangqi::openFiles_clicked()
{int k = 0;//获取文件所在位置QString path = QFileDialog::getExistingDirectory(this, tr("open dir"), QDir::currentPath(), QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);//qDebug()<<path;QDir dir(path);if (!dir.exists()) {qDebug()<<path<<"不存在";}QStringList filters;//设置过滤规则filters<<QString("*.mp3");dir.setFilter(QDir::Files|QDir::NoSymLinks);dir.setNameFilters(filters);QStringList list = dir.entryList();for (it = list.begin(); it < list.end();it++) {//qDebug()<<*it;musicpath[k] = QString("%1/%2").arg(path).arg(*it);qDebug()<<musicpath[k];k++;}current_y = k-1;if (current_y < 0) {current_y = 0;}if (!musicpath->isEmpty()) {ui->playBt->setEnabled(true);}QMessageBox::information(this,"消息提示","你指定的文件夹音乐打开成功!",QMessageBox::Ok);ui->label->setText("请播放!");ui->lineEdit->setText(musicpath[0]);//音乐路径显示}void Bofangqi::playBt_clicked()
{if (qMediaPlayer.mediaStatus() == QMediaPlayer::NoMedia) {qMediaPlayer.setMedia(QUrl::fromLocalFile(musicpath[current_x]));ui->lineEdit->setText(musicpath[current_x]);//音乐路径显示current_x++;if (current_x > current_y) {current_x = 0;}qMediaPlayer.play();ui->playBt->setText("暂停");ui->label->setText("正在播放!");//判断是否有正在播放}else if (qMediaPlayer.state() == QMediaPlayer::PlayingState) {qMediaPlayer.pause();ui->playBt->setText("继续");ui->label->setText("暂停中!");//没有歌曲正在}else {qMediaPlayer.play();ui->playBt->setText("暂停");ui->label->setText("正在播放!");}}//上一首
void Bofangqi::on_nextBt_clicked()
{current_x--;if (current_x < 0) {current_x = current_y;}qMediaPlayer.setMedia(QUrl::fromLocalFile(musicpath[current_x]));qMediaPlayer.play();ui->lineEdit->setText(musicpath[current_x]);//音乐路径显示ui->label->setText("正在播放!");}
void Bofangqi::on_prevBt_clicked()
{//下一首current_x++;if (current_x > current_y) {current_x = 0;}qMediaPlayer.setMedia(QUrl::fromLocalFile(musicpath[current_x]));qMediaPlayer.play();ui->lineEdit->setText(musicpath[current_x]);//音乐路径显示ui->label->setText("正在播放!");
}QSlider *qSlider;//快退
void Bofangqi::on_QianJing_clicked()
{ui->lineEdit->setText(musicpath[current_x]);//音乐路径显示ui->label->setText("正在播放!");//qSlider->triggerAction(QSlider::SliderPageStepAdd);qint64 position=qMediaPlayer.position();qDebug()<<"position"<<position;position=position-5000;ui->progressBar->setValue(position/1000);qMediaPlayer.setPosition(position);//如果滑块改变位置,则改变音乐进度}void Bofangqi::on_HouTui_clicked()//快进
{ui->lineEdit->setText(musicpath[current_x]);//音乐路径显示ui->label->setText("正在播放!");// qSlider->triggerAction(QSlider::SliderPageStepSub);qint64 position=qMediaPlayer.position();qDebug()<<"position"<<position;position=position+5000;ui->progressBar->setValue(position/1000);qMediaPlayer.setPosition(position);//如果滑块改变位置,则改变音乐进度ui->lineEdit->setText(musicpath[current_x]);//音乐路径显示ui->label->setText("正在播放!");
}
7:视频播放器:
#include "video.h"
#include "ui_video.h"
#include "QMessageBox"
#include "nongyemainwindow.h"
#include <QBoxLayout>
#include <QMouseEvent>Video::Video(QWidget *parent) :QMainWindow(parent),ui(new Ui::Video)
{ui->setupUi(this);//MainWindow自带layout,所以需要自定义layout并把它设置成中心layout//否则将会出现不能设置layout错误QWidget *widget = new QWidget;this->setCentralWidget(widget);player = new QMediaPlayer;Playlist = new QMediaPlaylist();player->setPlaylist(Playlist);videoWidget = new QVideoWidget(widget);player->setVideoOutput(videoWidget);ui->progressBar->setRange(0, player->duration() / 1000);//水平布局,控制按钮QBoxLayout *ctlLayout = new QHBoxLayout;ctlLayout->addWidget(ui->openFileBtn);ctlLayout->addWidget(ui->playBtn);ctlLayout->addWidget(ui->stopBtn);ctlLayout->addWidget(ui->fullScrBtn);ctlLayout->addWidget(ui->TuiButton);ctlLayout->addWidget(ui->GoButton);ctlLayout->addWidget(ui->backButton);//垂直布局:视频播放器、进度条、控制按钮布局QBoxLayout *mainLayout = new QVBoxLayout;mainLayout->addWidget(videoWidget);mainLayout->addWidget(ui->progressBar);mainLayout->addLayout(ctlLayout);//设置布局widget->setLayout(mainLayout);//信号槽QObject::connect(ui->openFileBtn,SIGNAL(clicked()),this,SLOT(openFile()));QObject::connect(ui->playBtn,SIGNAL(clicked()),this,SLOT(playVideo()));QObject::connect(ui->stopBtn,SIGNAL(clicked()),this,SLOT(stopVideo()));QObject::connect(ui->fullScrBtn,SIGNAL(clicked()),this,SLOT(fullScr()));
}Video::~Video()
{delete ui;
}void Video::openFile()
{QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"));addToPlaylist(fileNames);QMessageBox::information(this,"消息提示","你指定的视频打开成功!\n请播放!",QMessageBox::Ok);
}void Video::playVideo()
{m_playerState = QMediaPlayer::PlayingState;player->play();
}void Video::stopVideo()
{m_playerState = QMediaPlayer::StoppedState;player->stop();
}void Video::fullScr()
{if(m_playerState == QMediaPlayer::PlayingState){videoWidget->setFullScreen(true);}else{videoWidget->setFullScreen(false);}}void Video::addToPlaylist(const QStringList& fileNames)
{foreach (QString const &argument, fileNames) {QFileInfo fileInfo(argument);if (fileInfo.exists()) {QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());if (fileInfo.suffix().toLower() == QLatin1String("m3u")) {Playlist->load(url);} elsePlaylist->addMedia(url);} else {QUrl url(argument);if (url.isValid()) {Playlist->addMedia(url);}}}
}void Video::on_backButton_clicked()
{m_playerState = QMediaPlayer::StoppedState;player->stop(); //停止播放NongYeMainWindow *nyM = new NongYeMainWindow(this);this->close();nyM->show();}void Video::on_GoButton_clicked()
{//qSlider->triggerAction(QSlider::SliderPageStepAdd);qint64 position=player->position();qDebug()<<"position"<<position;position=position+5000;ui->progressBar->setValue(position/1000);player->setPosition(position);//如果滑块改变位置,则改变视频进度
}void Video::on_TuiButton_clicked()
{// qSlider->triggerAction(QSlider::SliderPageStepSub);qint64 position=player->position();qDebug()<<"position"<<position;position=position-5000;ui->progressBar->setValue(position/1000);player->setPosition(position);//如果滑块改变位置,则改变视频进度
}void Video::mousePressEvent(QMouseEvent *event)
{Q_UNUSED(event);//使用定义的参数,去掉警告m_bChecked=!m_bChecked;if(m_bChecked){player->pause(); //第一次点击暂停}else{player->play(); //第二次点击播放}}
到这整个基于qt开发的智能系统:电子相册,监控摄像头模块,音乐播放器,视频播放器,电子时钟
智能嵌入式系统介绍完,里面还有很多小的处理模块,就不一一赘述,感兴趣的小伙伴,可以移步本人开发的的完整项目源码:已共享项目源码,需要自取
感谢感谢!!!!