Qt: QFileDialog opens the dialog, selects the picture, shows on the QLabel

Recently, there is such a requirement: click the button to open a dialog box to select an image and display it on the UI. The
getOpenFileName() function parameter fills in the file path, and returns the path of the selected file

#include<QFileDialog>
void on_pushButton_clicked()
{
    
     
  QString curPath=QDir::currentPath();//获取当前路径
  QString str=QFileDialog::getOpenFileName(this,"打开文件",curPath);
 // QString str=QFileDialog::getOpenFileName(this,"打开文件",".");//"." 代表程序运行目录
 // QString str=QFileDialog::getOpenFileName(this,"打开文件","/");//"/" 代表当前盘符的根目录
 qDebug()<<str;//E:\project\Camerasensor\AT_VI_V5.6.0.12_Win64\GoldenTool\1.bmp
  //show图片
  ui->label_PicHK->setPixmap(QPixmap(str));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324164805&siteId=291194637