From 790f16dd54762919c5c2a4bb335ff9b37a9a33a3 Mon Sep 17 00:00:00 2001 From: Doris Verria Date: Thu, 19 Sep 2024 14:54:32 +0200 Subject: [PATCH] QMLPreviewer Example: More improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 Reviewed-by: MohammadHossein Qanbari --- .../qmlpreviewer/resources/default.qml | 2 -- .../qmlpreviewer/widgets/editorwidget.cpp | 4 ++-- .../qmlpreviewer/widgets/mainwindow.cpp | 4 ++-- .../qmlpreviewer/widgets/patheditwidget.cpp | 21 +++++++++++++------ .../qmlpreviewer/widgets/patheditwidget.h | 5 +++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/examples/quick/quickwidgets/qmlpreviewer/resources/default.qml b/examples/quick/quickwidgets/qmlpreviewer/resources/default.qml index 4ab0c68f1b..04d7652a06 100644 --- a/examples/quick/quickwidgets/qmlpreviewer/resources/default.qml +++ b/examples/quick/quickwidgets/qmlpreviewer/resources/default.qml @@ -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 diff --git a/examples/quick/quickwidgets/qmlpreviewer/widgets/editorwidget.cpp b/examples/quick/quickwidgets/qmlpreviewer/widgets/editorwidget.cpp index 0e2c470bef..7ad9df7165 100644 --- a/examples/quick/quickwidgets/qmlpreviewer/widgets/editorwidget.cpp +++ b/examples/quick/quickwidgets/qmlpreviewer/widgets/editorwidget.cpp @@ -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) diff --git a/examples/quick/quickwidgets/qmlpreviewer/widgets/mainwindow.cpp b/examples/quick/quickwidgets/qmlpreviewer/widgets/mainwindow.cpp index 339379e127..dd7ac192d3 100644 --- a/examples/quick/quickwidgets/qmlpreviewer/widgets/mainwindow.cpp +++ b/examples/quick/quickwidgets/qmlpreviewer/widgets/mainwindow.cpp @@ -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(); diff --git a/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.cpp b/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.cpp index a9c8feeb5c..378e22acce 100644 --- a/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.cpp +++ b/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.cpp @@ -7,12 +7,12 @@ #include #include #include -#include +#include 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); +} diff --git a/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.h b/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.h index eeb4a3b77d..97a5e4cf59 100644 --- a/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.h +++ b/examples/quick/quickwidgets/qmlpreviewer/widgets/patheditwidget.h @@ -7,7 +7,7 @@ #include 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