首页 > 其他分享 >Qt作业3.0

Qt作业3.0

时间:2024-10-28 21:46:06浏览次数:6  
标签:Dialog Qt 作业 3.0 ui dialog new include id

目录

题目:

ui界面设计:

​编辑

代码:

dialog.h

dialog.cpp

main.cpp

源码:


题目:

设计一个电子相册,点击上一张,切换到上一张图片,点击下一张,切换到下一张图片。

要求:图片的展示可以循环,使用QList<QString>存储图片路径

ui界面设计:

代码:

dialog.h

//头文件dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPixmap>
#include <QSize>
#include<QList>
#include <QDebug>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    Ui::Dialog *ui;
    QList<QString> path;

private slots:
    void btnClickSlot( );
    void btnClickSlot1( );

};

#endif // DIALOG_H

dialog.cpp

//dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"


static int id=0;


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);


    QString s1(":/new/prefix1/111.jpg");
    QString s2(":/new/prefix1/222.jpg");
    QString s3(":/new/prefix1/333.jpg");
    QString s4(":/new/prefix1/444.jpg");

    path.prepend(s1);
    path.append(s2);
    path.append(s3);
    path.append(s4);

    connect(ui->pushButton,SIGNAL(clicked()),
            this,SLOT(btnClickSlot()));
    connect(ui->pushButton_2,SIGNAL(clicked()),
            this,SLOT(btnClickSlot1()));
}

Dialog::~Dialog()
{
    delete ui;
}
void Dialog::btnClickSlot()
{



    if(id==3)
    {
        id=0;
    }

    qDebug()<<"下一张";
    QPixmap pic(path.at(id));
    ui->label->setPixmap(pic);id++;
}
void Dialog::btnClickSlot1()
{
    id--;

    if (id < 0)
    {
        id = 3;
    }

    qDebug() << "上一张";
    QPixmap pic(path.at(id));
    ui->label->setPixmap(pic);
}

main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

源码:

已绑定资源置顶

标签:Dialog,Qt,作业,3.0,ui,dialog,new,include,id
From: https://blog.csdn.net/QR70892/article/details/143276921

相关文章