2025-03-17 09:59:13 +08:00
|
|
|
#include "CLineEditWithBt.h"
|
2025-03-14 17:05:48 +08:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QEvent>
|
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QToolButton>
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
|
|
CLineEditWithBt::CLineEditWithBt(QWidget *parent, const QString &filePath, Option option)
|
|
|
|
|
:QWidget(parent),m_filePath(filePath),m_option(option)
|
|
|
|
|
{
|
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout(this);
|
|
|
|
|
m_edit = new QLineEdit(this);
|
|
|
|
|
m_edit->installEventFilter(this);
|
|
|
|
|
m_button = new QToolButton(this);
|
|
|
|
|
m_button->setText("...");
|
|
|
|
|
m_button->setFixedSize(21,20);
|
|
|
|
|
layout->addWidget(m_edit);
|
|
|
|
|
layout->addWidget(m_button);
|
|
|
|
|
layout->setMargin(0);
|
|
|
|
|
layout->setSpacing(0);
|
|
|
|
|
this->setLayout(layout);
|
|
|
|
|
|
|
|
|
|
connect(m_button, SIGNAL(clicked()), this, SLOT(getFilePath()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CLineEditWithBt::~CLineEditWithBt()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CLineEditWithBt::text()
|
|
|
|
|
{
|
|
|
|
|
return m_edit->text();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLineEditWithBt::setText(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
m_edit->setText(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLineEditWithBt::getFilePath()
|
|
|
|
|
{
|
|
|
|
|
QFileDialog *dialog = new QFileDialog(this, tr("选择文件"), m_filePath+text());
|
|
|
|
|
dialog->setOption(QFileDialog::DontUseNativeDialog);
|
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
connect(dialog, SIGNAL(fileSelected(QString)), this, SLOT(setFilePath(QString)));
|
|
|
|
|
dialog->show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLineEditWithBt::setFilePath(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
if(name != QString())
|
|
|
|
|
{
|
|
|
|
|
QDir dir;
|
|
|
|
|
switch (m_option) {
|
|
|
|
|
case GetFile:
|
|
|
|
|
{
|
|
|
|
|
dir.cd(m_filePath);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GetPath:
|
|
|
|
|
{
|
|
|
|
|
dir.cd(QCoreApplication::applicationDirPath());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString text = dir.relativeFilePath(name);
|
|
|
|
|
if(text.right(4) == ".exe")
|
|
|
|
|
{
|
|
|
|
|
text.remove(-4, 4);
|
|
|
|
|
}
|
|
|
|
|
setText(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CLineEditWithBt::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if(watched == m_edit)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::FocusOut)
|
|
|
|
|
{
|
|
|
|
|
this->setFocus();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
|
}
|