[QT]实现图片滑动效果

效果展示:
[QT]实现图片滑动效果
相关代码
.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#define IMAGE_WIDTH 300
#define IMAGE_HEIGHT 200

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    QStringList getImageFilePath(const QString&);

private slots:
    void on_lineEdit_textChanged(const QString &arg1);

    void cellClickedEvent(int, int);

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

.c文件

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

#include <QScroller>
#include <QDir>
#include <QLabel>
#include <QPixmap>
#include <QDebug>
#include <QListWidget>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->tableWidget->verticalHeader()->hide();
    ui->tableWidget->horizontalHeader()->hide();
    ui->tableWidget->setRowCount(1);

    QSize size = QSize(IMAGE_WIDTH, IMAGE_HEIGHT);
    ui->tableWidget->setRowHeight(0, size.height());

    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->tableWidget->setHorizontalScrollMode(QListWidget::ScrollPerPixel);
    QScroller::grabGesture(ui->tableWidget,QScroller::LeftMouseButtonGesture);

    connect(ui->tableWidget, &QTableWidget::cellClicked, this, &MainWindow::cellClickedEvent);

    img_list = getImageFilePath("../img");
}

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


void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
    if(arg1.isEmpty()) return;

    int count = arg1.toInt();

    if(count > img_list.count()) count = img_list.count();

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(count);
    for (int i = 0; i < count; i++) {
        ui->tableWidget->setColumnWidth(i, IMAGE_WIDTH);

        QLabel *label = new QLabel;
        QImage image(img_list.at(i));
        label->setPixmap(QPixmap::fromImage(image.scaled(IMAGE_WIDTH, IMAGE_HEIGHT, Qt::KeepAspectRatioByExpanding)));
        ui->tableWidget->setCellWidget(0,i,label);
    }
}

void MainWindow::cellClickedEvent(int row, int col)
{
    Q_UNUSED(row)
    qDebug() << col;
}



QStringList MainWindow::getImageFilePath(const QString &dirpath)
{
    QDir dir(dirpath);
    QStringList files_path;
    files_path.clear();
    if(!dir.exists()) return files_path;
    QStringList nameFilters;
    nameFilters << "*.png" << "*.jpg";
    QStringList files_name = dir.entryList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);

    for (int i = 0; i < files_name.size(); i++) {
        files_path << dirpath + "/" + files_name.at(i);
    }

    return files_path;
}

ui布局
[QT]实现图片滑动效果

上一篇:2020-12-16


下一篇:WPF在image控件用鼠标拖拽出矩形