项目功能
1.登录注册界面
2.动态去背景广告窗界面
2.1 去边框
2.2 可拖动
2.3 鼠标滚轮自由缩放
2.4 鼠标右键菜单
2.4.1 添加和删除广告资源
2.4.2 设置广告轮播时间
2.4.3 设置广告置顶
2.4.4 音视频播放器
2.4.5 画图工具
2.4.6 子菜单超链接 -> 网页
3.程序资源打包成.exe
代码实现
1、主登录界面,使用QSql 数据库模块,实现登录,注册,退出
/********************** widget.h *****************/
#ifndef WIDGET_H
#define WIDGET_H
#include "mainform.h"
#include <QWidget>
#include <QMovie>
#include <QGroupBox>
#include <QPalette>
#include <QEvent>
#include <QMouseEvent>
#include <QPoint>
#include <QDebug>
#include <Qstring>
#include <QMessageBox>
#include <QIcon>
/********** 数据库 *************/
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery> //QSqlQuery类提供了一种执行和操作SQL语句的方法。更多…
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
private:
bool m_Left_Press; //左鼠标点击实现移动
QPoint m_currentPos; //屏幕上点击的坐标点
QSqlDatabase m_db; //数据库句柄
public:
void mousePressEvent(QMouseEvent *event); //鼠标按下事件
void mouseMoveEvent(QMouseEvent *event); //鼠标移动事件
void sql_init(QSqlDatabase &m_db);
public slots:
void slot_pushButton_register_clicked(bool); //注册
void slot_pushButton_log_clicked(bool); //登录
void slot_pushButton_out_clicked(bool); //退出
};
#endif // WIDGET_H
/************************* widget.cpp *****************************/
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//去边框,背景透明化
this->setWindowFlags(Qt::FramelessWindowHint);//需要去掉标题栏
/*********** QWidget::setAttribute()
* 这个枚举类型用于指定各种小部件属性。
* 使用QWidget::setAttribute()设置和清除属性,
* 并使用QWidget. testattribute()查询属性,尽管有些属性具有下面提到的特殊的方便函数。
* ***************************************/
this->setAttribute(Qt::WA_TranslucentBackground); // Qt::WA_TranslucentBackground 120 QWidget背景透明化
//界面动图
QMovie * movie = new QMovie("://res/奔跑 奋斗.gif");
ui->label_moive->setMovie(movie);
//动图的播放才有效果
movie->start();
//注册
//数据库连接
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName("user.db"); //设置数据库名称
if(!m_db.open()) //打开数据库
{
qDebug() << "Error: Failed to connect m_db." << m_db.lastError();
}else
{
sql_init(m_db);
}
//信号与槽函数
connect(ui->pushButton_register,SIGNAL(clicked(bool)),this,SLOT(slot_pushButton_register_clicked(bool)));
connect(ui->pushButton_log,SIGNAL(clicked(bool)),this,SLOT(slot_pushButton_log_clicked(bool)));
connect(ui->pushButton_out,SIGNAL(clicked(bool)),this,SLOT(slot_pushButton_out_clicked(bool)));
}
void Widget::sql_init(QSqlDatabase &m_db)
{
QSqlQuery sql_query;
sql_query.exec("create table user (name Qstring primary key, pass Qstring)");
}
Widget::~Widget()
{
delete ui;
}
//鼠标按压事件
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_Left_Press = true;
m_currentPos = event->globalPos();
}
}
//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(m_Left_Press)
{
QPoint tempPos = event->globalPos() - m_currentPos;
this->move(pos() + tempPos);
m_currentPos = event->globalPos();
}
}
//注册
void Widget::slot_pushButton_register_clicked(bool)
{
if(!m_db.open()) //打开数据库
{
qDebug() << "Error: Failed to connect m_db." << m_db.lastError();
}else
{
sql_init(m_db);
}
//判断是否成功将账号密码插入数据库,并弹出对应提示窗
if(ui->lineEdit_name->text().isEmpty() || ui->lineEdit_pass->text().isEmpty())
{
QMessageBox MessageBox;
MessageBox.setWindowTitle("注册消息");
MessageBox.setText("账号不对,注册失败!");
MessageBox.setIconPixmap(QPixmap("://res/regiter.png"));;
MessageBox.exec();
}
else
{
QString name = ui->lineEdit_name->text();
QString pass = ui->lineEdit_pass->text();
QString insert_sql = "insert into user values (\"" + name + "\",\"" + pass +"\")";
//qDebug() << "insert_aql = " << insert_sql << endl;
QSqlQuery sql_query;
if(sql_query.exec(insert_sql))
{
QMessageBox MessageBox;
MessageBox.setWindowTitle("注册消息");
MessageBox.setText("恭喜注册成功!");
MessageBox.setIconPixmap(QPixmap("://res/regiter (1).png"));;
MessageBox.exec();
m_db.close();
}
else {
QMessageBox MessageBox;
MessageBox.setWindowTitle("注册消息");
MessageBox.setText("账号不对,注册失败!");
MessageBox.setIconPixmap(QPixmap("://res/regiter.png"));;
MessageBox.exec();
}
}
}
//登录
void Widget::slot_pushButton_log_clicked(bool)
{
if(!m_db.isOpen()) //打开数据库
{
m_db.open();
sql_init(m_db);
qDebug() << "Error: Failed to connect m_db." << m_db.lastError();
}else
{
sql_init(m_db);
}
//获取输入的内容
QString name = ui->lineEdit_name->text();
QString pass = ui->lineEdit_pass->text();
//保存查找结果
QString name_result;
QString pass_result;
//提示对话框
QMessageBox MessageBox;
MessageBox.setWindowTitle("登录消息");
MessageBox.setText("账号密码错误,登录失败!");
MessageBox.setIconPixmap(QPixmap("://res/regiter.png"));
//查找数据库信息
QSqlQuery sql_query;
QString sql_select = "select * from user where name = '%1' and pass = '%2'";
sql_select = sql_select.arg(name,pass);
if(sql_query.exec(sql_select)) //判断是否执行成功
{
while(sql_query.next()) //遍历数据库获取找到的结果
{
name_result = sql_query.value(0).toString();
pass_result = sql_query.value(1).toString();
}
if(name == name_result && pass == pass_result)
{
//进入到主界面,关闭当前界面
MainForm *myMainForm = new MainForm;
myMainForm->show();
this->close();
qDebug() << "登录成功" << endl;
}else {
MessageBox.exec(); //提示对话框 执行
}
}
else{
MessageBox.exec(); //提示对话框 执行
}
}
//关闭
void Widget::slot_pushButton_out_clicked(bool)
{
m_db.close();
this->close();
}
2、广告轮播界面,及鼠标右键菜单栏功能,代码中的图片,动态图等资源文件需要替换,自行添加。另外,音视频播放器是直接用qt的界面文件ui做的,就没有展示代码了。
/***************** mainform.h *************************/
#ifndef MainForm_H
#define MainForm_H
#include <QWidget>
#include "widget.h"
#include <QMenu>
#include <QSystemTrayIcon>
#include <QAction> //QAction类提供了一个抽象的用户界面操作,可以插入到小部件中。
#include <QTimer>
#include <QPixmap>
#include <QFileDialog>
#include <QFileInfo>
#include <QStringList>
#include <QInputDialog>
#include <QWheelEvent> //QWheelEvent类包含描述车轮事件的参数。
#include <QFile>
#include <QProcess>
//#include <QDesktopServices>
#include "aduiovideoform.h"
namespace Ui {
class MainForm;
}
class MainForm : public QWidget
{
Q_OBJECT
public:
explicit MainForm(QWidget *parent = 0);
~MainForm();
private:
Ui::MainForm *ui;
protected:
bool m_Left_Press; //左鼠标点击实现移动
QPoint m_currentPos; //屏幕上点击的坐标点
//相册
QTimer *timer; //定义时间变量用来设置定时器
QPixmap pix; //定义图片变量用来设置图片的属性
QMovie *movie;
QStringList File_Path; //文件名队列
QString *image_file; //图片文件名
int m_image_num; //图片总数量
int m_image_current_num; //图片当前数
int m_speed; //播放速度
public:
//菜单栏
QSystemTrayIcon *Hide_Menubar; //隐藏菜单栏
QMenu *m_Menu;
QAction *Action_add_advertising; //添加广告
QAction *Action_del_advertising; //删除广告
QAction *Action_set_image_width_height; //设置图片宽高
QAction *Action_set_image_play_speed; //设置图片播放速度
QAction *Action_istop; //置顶
QAction *Action_music_player; //音乐播放器
QAction *Action_Drawing_Tools; //画图类
QAction *Action_hyperlink; //超链接
QAction *Action_close; //关闭
public:
void mousePressEvent(QMouseEvent *event); //鼠标按下事件
void mouseMoveEvent(QMouseEvent *event); //鼠标移动事件
void wheelEvent(QWheelEvent *event);
private slots:
void slot_timer_timeout();
void slot_Action_add_advertising();
void slot_Action_del_advertising();
void slot_Action_set_image_width_height();
void slot_Action_set_image_play_speed();
void slot_slot_Action_istop();
void slot_slot_Action_music_player();
void slot_slot_Action_Drawing_Tools();
void slot_slot_Action_hyperlink();
void slot_slot_Action_close();
};
#endif // MainForm_H
/********************** mainform.cpp ************************/
#include "MainForm.h"
#include "ui_MainForm.h"
MainForm::MainForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainForm),m_image_current_num(0),m_image_num(3),m_speed(5)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);//需要去掉标题栏
this->setAttribute(Qt::WA_TranslucentBackground); // Qt::WA_TranslucentBackground 120 QWidget背景透明化
this->resize(400,400);
ui->label_advertising->setScaledContents(true); //此属性保存标签是否缩放其内容以填充所有可用空间。
ui->label_advertising->resize(this->size());
//制作相册
movie = new QMovie("://res/奔跑 奋斗.gif");
ui->label_advertising->setMovie(movie);
//动图的播放才有效果
movie->start();
image_file = new QString[100]; //文件名容器
timer = new QTimer(this);
timer->start(1000 * m_speed);
//将文件名放入到图片文件中
image_file[0] = (":/advertising/1.png");
image_file[1] = (":/advertising/2.gif");
image_file[2] = (":/advertising/3.gif");
//制作菜单栏
m_Menu = new QMenu(this);
Action_add_advertising = new QAction(QIcon("://res/tianjiatubiao.png"),"添加广告",this); //添加广告
m_Menu->addAction(Action_add_advertising);
connect(Action_add_advertising,SIGNAL(triggered(bool)),this,SLOT(slot_Action_add_advertising()));
Action_del_advertising = new QAction(QIcon("://res/shanchutupian.png"),"删除广告",this); //删除广告
m_Menu->addAction(Action_del_advertising);
connect(Action_del_advertising,SIGNAL(triggered(bool)),this,SLOT(slot_Action_del_advertising()));
Action_set_image_width_height = new QAction(QIcon("://res/kuangao.png"),"设置图片宽高",this); //设置图片宽高
m_Menu->addAction(Action_set_image_width_height);
connect(Action_set_image_width_height,SIGNAL(triggered(bool)),SLOT(slot_Action_set_image_width_height()));
Action_set_image_play_speed = new QAction(QIcon("://res/sudu.png"),"设置广告播放速度",this); //设置图片播放速度
m_Menu->addAction(Action_set_image_play_speed);
connect(Action_set_image_play_speed,SIGNAL(triggered(bool)),SLOT(slot_Action_set_image_play_speed()));
Action_istop = new QAction(QIcon("://res/yizhiding.png"),"置顶",this); //置顶
m_Menu->addAction(Action_istop);
connect(Action_istop,SIGNAL(triggered(bool)),SLOT(slot_slot_Action_istop()));
Action_music_player = new QAction(QIcon("://res/yinle.png"),"音乐播放器",this); //音乐播放器
m_Menu->addAction(Action_music_player);
connect(Action_music_player,SIGNAL(triggered(bool)),SLOT(slot_slot_Action_music_player()));
Action_Drawing_Tools = new QAction(QIcon("://images/110.JPG"),"画图工具",this); //画图工具
m_Menu->addAction(Action_Drawing_Tools);
connect(Action_Drawing_Tools,SIGNAL(triggered(bool)),SLOT(slot_slot_Action_Drawing_Tools()));
Action_hyperlink = new QAction(QIcon("://images/110.JPG"),"关于某某",this); //超链接
m_Menu->addAction(Action_hyperlink);
connect(Action_hyperlink,SIGNAL(triggered(bool)),SLOT(slot_slot_Action_hyperlink()));
Action_close = new QAction(QIcon("://res/关闭.png"),"关闭",this); //关闭
m_Menu->addAction(Action_close);
connect(Action_close,SIGNAL(triggered(bool)),SLOT(slot_slot_Action_close()));
//隐藏菜单栏设置
Hide_Menubar = new QSystemTrayIcon(this);
QIcon icon("://res/yinle.png");
Hide_Menubar->setIcon(icon);
Hide_Menubar->setContextMenu(m_Menu);
Hide_Menubar->show();
//轮放图片
connect(timer,SIGNAL(timeout()),this,SLOT(slot_timer_timeout()));
}
MainForm::~MainForm()
{
delete ui;
}
void MainForm::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) //鼠标左键移动
{
m_Left_Press = true;
m_currentPos = event->globalPos();
}
if(event->button() == Qt::RightButton) //鼠标右键打开菜单栏
{
m_Menu->exec(event->globalPos());
}
}
void MainForm::mouseMoveEvent(QMouseEvent *event)
{
if(m_Left_Press)
{
QPoint tempPos = event->globalPos() - m_currentPos;
this->move(pos() + tempPos);
m_currentPos = event->globalPos();
}
}
void MainForm::wheelEvent(QWheelEvent *event)
{
int temp = 0; //设置中间参数step用于将获取的数值转换成整数型
QPoint value = event->angleDelta(); //返回转轮旋转的距离,以八分之一度为单位。正值表示滚轮向前旋转,远离用户;负值表示滚轮向用户方向向后旋转。
if(!value.isNull()) //判断滚轮是否转动
{
temp = value.y();
}
event->accept(); // 获取事件
int currentWidth = this->width(); // 获取当前窗口的宽
int currentHeight = this->height(); // 获取当前窗口的高
currentWidth += temp; // 对当前窗口的宽累加
currentHeight += temp; // 对当前窗口的高累加
this->resize(currentWidth,currentHeight);
ui->label_advertising->setScaledContents(true); //此属性保存标签是否缩放其内容以填充所有可用空间。
ui->label_advertising->resize(this->size());
this->update();
}
void MainForm::slot_timer_timeout()
{
if(m_image_current_num >= m_image_num) //判断当前图片是否大于总图片数,进行循环播放
{
m_image_current_num = 0;
}
//图片总数等于0时,播放默认图片
if(m_image_num <= 0)
{
movie = new QMovie("://res/奔跑 奋斗.gif");
ui->label_advertising->setMovie(movie);
//动图的播放才有效果
movie->start();
}
else //播放列表图片
{
delete movie;
movie = new QMovie;
movie->setFileName(image_file[m_image_current_num]);
qDebug() << "image_file[m_image_current_num] = " << image_file[m_image_current_num] << endl;
ui->label_advertising->setMovie(movie);
movie->start();
m_image_current_num++;
}
}
void MainForm::slot_Action_add_advertising()
{
//打开文件对话框
QString file_name = QFileDialog::getOpenFileName(this,"选择文件","/","*.*");
if(file_name.isEmpty())
{
return ;
}
//将文件名加入到 image_file图片文件名 ,图片总数+1
image_file[m_image_num] = file_name;
m_image_num++;
}
void MainForm::slot_Action_del_advertising()
{
if(m_image_num == 0)
return ;
image_file[m_image_num].clear();
m_image_num--;
}
void MainForm::slot_Action_set_image_width_height()
{
bool ok;
int wight;
int heigh;
QInputDialog *InputDialog_wight = new QInputDialog;
QInputDialog* InputDialog_heigh = new QInputDialog;
wight = InputDialog_wight->getInt(this,"设置窗口宽度","请输入窗口宽度:(20-800)",200,20,800,10,&ok); //获取设置的宽高
heigh = InputDialog_heigh->getInt(this,"设置窗口高度","请输入窗口高度:(20-800)",150,20,800,10,&ok);
this->resize(wight,heigh);
ui->label_advertising->resize(this->size());
this->update();
}
void MainForm::slot_Action_set_image_play_speed()
{
int speed;
QInputDialog* InputDialog_speed = new QInputDialog;
speed = InputDialog_speed->getInt(this,"设置广告播放速度","请输入广告播放速度:(1-10)",5,1,10,1); //获取设置的速度
m_speed = speed;
timer->start(1000 * m_speed); //更新定时器
}
void MainForm::slot_slot_Action_istop()
{
QString text = Action_istop->text();
if(text == "取消置顶")
{
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
Action_istop->setText("置顶");
Action_istop->setIcon(QIcon("://res/yizhiding.png"));
this->show();
}
else if(text == "置顶")
{
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint |Qt::WindowStaysOnTopHint);
Action_istop->setText("取消置顶");
Action_istop->setIcon(QIcon("://res/quxiaozhiding.png"));
this->show();
}
}
//打开音视频播放器
void MainForm::slot_slot_Action_music_player()
{
AduiovideoForm *duiovideoForm = new AduiovideoForm;
duiovideoForm->show();
}
//打开windows自带的画图工具
void MainForm::slot_slot_Action_Drawing_Tools()
{
QProcess::execute("mspaint.exe"); //进程类的静态函数 在一个新进程中启动程序命令,等待它完成,然后返回退出代码。
}
//打开我的CSDN网页(超链接)
void MainForm::slot_slot_Action_hyperlink()
{
QLabel *openfilelabel = new QLabel;
openfilelabel->setOpenExternalLinks(true); //指定QLabel是否应该使用QDesktopServiceswopenUrl()自动打开链接,而不是发出linkActivated()信号。
//openfilelabel->openExternalLinks();
openfilelabel->setText(tr("<a href=\"https://blog.csdn.net/weixin_45574279?type=blog\">关于作者的CSDN"));
openfilelabel->show();
//QDesktopServices::openUrl(QUrl("<a href=\"https://blog.csdn.net/weixin_45574279?type=blog\>", QUrl::TolerantMode));
}
void MainForm::slot_slot_Action_close()
{
this->close();
}
效果视频演示
1、程序资源打包成 .exe ,注册及登录
注册及登录
2、动态去背景广告窗界面 及 右键菜单功能
移动_缩放_添加_删除_播放速度_高宽
3、其他功能
置顶_音视频播放_画图_超链接