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

View File

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

View File

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

View File

@ -7,12 +7,12 @@
#include <QFileDialog> #include <QFileDialog>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLineEdit> #include <QLineEdit>
#include <QToolButton> #include <QPushButton>
PathEditWidget::PathEditWidget(QWidget *parent) PathEditWidget::PathEditWidget(QWidget *parent)
: QWidget{parent} : QWidget{parent}
, m_lineEdit{new QLineEdit} , m_lineEdit{new QLineEdit}
, m_toolButton{new QToolButton} , m_urlButton{new QPushButton}
{ {
initUI(); initUI();
@ -48,14 +48,14 @@ void PathEditWidget::initUI()
{ {
QHBoxLayout *layout = new QHBoxLayout; QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_lineEdit, 1); layout->addWidget(m_lineEdit, 1);
layout->addWidget(m_toolButton); layout->addWidget(m_urlButton);
layout->setContentsMargins(0,0,0,0); layout->setContentsMargins(0,0,0,0);
setLayout(layout); setLayout(layout);
m_lineEdit->setPlaceholderText(tr("File Path")); 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() void PathEditWidget::setupConnections()
@ -63,7 +63,8 @@ void PathEditWidget::setupConnections()
connect(StateController::instance(), &StateController::stateChanged, this, connect(StateController::instance(), &StateController::stateChanged, this,
&PathEditWidget::onAppStateChanged); &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) void PathEditWidget::setFilePath(const QString &filePath)
@ -94,3 +95,11 @@ void PathEditWidget::onAppStateChanged(int oldState, int newState)
break; 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> #include <QWidget>
class QLineEdit; class QLineEdit;
class QToolButton; class QPushButton;
class PathEditWidget : public QWidget class PathEditWidget : public QWidget
{ {
@ -33,10 +33,11 @@ private:
private slots: private slots:
void onAppStateChanged(int oldState, int newState); void onAppStateChanged(int oldState, int newState);
void validatePath();
private: private:
QLineEdit *m_lineEdit = nullptr; QLineEdit *m_lineEdit = nullptr;
QToolButton *m_toolButton = nullptr; QPushButton *m_urlButton = nullptr;
}; };
#endif // PATHEDITWIDGET_H #endif // PATHEDITWIDGET_H