[英]Displaying picture that is not in resources of Qt
我目前正在Qt(基本上是倉庫)中編寫應用程序。 一個應用程序讀取CSV,使用戶能夠處理它並顯示每種商品的圖片。 我嘗試使用QLabel和Pixmap顯示圖片,但是即使文件位於同一文件夾中且提供的名稱也應正確,也沒有任何反應。 是資源問題還是我的代碼以某種方式失敗? 是否有可能在不將其添加到資源中的情況下顯示圖像,以避免手動添加很多照片?
void ImageViewer::viewImage(QString imgName)
{
QString pathWithName = imgName;
pathWithName.append(".jpg");
ui->label->setPixmap( QPixmap(pathWithName) );
ui->label->show();
update();
}
抱歉在創建帖子或在此處顯示代碼時出現任何錯誤-這是我的第一篇帖子。
編輯:我正在從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);
}
編輯2:解決。 必須使用QDir更改路徑:
QDir* directory = new QDir("/home/kokos/Magazyn/photos");
QFileInfo checkFile(*directory, pathWithName);
在此先感謝,Kokos
首先確認文件的位置和存在。 加上這個;
QFileInfo checkFile(pathWithName);
if (checkFile.exists() && checkFile.isFile()) {
// your code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.