Дополненная и исправленная версия:
#pragma once
#include <QtGui/QLabel>
class QFilePathEdit :public QLabel
{
Q_OBJECT
private:
class QLineEdit* m_edit;
QString m_path;
QString m_dlg_Caption;
QString m_dlg_Filter;
bool m_DlgType;
static const bool DirDlgType=0;
static const bool FileDlgType=1;
void construct(QWidget* parent);
public:
QFilePathEdit(QWidget* parent=0,const QString &Caption=tr("Open Directory"));
QFilePathEdit(const QString &Filter,QWidget* parent=0,const QString &Caption=tr("Open File"));
~QFilePathEdit(void);
bool getPath(QString &Path);
bool setPath(const QString &Path);
protected slots:
void showFileDlg();
void setPathFromLineEdit();
signals:
void finished(QWidget* This);
};
-----------cpp-------
#include "StdAfx.h"
#include "QFilePathEdit.h"
QFilePathEdit::QFilePathEdit(QWidget* parent,const QString &Caption):QLabel(parent)
{
m_DlgType=DirDlgType;
m_dlg_Caption=Caption;
construct(parent);
}
QFilePathEdit::QFilePathEdit(const QString &Filter,QWidget* parent,const QString &Caption):QLabel(parent)
{
m_DlgType=FileDlgType;
m_dlg_Caption=Caption;
m_dlg_Filter=Filter;
construct(parent);
}
void QFilePathEdit::construct(QWidget* parent)
{
QHBoxLayout* layout=new QHBoxLayout;
m_edit=new QLineEdit(parent);
QPushButton* file_button= new QPushButton("...",parent);
file_button->setFixedWidth(20);
connect(file_button,SIGNAL(released()),this,SLOT(showFileDlg()));
connect(m_edit,SIGNAL(returnPressed()),this,SLOT(setPathFromLineEdit()));
layout->setSpacing(1);
layout->setMargin(1);
layout->addWidget(m_edit);
layout->addWidget(file_button);
this->setLayout(layout);
}
void QFilePathEdit::showFileDlg()
{
if(m_DlgType==DirDlgType)
setPath(QFileDialog::getExistingDirectory(0,m_dlg_Caption,m_path));
else if(m_DlgType==FileDlgType)
setPath(QFileDialog::getOpenFileName(0,m_dlg_Caption,m_path,m_dlg_Filter));
emit finished(this);
}
bool QFilePathEdit::setPath(const QString &Path)
{
m_edit->setText(m_path);
QFileInfo ThePath(Path);
if(m_DlgType==DirDlgType&&!ThePath.isDir())
{
if(!Path.isNull())
QMessageBox::warning(this,tr("Error"),tr("Directory \"")+Path+tr("\" doesn't exist!"));
return false;
}
else if(m_DlgType==FileDlgType&&!ThePath.isFile())
{
if(!Path.isNull())
QMessageBox::warning(this,tr("Error"),tr("File \"")+Path+tr("\" doesn't exist!"));
return false;
}
m_path=ThePath.canonicalFilePath();
m_edit->setText(m_path);
return true;
}
void QFilePathEdit::setPathFromLineEdit()
{
setPath(m_edit->text());
emit finished(this);
}
bool QFilePathEdit::getPath(QString &Path)
{
if(m_DlgType==DirDlgType&&!QFileInfo(m_path).isDir())
return false;
else if(m_DlgType==FileDlgType&&!QFileInfo(m_path).isFile())
return false;
Path=m_path;
return true;
}
QFilePathEdit::~QFilePathEdit(void)
{
}
 Р В Р’ВВВВВВВВзображенРСвЂВВВВВВВР В Р’Вµ СѓРСВВВВВВВВеньшено
259 x 100 (2.03 килобайт)
|
примечание:
если выбран неправильный файл - выводит сообщение об ошибке, сохраняя старое значение пути.
если выбран путь "" сообщения не выводится => изначально(до первого вызова редактора) Path должен существовать быть нулевым.
update:
существовать, либо быть нулевым.