QMLPreviewer Example: More improvements

- Don't set implicitHeight to controls as the style should choose
valid default heights
- Use a QPushButton instead of a QToolButton in order to show
consistent styling between widgets and quick
- Make the QLineEdit editable so that a focus frame is displayed
around it (we don't show focus frames for read-only line edits in the
macOS style)
- Validate the URL when editing in the line edit is finished
- Add a tooltip to the button that opens the file dialog to make
its purpose more clear
- Fix typo and improve some messages shown in the message boxes

Pick-to: 6.8 6.8.0
Change-Id: I53095808a76e2615117039363be01cfa4d9f243f
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
Reviewed-by: MohammadHossein Qanbari <mohammad.qanbari@qt.io>
This commit is contained in:
Doris Verria 2024-09-19 14:54:32 +02:00
parent dca7f9b319
commit 790f16dd54
5 changed files with 22 additions and 14 deletions

View File

@ -16,7 +16,6 @@ Rectangle {
TextField {
id: textField
implicitHeight: 40
Layout.fillWidth: true
focus: true
@ -41,7 +40,6 @@ Rectangle {
Button {
id: button
text: qsTr("Click Me!")
implicitHeight: 40
Layout.fillHeight: true
focus: true

View File

@ -65,8 +65,8 @@ void EditorWidget::openFile()
case StateController::NewState:
case StateController::DirtyState: {
const auto answer = QMessageBox::question(this, tr("About to Open File"),
tr("There are some unsaved changes. "
"Do you want to discard the changes?"),
tr("You have unsaved changes. "
"Are you sure you want to discard them?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if (answer != QMessageBox::Yes)

View File

@ -88,8 +88,8 @@ void MainWindow::closeEvent(QCloseEvent *event)
case StateController::DirtyState: {
const QMessageBox::StandardButton answer =
QMessageBox::question(this, tr("About to Close"),
tr("There are some unsaved changes. "
"Do you want to close withou saving?"),
tr("You have unsaved changes. "
"Are you sure you want to close without saving?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (answer == QMessageBox::Yes)
event->accept();

View File

@ -7,12 +7,12 @@
#include <QFileDialog>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QToolButton>
#include <QPushButton>
PathEditWidget::PathEditWidget(QWidget *parent)
: QWidget{parent}
, m_lineEdit{new QLineEdit}
, m_toolButton{new QToolButton}
, m_urlButton{new QPushButton}
{
initUI();
@ -48,14 +48,14 @@ void PathEditWidget::initUI()
{
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_lineEdit, 1);
layout->addWidget(m_toolButton);
layout->addWidget(m_urlButton);
layout->setContentsMargins(0,0,0,0);
setLayout(layout);
m_lineEdit->setPlaceholderText(tr("File Path"));
m_lineEdit->setReadOnly(true);
m_toolButton->setText(tr("..."));
m_urlButton->setText(tr("Choose"));
m_urlButton->setToolTip(tr("Select path to QML file to load"));
}
void PathEditWidget::setupConnections()
@ -63,7 +63,8 @@ void PathEditWidget::setupConnections()
connect(StateController::instance(), &StateController::stateChanged, this,
&PathEditWidget::onAppStateChanged);
connect(m_toolButton, &QToolButton::clicked, this, &PathEditWidget::openFileRequested);
connect(m_lineEdit, &QLineEdit::editingFinished, this, &PathEditWidget::validatePath);
connect(m_urlButton, &QPushButton::clicked, this, &PathEditWidget::openFileRequested);
}
void PathEditWidget::setFilePath(const QString &filePath)
@ -94,3 +95,11 @@ void PathEditWidget::onAppStateChanged(int oldState, int newState)
break;
}
}
void PathEditWidget::validatePath()
{
const auto filePath = m_lineEdit->text();
QUrl url = QUrl::fromUserInput(filePath);
if (url.isValid())
emit fileSelected(filePath);
}

View File

@ -7,7 +7,7 @@
#include <QWidget>
class QLineEdit;
class QToolButton;
class QPushButton;
class PathEditWidget : public QWidget
{
@ -33,10 +33,11 @@ private:
private slots:
void onAppStateChanged(int oldState, int newState);
void validatePath();
private:
QLineEdit *m_lineEdit = nullptr;
QToolButton *m_toolButton = nullptr;
QPushButton *m_urlButton = nullptr;
};
#endif // PATHEDITWIDGET_H