用Qt设计一个图片浏览器

本人分享的是不用ui界面的图片浏览器,它可以从系统文件里面选取各种格式的图片(可以自己设置),然后点击左右的按钮来浏览这些图片,而且还支持图片多选的功能。

以下是Widget.h中的代码:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
public slots:
    void openfiles();
    void lshow();
    void rshow();

private:
    QLabel *lab;
    QLineEdit *edt;
    QPushButton *btn, *lbtn, *rbtn;

    QStringList file;
    int current_index;
};

#endif // WIDGET_H

 

Widget.cpp中的代码:

#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
        lab = new QLabel(this);
        lab->setFixedSize(640, 480);
        lab->setScaledContents(true);//设置图片的自动缩放
        lbtn = new  QPushButton(QIcon("lpic.png"),"上一张", this);
        rbtn = new  QPushButton(QIcon("rpic.jpg"),"下一张", this);
        btn  = new  QPushButton(QIcon("openfile.png"), "查找",this);

        QHBoxLayout *hbox = new QHBoxLayout;
        hbox->addStretch();
        hbox->addWidget(lbtn);
        hbox->addWidget(btn);
        hbox->addWidget(rbtn);
        hbox->addStretch();

        QVBoxLayout * vbox = new QVBoxLayout;
//        lab->installEventFilter(this);
        vbox->addWidget(lab);
        vbox->addLayout(hbox);
        setLayout(vbox);

        file.clear();

        current_index = 0;

        connect(btn, SIGNAL(clicked()), this, SLOT(openfiles()));
        connect(rbtn, SIGNAL(clicked()), this, SLOT(rshow()));
        connect(lbtn, SIGNAL(clicked()), this, SLOT(lshow()));
}

void Widget::openfiles()
{
    file = QFileDialog::getOpenFileNames(this, "select pic", ".", "Images (*.png *.xpm *.jpg)");
    lab->setPixmap(QPixmap(file[0]));
}
void Widget::lshow()
{
    if (current_index == 0)
        current_index = file.length()-1;
    else
        current_index--;
    lab->setPixmap(QPixmap(file[current_index]));

}
void Widget::rshow()
{
    if (current_index == file.length()-1)
        current_index = 0;
    else
        current_index++;
    lab->setPixmap(QPixmap(file[current_index]));
}

Widget::~Widget()
{

}

 

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值