Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for Widget window in Qt Quick #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Cuperino
Copy link

Example app where a window bounces across the screen, like an old DVD player's logo would. You control the text through one of two identical forms, one implemented in QML and the other in Widgets. The example code follows best practices.

Example app wher a window bounces across the screen,
like an old DVD player's logo would. You control
the text through one of two identical forms,
one implemented in QML and the other in Widgets.
The example code follows best practices.

Due to limitations with Wayland, it only works on
X11 on Linux.
void closeEvent(QCloseEvent *event);

private slots:
void on_lineEdit_textChanged(const QString &arg1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on_* is definitely not best practice (no error at compile time in case of a mismatch). Please use a real connect to PMF.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to explicit, modern, connects. Removed confusing on_ prefix.
Are my declarations and casing correct or can these be improved?

- Dropped on_ from UI form actions naming pattern
- Manually connected to UI form actions
- Made getters const
- Renamed getters to follow
- Turn fontList into a static const member function
- Indicate function override
emit textChanged();
}

const QString FontControlsWidgetsForm::font()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misunderstanding.
Returning a const QString serves no purpose (on the contrary - it prevents move semantics for the caller)

When I said "make those methods const", I meant marking the actual methods themselves as const:

QString FontControlsWidgetsForm::font() const

(both in .h and .cpp)

(applies to all getters)

The purpose is to indicate (and ensure) that the member variables are not (and cannot be) modified in such methods, because in a const method, all member variables are const.

return ui->fontComboBox->currentFont().family();
}

void FontControlsWidgetsForm::closeEvent(QCloseEvent *event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent formatting: you inserted a newline before '{' in most other methods (which is indeed more common).

You could grab the _clang-format from e.g. KDABViewer and run clang-format over the whole code, for consistent formatting.

QApplication::quit();
}

void FontControlsWidgetsForm::fontComboBox_currentFontChanged(const QFont &f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I see why you asked about casing... This snake/camel mix is weird ;)

Suggestions:
slotFontChanged
onFontChanged

or in you really need to mention the originating widget, say if you have two lineEdits and you were connecting to textChanged() from each, you'd say something like slotNameChanged and slotCountryChanged or possibly slotNameTextChanged/slotCountryTextChanged. But it's not like the exact name of the signal has to be in the slot name. Only if you were connecting to multiple similar signals.

signals:
void textChanged();
void fontChanged();
void pushButton_clicked();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buttonClicked()

{
Q_OBJECT
QML_ELEMENT
private:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

public:
explicit Timer(QObject *parent = nullptr);

Q_INVOKABLE int deltaTime();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q_INVOKABLE int deltaTime() const;

m_window = new FontControlsWidgetsForm();
m_window->setVisible(false);

QObject::connect(m_window, &FontControlsWidgetsForm::pushButton_clicked, this, &WidgetFormHandler::toggleWidgetsWindow);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "QObject::", unneeded, this is a QObject subclass, it's in scope.

#ifndef WIDGETFORMHANDLER_H
#define WIDGETFORMHANDLER_H

#include "fontcontrolswidgetsform.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't class FontControlsWidgetsForm; enough in the header?
(and of course, do the real #include in the .cpp file)


#include <QObject>
#include <QQmlEngine>
#include <QWidget>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed here


Q_INVOKABLE int deltaTime();

signals:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused, remove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants