dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QList>
#include <QDebug>
#include <QPixmap>
#include <QString>
#include "form.h"
#include "ui_form.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
//建立一个QList对象
QList<QString> *Picture;
//遍历变量
int currpic;
//警告窗口
Form *warnWindow;
private slots:
void next_click();
void back_click();
void find();
};
#endif // DIALOG_H
- form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
private:
Ui::Form *ui;
};
#endif // FORM_H
- dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
currpic=0;
Picture=new QList<QString>;
ui->setupUi(this);
warnWindow=new Form;
//插入操作(图片像素插入前进行400×400的像素设置)
QString p1(":/new/prefix1/1.jpg");
QString p2(":/new/prefix1/2.jpeg");
QString p3(":/new/prefix1/3.jpeg");
QString p4(":/new/prefix1/4.jpeg");
//添加
Picture->append(p1);
Picture->push_back(p2);
Picture->push_front(p3);
Picture->append(p4);
//链接进行图片切换
connect(ui->back,SIGNAL(clicked()),this,SLOT(back_click()));
connect(ui->next,SIGNAL(clicked()),this,SLOT(next_click()));
//链接搜索框
connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(find()));
}
//切换下一张
void Dialog::next_click()
{
if(currpic>=Picture->count()-1)
{
currpic=0;
}
else
{
currpic++;
}
QString s=Picture->at(currpic);
QPixmap pic(s);
QSize size(ui->label->width(),ui->label->height());
pic=pic.scaled(size,Qt::KeepAspectRatioByExpanding);
ui->label->setPixmap(pic);
}
//切回上一张
void Dialog::back_click()
{
if(currpic==0)
{
currpic=Picture->count()-1;
}
else
{
currpic--;
}
QString s=Picture->at(currpic);
QPixmap pic(s);
QSize size(ui->label->width(),ui->label->height());
pic=pic.scaled(size,Qt::KeepAspectRatioByExpanding);
ui->label->setPixmap(pic);
}
void Dialog::find()
{
if(ui->lineEdit->text()=="迪迦"||ui->lineEdit->text()=="迪"||ui->lineEdit->text()=="迦")
{
warnWindow->close();
QString s=Picture->at(1);
QPixmap pic(s);
QSize size(ui->label->width(),ui->label->height());
pic=pic.scaled(size,Qt::KeepAspectRatioByExpanding);
ui->label->setPixmap(pic);
}
else if(ui->lineEdit->text()=="赛文"||ui->lineEdit->text()=="赛"||ui->lineEdit->text()=="文")
{
warnWindow->close();
QString s=Picture->at(2);
QPixmap pic(s);
QSize size(ui->label->width(),ui->label->height());
pic=pic.scaled(size,Qt::KeepAspectRatioByExpanding);
ui->label->setPixmap(pic);
}
else if(ui->lineEdit->text()=="泰罗"||ui->lineEdit->text()=="泰"||ui->lineEdit->text()=="罗")
{
warnWindow->close();
QString s=Picture->at(3);
QPixmap pic(s);
QSize size(ui->label->width(),ui->label->height());
pic=pic.scaled(size,Qt::KeepAspectRatioByExpanding);
ui->label->setPixmap(pic);
}
else if(ui->lineEdit->text()=="奥特之父"||ui->lineEdit->text()=="奥特"||ui->lineEdit->text()=="父")
{
warnWindow->close();
QString s=Picture->at(4);
QPixmap pic(s);
QSize size(ui->label->width(),ui->label->height());
pic=pic.scaled(size,Qt::KeepAspectRatioByExpanding);
ui->label->setPixmap(pic);
}
else
{
QPixmap pic(":/new/prefix2/6.jpeg");
QSize size(ui->label->width(),ui->label->height());
pic=pic.scaled(size,Qt::KeepAspectRatioByExpanding);
ui->label->setPixmap(pic);
warnWindow->show();
}
}
Dialog::~Dialog()
{
delete ui;
delete Picture;
}
- form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
- main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
- 界面文件
1、dialog.ui
2、form.ui
- 运行结果