Emanuele
Emanuele

Reputation: 2394

QLabel not showing images using QToolButton

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)

enter image description here

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?

Upvotes: 0

Views: 103

Answers (1)

rafix07
rafix07

Reputation: 20934

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

Upvotes: 2

Related Questions