QLabel not showing images using QToolButton

时间秒杀一切 提交于 2019-12-11 20:32:07

问题


I have a small minimal example of a user interface for visualizing images (both .tif, .tiff, .jpg etc) composed of:

1) N.1 QLabel (used to show the image)

2) N.1 Pushbutton (used to upload a folder)

3) N.1 QLineEdit (used to visualize the path)

4) N.2 QToolbuttons (used as left and right to look through images)

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?


回答1:


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


来源:https://stackoverflow.com/questions/53600982/qlabel-not-showing-images-using-qtoolbutton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!