目录
一:核心源码 & 效果展示
引入头文件
#include<QMessageBox>//弹窗
information消息对话框的使用
if(btnName == "电影")
{
QMessageBox::information(NULL,"电影",btn->text());
}
critical消息对话框的使用
用于给用户提示“操作错误”或“运行失败”的信息
if(btnName == "综艺")
{
QMessageBox::critical(NULL,"综艺",btn->text());
}
question消息对话框的使用
常用于向用户提出问题并接收用户的答案
if(btnName == "儿童")
{
QMessageBox::question(NULL,"儿童",btn->text());
}
warning消息对话框的使用
常用于向用户显示一些警告信息
if(btnName == "动漫")
{
QMessageBox::warning(NULL,"动漫",btn->text());
}
about消息对话框的使用
作为介绍某个产品或某项功能的临时窗口
if(btnName == "游戏")
{
QMessageBox::about(NULL,"游戏",btn->text());
}
二:布局管理器 设计
indexwin.h .cpp【布局设计的学习】
#ifndef INDEXWIN_H
#define INDEXWIN_H
#include <QWidget>
#include<QVBoxLayout>//垂直布局
#include<QHBoxLayout>//水平布局
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>
#include<QListWidget>//制作视频页面列表
class IndexWin : public QWidget
{
Q_OBJECT
public:
explicit IndexWin(QWidget *parent = 0);
QWidget *leftwin; //左边窗口
QWidget *topwin; //顶部窗口
QWidget *indexwin;
QWidget *rightwin; //右边窗口
QListWidget *videowinlist;//制作视频页面列表
QLineEdit *searchEdit; //搜索编辑框
QPushButton *userBtn; //用户按钮
QPushButton *vipBtn; //会员按钮
QPushButton *recordBtn; //观看记录按钮
QPushButton *downloadBtn; //下载视频记录按钮
QPushButton *likeBtn; //喜欢按钮
QPushButton *shrinkBtn; //缩小按钮
QPushButton *enlargeBtn; //放大按钮
QPushButton *closeBtn; //关闭按钮
QPushButton *searchBtn; //搜索按钮
QLabel *logo1Lab; //视频logo
QPushButton *return1Btn; //返回登录界面按钮
signals:
public slots:
void returnLoginSlot();
void goNewwinSlot(); //点击不同的按钮做出不同的响应
};
#endif // INDEXWIN_H
#include "indexwin.h"
#include<QStringList>//一次添加多个控件
#include<QDebug>
#include<QMessageBox>//弹窗
#include<QListWidget>//多个按钮显示,存放窗口的容器
#include<QDir>//路径
#include<QPixmap>//放置图片
#include<QListView>//设置图片模式
#include "widget.h"
IndexWin::IndexWin(QWidget *parent) : QWidget(parent)
{
this->resize(1500,800);
setWindowFlags(Qt::FramelessWindowHint);//无边框
setAttribute(Qt::WA_TranslucentBackground);//背景透明
//自己设置背景图片
// QPixmap pixmap(":/image/0508005.jpg");
// QPalette palette;
// palette.setBrush(backgroundRole(), QBrush(pixmap));
// setPalette(palette);
leftwin = new QWidget();
topwin = new QWidget();
//indexwin = new QWidget();
rightwin = new QWidget();
videowinlist = new QListWidget();//视频列表
//设置背景颜色
leftwin->setStyleSheet("background-color: rgb(252,157,154)");
topwin->setStyleSheet("background-color: rgb(252,157,154)");
videowinlist->setStyleSheet("background-color: rgb(252,157,154)");
//设置图片模式--上面图片下面名称
videowinlist->setViewMode(QListView::IconMode);
//获取视频图片路径
QString videopath = QDir::currentPath()+"/image";
qDebug()<<videopath;
QDir dir(videopath);//查找目录
QStringList moviename;
moviename << "*.png";//查找.png图片
QStringList files = dir.entryList(moviename,QDir::Files|QDir::Readable,QDir::Name);
qDebug()<<files; //查找.png图片名
for(int i=0;i<files.size();i++)
{
//显示.png图片及名称
QListWidgetItem *newitem = new QListWidgetItem(
QIcon(QPixmap(videopath+"/"+files.at(i))),files.at(i));
//设置图片尺寸
newitem->setSizeHint(QSize(200,250));
videowinlist->addItem(newitem);
}
//控件布局
//方法一:自定义所有控件
//控件水平布局 顶部窗口放置水平布局
QHBoxLayout *hboxlayout = new QHBoxLayout(topwin);
logo1Lab = new QLabel();
searchEdit = new QLineEdit();
searchBtn = new QPushButton();
return1Btn = new QPushButton();
userBtn = new QPushButton();
vipBtn = new QPushButton();
recordBtn = new QPushButton();
downloadBtn = new QPushButton();
likeBtn = new QPushButton();
shrinkBtn = new QPushButton();
enlargeBtn = new QPushButton();
closeBtn = new QPushButton();
logo1Lab->setFixedSize(170,110);
logo1Lab->setPixmap(QPixmap(":/image/0510.png").scaled(logo1Lab->size()));
//编辑框的大小位置设置
searchEdit->setFixedSize(600,35);
//按钮控件大小设置
searchBtn->setFixedSize(40,40);
searchBtn->setIcon(QIcon(QPixmap(":/image/050909.png")));
searchBtn->setIconSize(QSize(40,40));
return1Btn->setFixedSize(40,40);
return1Btn->setIcon(QIcon(QPixmap(":/image/051001.png")));
return1Btn->setIconSize(QSize(40,40));
userBtn->setFixedSize(40,40);
userBtn->setIcon(QIcon(QPixmap(":/image/050901.png")));
userBtn->setIconSize(QSize(40,40));
vipBtn->setFixedSize(40,40);
vipBtn->setIcon(QIcon(QPixmap(":/image/050902.png")));
vipBtn->setIconSize(QSize(40,40));
recordBtn->setFixedSize(40,40);
recordBtn->setIcon(QIcon(QPixmap(":/image/050903.png")));
recordBtn->setIconSize(QSize(40,40));
downloadBtn->setFixedSize(40,40);
downloadBtn->setIcon(QIcon(QPixmap(":/image/050904.png")));
downloadBtn->setIconSize(QSize(40,40));
likeBtn->setFixedSize(40,40);
likeBtn->setIcon(QIcon(QPixmap(":/image/050905.png")));
likeBtn->setIconSize(QSize(40,40));
shrinkBtn->setFixedSize(40,40);
shrinkBtn->setIcon(QIcon(QPixmap(":/image/050906.png")));
shrinkBtn->setIconSize(QSize(40,40));
enlargeBtn->setFixedSize(40,40);
enlargeBtn->setIcon(QIcon(QPixmap(":/image/050907.png")));
enlargeBtn->setIconSize(QSize(40,40));
closeBtn->setFixedSize(40,40);
closeBtn->setIcon(QIcon(QPixmap(":/image/050908.png")));
closeBtn->setIconSize(QSize(40,40));
//加空白 - 将控件右置
hboxlayout->addStretch();
hboxlayout->addWidget(logo1Lab);
hboxlayout->addWidget(searchEdit);
hboxlayout->addWidget(searchBtn);
hboxlayout->addWidget(return1Btn);
hboxlayout->addWidget(userBtn);
hboxlayout->addWidget(vipBtn);
hboxlayout->addWidget(recordBtn);
hboxlayout->addWidget(downloadBtn);
hboxlayout->addWidget(likeBtn);
hboxlayout->addWidget(shrinkBtn);
hboxlayout->addWidget(enlargeBtn);
hboxlayout->addWidget(closeBtn);
//方法二:动态创建所有控件
//按钮控件垂直 左边窗口放置一个垂直布局
QVBoxLayout *vboxlayout1 = new QVBoxLayout(leftwin);
QStringList funBtnlist;
funBtnlist<<"推荐"<<"电视剧"<<"电影"<<"综艺"<<"儿童"<<"动漫"<<"游戏"<<"纪录片"<<"体育";
for(int i=0;i<funBtnlist.size();i++)
{
QPushButton *newBtn = new QPushButton(funBtnlist.at(i));
//多个按钮绑定同一个槽 点击不同的按钮做出不同的响应
connect(newBtn,SIGNAL(clicked(bool)),this,SLOT(goNewwinSlot()));
//添加控件
vboxlayout1->addWidget(newBtn);
}
//加空白 -- 将按钮上置
//vboxlayout1->addStretch();
//窗口布局
//右半部分的布局放置顶部窗口和(视频列表)主窗口 两个窗口垂直
QVBoxLayout *rightboxlayout = new QVBoxLayout(rightwin);
rightboxlayout->addWidget(topwin);
//rightboxlayout->addWidget(indexwin);
rightboxlayout->addWidget(videowinlist);
//左边窗口和前面两个窗口垂直的部分 进行水平的放置
//this指代最大布局
QHBoxLayout *mainlayout = new QHBoxLayout(this);
mainlayout->addWidget(leftwin); //放置窗口
//mainlayout->addLayout(rightboxlayout);//放置布局
mainlayout->addWidget(rightwin);
connect(return1Btn,SIGNAL(clicked(bool)),this,SLOT(returnLoginSlot()));
connect(closeBtn,SIGNAL(clicked(bool)),this,SLOT(close()));
}
void IndexWin::returnLoginSlot()
{
Widget *re1 = new Widget;
re1->show();
this->hide();
}
void IndexWin::goNewwinSlot()
{
//获取点击按钮的文本信息 - sender -对QPushButton对象进行操作
QPushButton *btn = (QPushButton *)sender();
//按钮对象文本信息的获取和保存
QString btnName = btn->text();
//qDebug()<<btn->text();
//根据获取到的文本信息进行判断 - 根据文本信息进行区分不同的按钮 - 不同的按钮做出不同的响应
//更新列表 分文件管理
if(btnName == "推荐")
{
// 弹窗
QMessageBox::information(NULL,"推荐",btn->text());
//更新推荐列表
}
if(btnName == "电视剧")
{
QMessageBox::information(NULL,"电视剧",btn->text());
}
if(btnName == "电影")
{
QMessageBox::information(NULL,"电影",btn->text());
}
if(btnName == "综艺")
{
QMessageBox::critical(NULL,"综艺",btn->text());
}
if(btnName == "儿童")
{
QMessageBox::question(NULL,"儿童",btn->text());
}
if(btnName == "动漫")
{
QMessageBox::warning(NULL,"动漫",btn->text());
}
if(btnName == "游戏")
{
QMessageBox::about(NULL,"游戏",btn->text());
}
if(btnName == "纪录片")
{
QMessageBox::information(NULL,"纪录片",btn->text());
}
if(btnName == "体育")
{
QMessageBox::information(NULL,"体育",btn->text());
}
}
main.cpp【程序主入口】
#include "widget.h"
#include <QApplication>
#include"indexwin.h"
#include<QTransform>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
IndexWin w;
w.show();
return a.exec();
}