在这里插入图片描述
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//创建界面
init_ui();
//参数初始化
init_args();
connect(bt_up, SIGNAL(clicked(bool)), this, SLOT(show_up()));
connect(bt_down, SIGNAL(clicked(bool)), this, SLOT(show_down()));
connect(bt_roll, SIGNAL(clicked(bool)), this, SLOT(show_roll()));
connect(t, SIGNAL(timeout()), this, SLOT(show_down()));
connect(bt_getpicnames, &QPushButton::clicked, [&](){
QStringList namex = QFileDialog::getOpenFileNames(this, "打开图片", ".", "Images (*.png *.xpm *.jpg)");
names += namex;
currentIndex = 0;
});
}
//显示上一页
void Widget::show_up()
{
currentIndex--;
if(-1 == currentIndex)
currentIndex = names.length()-1;
lb_pix->setPixmap(QPixmap(names[currentIndex]));
}
//显示下一页
void Widget::show_down()
{
currentIndex++;
if(names.length() == currentIndex)
currentIndex = 0;
lb_pix->setPixmap(QPixmap(names[currentIndex]));
}
//滚动显示
void Widget::show_roll()
{
static bool rollf = true;
if(rollf){
t->start(100);
bt_roll->setText("停止");
}
else{
t->stop();
bt_roll->setText("开始");
}
rollf = !rollf;
}
void Widget::init_ui()
{
lb_pix = new QLabel;
lb_pix->setMinimumSize(640, 480);
lb_pix->setScaledContents(true);
bt_up = new QPushButton("上一张");
bt_down = new QPushButton("下一张");
bt_roll = new QPushButton("开始");
bt_getpicnames = new QPushButton("选择图片");
QGridLayout *gbox = new QGridLayout;
gbox->addWidget(lb_pix, 0, 0, 1, 5);
gbox->addWidget(bt_getpicnames, 1, 0, 1, 1);
gbox->addWidget(bt_up, 1, 1, 1, 1);
gbox->addWidget(bt_roll, 1, 2, 1, 1);
gbox->addWidget(bt_down, 1, 3, 1, 1);
setLayout(gbox);
}
void Widget::init_args()
{
currentIndex = 0;
names.clear();
names<<":/image/Luffy.png"<<"2.jpg"<<"3.jpg";
t = new QTimer;
}
Widget::~Widget()
{
}