QLabel 不使用 QToolButton 显示图像 [英] QLabel not showing images using QToolButton
问题描述
我有一个用于可视化图像(.tif、.tiff、.jpg 等)的用户界面的小示例:
I have a small minimal example of a user interface for visualizing images (both .tif, .tiff, .jpg etc) composed of:
1) N.1 QLabel(用于显示图片)
1) N.1 QLabel (used to show the image)
2) N.1 按钮(用于上传文件夹)
2) N.1 Pushbutton (used to upload a folder)
3) N.1 QLineEdit(用于可视化路径)
3) N.1 QLineEdit (used to visualize the path)
4) N.2 QToolbuttons(用作左右浏览图片)
4) N.2 QToolbuttons (used as left and right to look through images)
我正在尝试使用左右 QToolbuttons 查看图像,但有些地方不正确,我看不到任何图像.我正在查看 this source 作为示例以便开发我自己的实现并将其用于我正在开发的其他项目.
I am trying to look through images using the left and the right QToolbuttons but something is not correct and I am not able to see any image. I was looking at this source as an example in order to develop my own implementation and use it for other projects I am developing.
mainwindow.h
private slots:
void on_imageCroppedABtn_clicked();
void on_leftArrowCroppedA_clicked();
void on_rightArrowCroppedA_clicked();
private:
Ui::MainWindow *ui;
QString camADir;
QString fileCamA;
int croppedIndexA;
QStringList croppedFilenamesA;
QDir croppedA;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
croppedIndexA = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_imageCroppedABtn_clicked()
{
QString cdir = QFileDialog::getExistingDirectory(this, tr("Choose an image directory to load"),
fileCamA, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if((cdir.isEmpty()) || (cdir.isNull()))
return;
croppedA.setPath(cdir);
croppedFilenamesA = croppedA.entryList(QStringList() << "*.tiff" << "*.TIFF" << "*.tif" << "*.TIF", QDir::Files);
croppedIndexA = 0;
ui->lineEditfolderA->setText(croppedA.path());
}
void MainWindow::on_leftArrowCroppedA_clicked()
{
croppedIndexA--;
if(croppedIndexA < 0)
croppedIndexA = croppedFilenamesA.size()-1;
if(croppedFilenamesA.size() > 0)
{
ui->labelCroppedA->setScaledContents(true);
ui->labelCroppedA->setPixmap(QPixmap::fromImage(QImage(croppedFilenamesA[croppedIndexA])));
ui->labelCroppedA->show();
}
}
void MainWindow::on_rightArrowCroppedA_clicked()
{
croppedIndexA++;
if(croppedIndexA >= croppedFilenamesA.size())
croppedIndexA = 0;
if(croppedFilenamesA.size() > 0)
{
ui->labelCroppedA->setScaledContents(true);
ui->labelCroppedA->setPixmap(QPixmap::fromImage(QImage(croppedFilenamesA[croppedIndexA])));
ui->labelCroppedA->show();
}
}
我一直在尝试以多种不同的方式更改实现,但我总是无法看到图像.任何人都可以对这个问题有所了解吗?
I have been trying to change the implementation in many different ways but I always am not able to see images. Can anyone shed a little bit of light on this issue?
推荐答案
QImage
ctor 需要读取图像的完整路径.您可以将调用getExistingDirectory
的结果存储在数据成员cdir
中.当您调用 entryList
时,将列出传递目录中的所有文件.在创建 QImage
时,您需要将目录名称与来自该目录的文件名连接起来.所以你可以打电话:
QImage
ctor requires the full path to an image which is read. You can store a result of calling getExistingDirectory
in data member cdir
. When you call entryList
all files in the passed directory are listed. While creating QImage
you need to concatenate dir name with file name from this dir. So you can call:
ui->labelCroppedA->setPixmap(
QPixmap::fromImage(QImage(cdir + "/" + croppedFilenamesA[croppedIndexA])));
^ add directory separator
这篇关于QLabel 不使用 QToolButton 显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!