- 登录模块
源码请移步
void MainWindow::on_loginBtn_clicked()
{
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_date = QDate::currentDate().toString("yyyy-MM-dd");
ui->lcdNumber->display(cur_date);
QDateTime time = QDateTime::currentDateTime();
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);
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");
ui->lcdNumber->display(cur_date);
}
void Photo::on_FileButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this);
ui->FileEdit->setText(filename);
qDebug()<<filename;
}
void Photo::on_startshow_clicked()
{
tutimer.start(1000);
ui->label_4->setText("正在播放图片!");
ui->FileEdit->setText(imgpath[i]);
ui->FileEdit->setText("正在播放图片》》》》》》》》》》》》》》》》》》》》》》》》》》》》");
}
void Photo::on_stopshow_clicked()
{
tutimer.stop();
ui->label_4->setText("已停止播放图片!");
}
void Photo::img_show()
{
pix.load(imgpath[i]);
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;
QString path = QFileDialog::getExistingDirectory(this, tr("open dir"), QDir::currentPath(), QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);
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++) {
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);
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);
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);
ui->imglabel->setAlignment(Qt::AlignCenter);
ui->capButton->setEnabled(false);
QList<QCameraInfo> infos = QCameraInfo::availableCameras();
foreach (QCameraInfo info, infos) {
ui->deviceBox->addItem(info.deviceName());
}
}
Camera::~Camera()
{
delete qCapture;
delete qCfinder;
delete qCamera;
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");
ui->lcdNumber->display(cur_date);
}
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);
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->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");
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);
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++) {
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("正在播放!");
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("正在播放!");
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);
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);
} else
Playlist->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()
{
qint64 position=player->position();
qDebug()<<"position"<<position;
position=position+5000;
ui->progressBar->setValue(position/1000);
player->setPosition(position);
}
void Video::on_TuiButton_clicked()
{
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开发的智能系统:电子相册,监控摄像头模块,音乐播放器,视频播放器,电子时钟 智能嵌入式系统介绍完,里面还有很多小的处理模块,就不一一赘述,感兴趣的小伙伴,可以移步本人开发的的完整项目源码:已共享
项目源码,需要自取 感谢感谢!!!!
|