项目需求分析
图片查看器,要有查找图片文件,以及打开图片文件的功能,以及可以通过滚轮的改变图片显示的大小。
但是我写的这个就只有通过文件对话框查找图片的功能,以及打开图片文件的功能,并且当我们重新打开新的文件夹的时候,并且的图片会被关闭,如果我们没有选择任何文件,就保持上一次的图片显示。同时项目并没有用ui界面来布局界面的整体布局。
项目效果图
项目布局设计图
所以明面上要用到三个pushbutton按键,一个lable标签,用lable标签显示图片。
项目用到的头文件
#include <QWidget>
#include <QPushButton>//按钮
#include <QLabel>//标签
#include <QHBoxLayout>//水平布局
#include <QVBoxLayout>//垂直布局
#include <QFileDialog>//文件对话框
#include <QDebug>
信号与槽机制
信号与槽机制是Qt的重要机制之一,简单的来说就是某一个控件的某一个信号与一个槽函数绑定,当这个控件的这个信号被触发了,那么这个槽函数就会被调用
(1)信号和槽是用于QT对象之间的通信,信号可以有某种动作触发,也可以直接由代码触发。
(2)槽也叫操函数,当完成了信号和槽的连接之后,一旦触发信号,自动调用连接的槽函数。
(3)信号和槽的连接是动态的,对象释放后会自动断开所有的信号和槽。
(4)代码触发信号的写法
Widget.h里面的内容
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>//按钮
#include <QLabel>//标签
#include <QHBoxLayout>//水平布局
#include <QVBoxLayout>//垂直布局
#include <QFileDialog>//文件对话框
#include <QDebug>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void get_File_names();//获取图片名字
void on_pix_show();//上一张图片显示
void down_pix_show();//下一张图片显示
private:
QPushButton *choose_dir,*on_pix,*down_pix;//定义三个QPushButton的变量
QLabel *show_pix;//定义一个QLable标签变量
int file_location;//定义一个int变量,用来记录图片文件的索引
QStringList file_list;//用来存放图片文件的路径和名字
};
#endif // WIDGET_H
Widget.cpp里面内容
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//设置字体
QFont serifFont("Times", 20, QFont::Bold);
this->setFont(serifFont);
//初始化控件
choose_dir=new QPushButton("选择查看图片的目录");
on_pix=new QPushButton("上一张");
down_pix=new QPushButton("下一张");
show_pix=new QLabel(" ");
//设置标题
this->setWindowTitle("图片查看器");
//设置大小
this->setFixedSize(800,700);
show_pix->setFixedSize(800,600);
//show_pix->setFixedSize(this->size());
//让label标签居中和图片自适应
show_pix->setAlignment(Qt::AlignCenter);
show_pix->setScaledContents(true);
//设置按键透明
choose_dir->setFlat(true);
on_pix->setFlat(true);
down_pix->setFlat(true);
//布局
QHBoxLayout *hbox=new QHBoxLayout;
hbox->addWidget(on_pix);
hbox->addStretch();
hbox->addWidget(choose_dir);
hbox->addStretch();
hbox->addWidget(down_pix);
QVBoxLayout *vbox=new QVBoxLayout;
//vbox->addWidget(choose_dir);
//vbox->addStretch();
vbox->addWidget(show_pix);
vbox->addLayout(hbox);
this->setLayout(vbox);
//链接信号
file_location=0;
connect(choose_dir,&QPushButton::clicked,this,&Widget::get_File_names);
connect(on_pix,&QPushButton::clicked,this,&Widget::on_pix_show);
connect(down_pix,&QPushButton::clicked,this,&Widget::down_pix_show);
}
Widget::~Widget()
{
}
void Widget::get_File_names()
{
//通过QFileDialog::getOpenFileNames获取选中的图片文件的名字
QStringList list=QFileDialog::getOpenFileNames(this,"打开图片",".","Images (*.jpg *.png *.bmp)");
if(list.isEmpty())
return;
else
{
file_list.clear();
file_list=list;
file_location=0;
}
show_pix->setPixmap(QPixmap(file_list.at(file_location)));
}
void Widget::on_pix_show()
{
if(file_location == 0)
file_location=file_list.length()-1;
else
file_location--;
show_pix->setPixmap(QPixmap(file_list.at(file_location)));
}
void Widget::down_pix_show()
{
if(file_location == file_list.length()-1)
file_location=0;
else
file_location++;
show_pix->setPixmap(QPixmap(file_list.at(file_location)));
}