widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
-
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
-
- private slots:
-
- void on_pb_2_clicked();
-
- void on_pb_left_clicked();
-
- void on_pb_3_clicked();
-
- private:
- Ui::Widget *ui;
- static int i; //索引
- QStringList picfiles; //存储路径
- };
-
- #endif // WIDGET_H
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
- #include
//文件对话框 -
- int Widget::i = 0; //初始化i为0
-
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- setWindowTitle("图片浏览器");
- setFixedSize(530,500);
-
- ui->setupUi(this); //ui操作在此下面写
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::on_pb_2_clicked()
- {
- picfiles = QFileDialog::getOpenFileNames(this,"打开图片","..","图片文件(*.png)"); //父类、窗口标题、上一级路径打开、文件类型
- ui->te->setText(picfiles[i]);
- ui->lb_2->setScaledContents(true); //设置标签自适应大小
- ui->lb_2->setPixmap(QPixmap(picfiles.at(i))); //设置标签中装入索引为i的图片
- }
-
- void Widget::on_pb_left_clicked()
- {
- if(i != 0)
- {
- ui->te->setText(picfiles[--i]);
- ui->lb_2->setPixmap(QPixmap(picfiles.at(i)));
- }
- else
- {
- ui->te->setText(picfiles[picfiles.size()-1]);
- ui->lb_2->setPixmap(QPixmap(picfiles.at(picfiles.size()-1)));
- i = picfiles.size()-1;
- }
- }
-
- void Widget::on_pb_3_clicked()
- {
- if(i != picfiles.size()-1)
- {
- ui->te->setText(picfiles[++i]);
- ui->lb_2->setPixmap(QPixmap(picfiles.at(i)));
- }
- else
- {
- ui->te->setText(picfiles[0]);
- ui->lb_2->setPixmap(QPixmap(picfiles.at(0)));
- i = 0;
- }
- }
main.cpp
- #include "widget.h"
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
-
- return a.exec();
- }
widget.ui
结果