- qtchooser
- Qt Modules
- .pro
- Concrete QWidget
- UIC
- MOC (meta-object compiler)
- Q_OBJECT v.s. Q_GADGET
- UI
- Event
- Paint
- File
- Logging
vi /usr/lib/x86_64-linux-gun/qt-chooser/default.conf
<qt>/bin
<qt>/lib
- Basic
- Qt Core
- Qt GUI
- Qt Quick
- Qt QML
- Qt Test
- Qt Network
- Qt Multimedia
- Qt SQL
- Qt Webkit
- Qt WebKit Widgets
- Qt Widgets
- Extended
- Qt 3D / Qt Bluetooth / Qt Contact
- Qt Location / Qt Organizer / Qt Sensors / Qt Publis and Subscribe
- Qt Service Framework / System Info / JSON DB / Versit / Wayland / Feekback
CONFIG += C++11
class MyWidget: public QWidget {
public:
MyWidget() : QWidget() {}
~MyWidget() {}
public slots: // optional parameters, dependents on signal functions, // void return, need implementation
void button_click();
signals: // no return value, optional parameters // only declare, no implementation
void show_hide_signal(int a);
};
- MOC is not the only code generator. Another code generator that Qt uses is uic (User Interface Compiler).
- UIC reads user interface described in XML and creates C++ code that sets up the form.
- The program that handles Qt's C++ extensions.
- moc reads C++ source, if it contains
Q_OBJECT
macor, it produces another C++ file which contain the meta object code for those classes.
- moc reads C++ source, if it contains
- one signal func can connect to multiple slots function, invoke order is undefined
- multiple signal can connect to one slot
- one signal can connect to another signal
- the slot func of the signal can
emit
another signal
- the slot func of the signal can
- member func, static func, global func, lambda func(since QT5)
- Lambda
int a = 20; [=]() mutable -> int { a = 100; return a; }
- Lambda
- slots function parameter could be less than the parameter in signal func
- if slots func has parameters, it should be the same order / type with signal func
- when one object is deleted, QT disconnect all slots connected to the object automatically
-
QObject::connect(&button, SIGNAL(clicked()), &app, SLOT(quit()));
-
Qt5
QObject::connect(&button, &QPushButton::clicked, &app, &QApplication::quit);
-
Lambda
QObject::connect(button, &QPushButton::clicked, secondWindow, [secondWindow]() { secondWindow->setText("clicked"); });
- the 3rd parameter
secondWindow
is optional, but to specific it would prevent crash issue oncesecondWindow
is deleted earlier than current connect.
- adds features to C++ (MOC)
- signals and slots
- Properties
- Event handlong
- Memory Management
- destruct all children first in its destructor
Q_GADGET is lighter version of Q_OBJECT, it doesn't have signal and slot.
The Q_GADGET macro is a lighter version of the Q_OBJECT macro for classes that do not inherit from QObject but still want to use some of the reflection capabilities offered by QMetaObject. Just like the Q_OBJECT macro, it must appear in the private section of a class definition.
Q_GADGETs can have Q_ENUM, Q_PROPERTY and Q_INVOKABLE, but they cannot have signals or slots.
Q_GADGET makes a class member, staticMetaObject, available. staticMetaObject is of type QMetaObject and provides access to the enums declared with Q_ENUMS.
- relative path used in UI is related to Makefile
- not UI file, not source file, not exe
- when adding resource/image to resouce file, it must be inside the folder of project
x, y, width, height
could be affected by anchor
Rectangle {
id: bg
Rectangle {
width: 40; height: 40
y: 20
anchor.right: bg.right
anchor.top: bg.top // override y:20
}
}
e.ignore()
generally is not used in override event functions, it may used in dialog close event.
class MyButton: public QPushButton {
protected:
void MyButton::mousePressedEvent(MouseEvent *e) {
if (e->button() == Qt::LeftButton) {
qDebug() << "left button pressed";
// e.ignore(); // this would pass the event to parent widget, not super class (inhereted class)
}
QPushButton::mousePressedEvent(e);
}
/// event filter
private:
MyLabel *label;
/*
label->installEventFilter(this);
bool eventFilter(QObject *obj, QEvent *e) {
if (obj == lable) {
if (e->type() == QEvent::MouseMove) {
// ...
}
}
return QWidget::eventFilter(obj, e);
}
*/
};
- In
QObject
- return
true
: it won't forward anymore; returnfalse
: it will continue forward - event filter and the object/widget it filters MUST be in same thread
- QPixmap: optimized for scree, platform-related, cannot modify image
- QImage : platfor-unrelated, can modify pixes of image, can be drawn in thread
- QPicture : save paiting status in binary
QFileInfo q(path);
qDebug << q.fileName().utf8().data(); // multil-bytes
- Def Logging category in code
bool Highscore::load(const QString &filepath)
{
QLoggingCategory fcIo("fc.io");
qCDebug(fcIo) << "Loading highscore ...";
- Def Logging category in one place
// fclogging.h
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(fcIo)
// fclogging.cpp
#include "fclogging.h"
Q_LOGGING_CATEGORY(fcIo, "fc.io")
- Rules can be defined in config file or in code
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
QLoggingCategory::setFilterRules("*.debug=false\n"
"fc.io.debug=true");
// ...
}
// ~/.config/QtProject/qtlogging.ini:
[rules]
*.debug=false
fc.io.debug=true