qt打开文件对话框

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMenu>
#include<QToolBar>
#include<QFileDialog>
#include<QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
  
  
    //ui->setupUi(this);
 
 
    openAction =new QAction(tr("&打开"),this);
    openAction->setShortcut(QKeySequence::Open);
    openAction->setStatusTip(tr("Open a file."));
    openAction->setIcon(QIcon(":/a.png"));
 
 
    QMenu *menu= menuBar()->addMenu(tr("&文件"));
    menu->addAction(openAction);
 
 
    QToolBar *tool = addToolBar(tr("&文件"));
    tool->addAction(openAction);
 
 
    lable = new QLabel;
    statusBar()->addAction(openAction);
 
 
    connect(openAction,SIGNAL(triggered()),this,SLOT(open()));
}
//打开文件函数
void MainWindow::open()
{
  
  
    QString path = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files(*.jpg *.png)"));
    if(path.length() == 0) {
  
  
            QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
    } else {
  
  
            QMessageBox::information(NULL, tr("Path"), tr("You selected ") + path);
    }
}
MainWindow::~MainWindow()
{
  
  
    delete ui;
}
 

猜你喜欢

转载自blog.csdn.net/wyyy2088511/article/details/127197446