当前位置:   article > 正文

Qt(九)文件夹与文件(显示图像)_qt 文件夹图片

qt 文件夹图片

一、UI布局

在这里插入图片描述
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <QTreeWidgetItem>
#include <QTreeWidget>
#include <QPixmap>
#include <QLabel>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
private:
    enum treeItemType {itTopItem=1001,itGroupItem,itImageItem};
    enum treeColNum {colItem=0,colItemType=1};
    QPixmap pixmap;
    QLabel  labFileName;
    float pixRatio;

    void changeItemCaption(QTreeWidgetItem *item);
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_actOpenFolder_triggered();

    void on_actOpenFile_triggered();

    void on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);

    void on_actWidth_triggered();

    void on_actIter_triggered();

    void on_actDel_triggered();

    void on_actHeight_triggered();

    void on_actZoomIn_triggered();

    void on_actZoomOut_triggered();

    void on_actRealSize_triggered();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->statusbar->addWidget(&labFileName);
}

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


void MainWindow::on_actOpenFolder_triggered()
{
    QString fullname = QFileDialog::getExistingDirectory(this,"open Folder",".");
    if(fullname.isEmpty()) return;

    QString dir = fullname.right(fullname.length()-fullname.lastIndexOf('/')-1);

    if(!ui->treeWidget->currentIndex().isValid()) return;
    QTreeWidgetItem* parItem = ui->treeWidget->currentItem();
    QTreeWidgetItem* item = new QTreeWidgetItem(itGroupItem);

    QIcon icon(":/images/icon/open.ico");
    item->setIcon(treeColNum::colItem, icon);
    item->setText(treeColNum::colItem, dir);
    item->setText(treeColNum::colItemType, "type:group");
    item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsAutoTristate|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);
    item->setCheckState(colItem,Qt::Checked);
    parItem->addChild(item);

}

void MainWindow::on_actOpenFile_triggered()
{
    QStringList files = QFileDialog::getOpenFileNames(this,"file or files",".","Images(*.jpg *.png)");
    if (files.isEmpty()) return;

    QTreeWidgetItem* parItem;
    QTreeWidgetItem* item = ui->treeWidget->currentItem();

    if(item->type() == treeItemType::itImageItem)
        parItem = item->parent();
    else
        parItem = item;
    foreach(auto &file, files){
        item = new QTreeWidgetItem(treeItemType::itImageItem);
        QString name = file.right(file.length()-file.lastIndexOf('/')-1);
        QIcon icon(":/images/icon/picture.ico");
        item->setIcon(treeColNum::colItem, icon);
        item->setText(treeColNum::colItem, name);
        item->setText(treeColNum::colItemType, "type:Image");
        item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsAutoTristate|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable);
        item->setCheckState(colItem,Qt::Checked);
        item->setData(treeColNum::colItem,Qt::UserRole,file);
        parItem->addChild(item);
    }

}

void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    Q_UNUSED(previous);
    if(current == NULL) return;
    int var = current->type();
    switch (var) {
    case treeItemType::itTopItem:
        ui->actOpenFile->setEnabled(true);
        ui->actOpenFolder->setEnabled(true);
        ui->actDel->setEnabled(false);
        break;
    case treeItemType::itGroupItem:
        ui->actOpenFile->setEnabled(true);
        ui->actOpenFolder->setEnabled(true);
        ui->actDel->setEnabled(true);
        break;
    case treeItemType::itImageItem:
        ui->actOpenFile->setEnabled(true);
        ui->actOpenFolder->setEnabled(false);
        ui->actDel->setEnabled(true);
        labFileName.setText(current->data(colItem,Qt::UserRole).toString());
        pixmap.load(current->data(colItem,Qt::UserRole).toString());
        on_actWidth_triggered();
        ui->actWidth->setEnabled(true);
        ui->actHeight->setEnabled(true);
        ui->actZoomIn->setEnabled(true);
        ui->actZoomOut->setEnabled(true);
        ui->actRealSize->setEnabled(true);
        break;
    }
}

void MainWindow::on_actWidth_triggered()
{
    int W = ui->scrollArea->width();
    int realW = pixmap.width();
    pixRatio = (float)W/realW;
    QPixmap pix = pixmap.scaledToWidth(W-30);
    ui->label->setPixmap(pix);

}

void MainWindow::on_actIter_triggered()
{
    for (int i=0; i<ui->treeWidget->topLevelItemCount(); i++) {
        QTreeWidgetItem *item = ui->treeWidget->topLevelItem(i);
        changeItemCaption(item);
    }
}

void MainWindow::changeItemCaption(QTreeWidgetItem *item)
{
    QString str = "x_"+item->text(colItem);
    item->setText(colItem,str);
    for (int i=0; i<item->childCount(); i++) {
        changeItemCaption(item->child(i));
    }
}


void MainWindow::on_actDel_triggered()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if (!item->parent()) {
        ui->actDel->setEnabled(false);
        return;
    }
    QTreeWidgetItem* ParItem = item->parent();
    ParItem->removeChild(item);
    delete item;
}

void MainWindow::on_actHeight_triggered()
{
    int H = ui->scrollArea->height();
    int realH = pixmap.height();
    pixRatio = (float)H/realH;
    QPixmap pix = pixmap.scaledToHeight(H-30);
    ui->label->setPixmap(pix);
}

void MainWindow::on_actZoomIn_triggered()
{
    pixRatio *= 1.2;
    int w = pixRatio*pixmap.width();
    int h = pixRatio*pixmap.height();
    QPixmap pix = pixmap.scaled(w,h);
    ui->label->setPixmap(pix);

}

void MainWindow::on_actZoomOut_triggered()
{
    pixRatio *= 0.8;
    int w = pixRatio*pixmap.width();
    int h = pixRatio*pixmap.height();
    QPixmap pix = pixmap.scaled(w,h);
    ui->label->setPixmap(pix);
}

void MainWindow::on_actRealSize_triggered()
{
    ui->label->setPixmap(pixmap);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171

二、效果演示

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/306744
推荐阅读
相关标签
  

闽ICP备14008679号