参考:QFileDialog 的文档,完成对某个目录下的多个图片文件的选择。然后,结合使 用 QLabel/QImage/QPixmap 来显示图片的示例项目 viewer-demo,实现图片的依次浏览,此时按下 space/ backspace 键,或者用鼠标点击 next/previous 菜单项,可以切换显示下一张/上一张图片;当浏览到最后/第 一张图片时,程序给出相应的提示
时间: 2024-01-13 18:02:32 浏览: 166
好的,下面是一个基于 viewer-demo 的示例程序,实现对目录下多张图片的浏览和切换:
```cpp
#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QFileDialog>
#include <QDirIterator>
#include <QLabel>
#include <QPixmap>
#include <QKeyEvent>
class Viewer : public QMainWindow
{
public:
Viewer(QWidget *parent = nullptr);
void showImages(const QStringList &fileNames);
protected:
void keyPressEvent(QKeyEvent *event) override;
private:
QLabel *imageLabel;
QStringList imageFiles;
int currentIndex;
};
Viewer::Viewer(QWidget *parent)
: QMainWindow(parent)
{
imageLabel = new QLabel(this);
imageLabel->setAlignment(Qt::AlignCenter);
setCentralWidget(imageLabel);
QMenuBar *menuBar = new QMenuBar(this);
setMenuBar(menuBar);
QMenu *fileMenu = new QMenu(tr("File"), this);
menuBar->addMenu(fileMenu);
QAction *openAction = new QAction(tr("Open..."), this);
fileMenu->addAction(openAction);
connect(openAction, &QAction::triggered, [=]() {
QStringList filters;
filters << "*.png" << "*.jpg" << "*.jpeg" << "*.bmp";
QString dir = QFileDialog::getExistingDirectory(this, tr("Select Directory"), QDir::homePath());
if (!dir.isEmpty()) {
QDirIterator it(dir, filters, QDir::Files, QDirIterator::Subdirectories);
showImages(QStringList());
while (it.hasNext()) {
imageFiles << it.next();
}
if (!imageFiles.isEmpty()) {
currentIndex = 0;
showImages(QStringList() << imageFiles[currentIndex]);
}
}
});
QMenu *viewMenu = new QMenu(tr("View"), this);
menuBar->addMenu(viewMenu);
QAction *previousAction = new QAction(tr("Previous"), this);
viewMenu->addAction(previousAction);
connect(previousAction, &QAction::triggered, [=]() {
if (currentIndex > 0) {
currentIndex--;
showImages(QStringList() << imageFiles[currentIndex]);
} else {
QMessageBox::warning(this, tr("Viewer"), tr("This is the first image."));
}
});
QAction *nextAction = new QAction(tr("Next"), this);
viewMenu->addAction(nextAction);
connect(nextAction, &QAction::triggered, [=]() {
if (currentIndex < imageFiles.count() - 1) {
currentIndex++;
showImages(QStringList() << imageFiles[currentIndex]);
} else {
QMessageBox::warning(this, tr("Viewer"), tr("This is the last image."));
}
});
}
void Viewer::showImages(const QStringList &fileNames)
{
if (fileNames.isEmpty()) {
imageLabel->setText(tr("No image."));
return;
}
QPixmap pixmap(fileNames.first());
if (pixmap.isNull()) {
imageLabel->setText(tr("Invalid image."));
return;
}
imageLabel->setPixmap(pixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
setWindowTitle(fileNames.first());
}
void Viewer::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Space:
emit ui->actionNext->triggered();
break;
case Qt::Key_Backspace:
emit ui->actionPrevious->triggered();
break;
default:
QMainWindow::keyPressEvent(event);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Viewer w;
w.show();
return a.exec();
}
```
这个程序与 viewer-demo 相似,但加入了一个 openAction 菜单项,用于打开一个文件夹选择对话框,并显示其中的所有图片。浏览图片时,可以使用 previousAction 和 nextAction 菜单项、空格键和退格键进行切换。当浏览到第一张或最后一张图片时,程序会给出相应的提示。
阅读全文