logplus/logPlus/fileedit.cpp
2025-10-29 17:23:30 +08:00

85 lines
2.7 KiB
C++

/****************************************************************************
**
** Copyright (C) 2006 Trolltech ASA. All rights reserved.
**
** This file is part of the documentation of Qt. It was originally
** published as part of Qt Quarterly.
**
** This file may be used under the terms of the GNU General Public License
** version 2.0 as published by the Free Software Foundation or under the
** terms of the Qt Commercial License Agreement. The respective license
** texts for these are provided with the open source and commercial
** editions of Qt.
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "fileedit.h"
#include <QHBoxLayout>
#include <QToolButton>
#include <QFileDialog>
#include <QFocusEvent>
FileEdit::FileEdit(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setMargin(0);
layout->setSpacing(0);
theLineEdit = new QLineEdit(this);
theLineEdit->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred));
QToolButton *button = new QToolButton(this);
button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred));
button->setText(QLatin1String("..."));
layout->addWidget(theLineEdit);
layout->addWidget(button);
setFocusProxy(theLineEdit);
setFocusPolicy(Qt::StrongFocus);
setAttribute(Qt::WA_InputMethodEnabled);
connect(theLineEdit, SIGNAL(textEdited(const QString &)),
this, SIGNAL(filePathChanged(const QString &)));
connect(button, SIGNAL(clicked()),
this, SLOT(buttonClicked()));
}
void FileEdit::buttonClicked()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("选择文件"), theLineEdit->text(), theFilter);
if (filePath.isNull())
return;
theLineEdit->setText(filePath);
emit filePathChanged(filePath);
}
void FileEdit::focusInEvent(QFocusEvent *e)
{
theLineEdit->event(e);
if (e->reason() == Qt::TabFocusReason || e->reason() == Qt::BacktabFocusReason) {
theLineEdit->selectAll();
}
QWidget::focusInEvent(e);
}
void FileEdit::focusOutEvent(QFocusEvent *e)
{
theLineEdit->event(e);
QWidget::focusOutEvent(e);
}
void FileEdit::keyPressEvent(QKeyEvent *e)
{
theLineEdit->event(e);
}
void FileEdit::keyReleaseEvent(QKeyEvent *e)
{
theLineEdit->event(e);
}