设计电子相册,实现图片切换(可循环),添加搜索框,输入关键字,显示出已有图片。

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
    在这里插入图片描述
  • 运行结果
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值