118.Qt入门,实现一个图片查看软件

需要用到的控件

  • QLabel
  • QLineEdit
  • QPushButton

需要实现的功能

  • 打开目录选择图片
  • 显示图片的名字
  • 显示图片

QLabel基本用法

  • void setText(const QString &);//设置文本
  • QString text() const;//获取文本
  • void setPixmap(const QPixmap &);
  • 设置大小 使用父类QWidget的方法
  • 设置样式表(qss)setStyleSheet

QLineEdit基本用法

  • void settext(const QString &);//设置文本
  • QString text() const;//获取文本
  • 设置大小 使用父类QWidget的方法

QPushButton基本用法

  • void settext(const QString &);//设置文本
  • QString text() const;//获取文本
  • 设置图片 qss
  • 设置大小 使用父类QWidget的方法

 

代码

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    void open1();
    void open2();
    void open3();
    void open4();

private slots:
    void on_btnOpen_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QSettings>
#include <QDebug>
#include <QStandardPaths>
#include <memory>

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

    ui->label_image->clear();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::open1()
{
    QString filename=QFileDialog::getOpenFileName(this,"请选择图片","D:/","图片(*.png *.jpg);");
    if(filename.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(filename);
    ui->label_image->setPixmap((QPixmap(filename)));
}
void Widget::open2()
{
    QString config_path=qApp->applicationDirPath()+"/config/Setting.ini";
    qDebug()<<config_path;

    QSettings *pIniSet=new QSettings(config_path,QSettings::IniFormat);
    QString lastpath=pIniSet->value("/LastPath/path").toString();
    if(lastpath.isEmpty())
    {
        lastpath=QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString filename=QFileDialog::getOpenFileName(this,"请选择图片",lastpath,"图片(*.png *.jpg);");
    if(filename.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(filename);
    ui->label_image->setPixmap((QPixmap(filename)));

    int end=filename.lastIndexOf("/");
    QString _path=filename.left(end);
    pIniSet->setValue("/LastPath/path",_path);
    delete pIniSet;
    pIniSet=nullptr;
    qDebug()<<_path;
}

void Widget::open3()
{
    QString config_path=qApp->applicationDirPath()+"/config/Setting.ini";
    qDebug()<<config_path;

    std::unique_ptr<QSettings>pIniSet(new QSettings(config_path,QSettings::IniFormat));
    //QSettings *pIniSet=new QSettings(config_path,QSettings::IniFormat);
    QString lastpath=pIniSet->value("/LastPath/path").toString();
    if(lastpath.isEmpty())
    {
        lastpath=QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString filename=QFileDialog::getOpenFileName(this,"请选择图片",lastpath,"图片(*.png *.jpg);");
    if(filename.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(filename);
    ui->label_image->setPixmap((QPixmap(filename)));

    int end=filename.lastIndexOf("/");
    QString _path=filename.left(end);
    pIniSet->setValue("/LastPath/path",_path);
    //delete pIniSet;
    //pIniSet=nullptr;
    qDebug()<<_path;
}

void Widget::open4()
{
    QString config_path=qApp->applicationDirPath()+"/config/Setting.ini";
    qDebug()<<config_path;

    std::unique_ptr<QSettings>pIniSet(new QSettings(config_path,QSettings::IniFormat));
    //QSettings *pIniSet=new QSettings(config_path,QSettings::IniFormat);
    QString lastpath=pIniSet->value("/LastPath/path").toString();
    if(lastpath.isEmpty())
    {
        lastpath=QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
    }

    QString filename=QFileDialog::getOpenFileName(this,"请选择图片",lastpath,"图片(*.png *.jpg);");
    if(filename.isEmpty())
    {
        return;
    }

    ui->lineEdit_path->setText(filename);

    QPixmap *pix=new QPixmap(filename);
    *pix=pix->scaled(ui->label_image->size(),Qt::KeepAspectRatio);
    ui->label_image->setScaledContents(true);

    ui->label_image->setPixmap(*pix);

    delete pix;
    pix=nullptr;

    int end=filename.lastIndexOf("/");
    QString _path=filename.left(end);
    pIniSet->setValue("/LastPath/path",_path);
    //delete pIniSet;
    //pIniSet=nullptr;
    qDebug()<<_path;
}

void Widget::on_btnOpen_clicked()
{
    //open1();
    //open2();
    //open3();
    open4();
}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清酒。233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值