-
Notifications
You must be signed in to change notification settings - Fork 79
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
base: master
Are you sure you want to change the base?
Conversation
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.
Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp
Outdated
Show resolved
Hide resolved
void closeEvent(QCloseEvent *event); | ||
|
||
private slots: | ||
void on_lineEdit_textChanged(const QString &arg1); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
Blog-projects/Widget-window-in-Qt-Quick-app/fontcontrolswidgetsform.h
Outdated
Show resolved
Hide resolved
Blog-projects/Widget-window-in-Qt-Quick-app/widgetFormHandler.cpp
Outdated
Show resolved
Hide resolved
- 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() |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused, remove
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.