简体   繁体   English

显示不在Qt资源中的图片

[英]Displaying picture that is not in resources of Qt

I am currently writing an application in Qt, which is basically a warehouse. 我目前正在Qt(基本上是仓库)中编写应用程序。 An application reads CSV, enables user to process it and enables to show picture of each good. 一个应用程序读取CSV,使用户能够处理它并显示每种商品的图片。 I tried displaying picture using QLabel and Pixmap, however nothing happens even though the file is in the same folder and the name provided is exactly as it should be. 我尝试使用QLabel和Pixmap显示图片,但是即使文件位于同一文件夹中且提供的名称也应正确,也没有任何反应。 Is it the resources issue or my code fails somehow? 是资源问题还是我的代码以某种方式失败? Is there any possibility to display the image without adding it to resources in order to avoid adding many photos manually? 是否有可能在不将其添加到资源中的情况下显示图像,以避免手动添加很多照片?

void ImageViewer::viewImage(QString imgName)
{
    QString pathWithName = imgName;
    pathWithName.append(".jpg");

    ui->label->setPixmap( QPixmap(pathWithName) );
    ui->label->show();
    update();
}

Sorry for any mistakes in post creation or code displaying here- it's my first post. 抱歉在创建帖子或在此处显示代码时出现任何错误-这是我的第一篇帖子。

Edit: I am adding code from MainWindow (called CsvReader in my project) to how I'm invoking the method viewImage: 编辑:我正在从MainWindow(在我的项目中称为CsvReader)中添加代码以调用方法viewImage:

void CsvReader::on_imgView_clicked()
{
    ImageViewer* img = new ImageViewer(this);
    img->setModal(true);
    img->exec();
QModelIndex List selInd ui->tableView->selectionModel()->selectedIndexes();
QString id = model->item(selInd.first().row(), 0)->text();

img->viewImage(id);
}

Edit 2: Solved. 编辑2:解决。 Had to change path using QDir: 必须使用QDir更改路径:

QDir* directory = new QDir("/home/kokos/Magazyn/photos");
QFileInfo checkFile(*directory, pathWithName);

Thanks in advance, Kokos 在此先感谢,Kokos

Confirm your file's location and existence first. 首先确认文件的位置和存在。 Add this; 加上这个;

    QFileInfo checkFile(pathWithName);

    if (checkFile.exists() && checkFile.isFile()) {
        // your code
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM