- The slash of file path is also different on different platforms. For example, linux system generally has / slash, while windows has \ two backslashes. Qt itself supports the path of / slash in both win and linux. However, some third-party libraries may need to be converted to the path of the corresponding system, which requires slash conversion, Qt of course has built-in class methods.
QString path = "C:/temp/test.txt"; path = QDir::toNativeSeparators(path); //Output C: \ \ temp \ \ test txt QString path = "C:\\temp\\test.txt"; path = QDir::toNativeSeparators(path); //Output C: / temp / test txt
- Using QMetaObject::invokeMethod skillfully can achieve many effects, including synchronous and asynchronous execution, which largely solves the problem of cross thread signal slot processing. For example, in an application scenario, a public function needs to be called asynchronously in a callback. If it is called directly, it will be found unsuccessful. At this time, QMetaObject::invokeMethod(obj, "fun", Qt::QueuedConnection) needs to be used; Just do it this way. The invokeMethod function has many overloaded parameters. You can pass in the return value and the parameters of the execution method. The invokeMethod function supports not only slot functions but also signals. Moreover, it is thread safe and can be used safely in threads. Cow!
//Header files declare signals and slot functions signals: void sig_test(int type,double value); private slots: void slot_test(int type, double value); //Constructor associated signal slot connect(this, SIGNAL(sig_test(int, double)), this, SLOT(slot_test(int, double))); //Click the button to trigger the signal and slot. Here are examples of signal slots at the same time void MainWindow::on_pushButton_clicked() { QMetaObject::invokeMethod(this, "sig_test", Q_ARG(int, 66), Q_ARG(double, 66.66)); QMetaObject::invokeMethod(this, "slot_test", Q_ARG(int, 88), Q_ARG(double, 88.88)); } //Will print 66.66 and 88.88 void MainWindow::slot_test(int type, double value) { qDebug() << type << value; }
-
The signal in Qt5 is public and can be directly emit where needed. In Qt4, the signal is protected and cannot be used directly. You need to define a public function to emit.
-
Qt5. Since version 15, the official will no longer provide the installation package, but only the source code, which can be compiled or installed online. It is estimated that it is too troublesome to compile various versions each time. It is more to collect user use information. For example, through online installation, commercialization may be gradually increased in the later stage.
-
Sometimes we need to judge whether there is a module in the current QT version. We can use qtHaveModule (a new judgment introduced by Qt5) to judge. If we want to judge whether there is a module added by QT + = in our project, we can use contains to judge.
qtHaveModule(webenginewidgets) { message("current Qt Library found webenginewidgets modular") } !qtHaveModule(webkit) { message("current Qt library cannot found webkit modular") } contains(QT, network) { message("The current project has been imported network modular") } !contains(QT, widgets) { message("The current project has not been imported widgets modular") }
- The original string format is newly introduced in c++11. Users avoid adding escape characters \, which can be used to represent json strings and other scenarios.
QString s1 = R"(test\001.jpg)"; s1.replace("\\", "#"); qDebug()<< s1; //Result test#001 jpg
-
It is recommended to use qInfo() instead of qDebug() to print information on Android.
-
Qt's default timer accuracy is not high enough (for example, the application scenario is to save a record or file in one minute. When you use the default timer, you will find that some times it is 60 seconds and some are 59 seconds random. If the customer requires it, you need to set the accuracy. Of course, most of our projects do not need a timer with very high accuracy. After all, the higher the accuracy, the greater the system resources may be occupied.) , if you need to set higher precision, you can set setTimerType(Qt::PreciseTimer). Qt has two kinds of timer processing, one is QTimer class, and the other is the built-in timeevent event of QObject class. If the timer of QObject class needs to be set, call startTimer(interval, Qt::PreciseTimer);
- Qt::PreciseTimer is an accurate timer, and try to maintain the accuracy of milliseconds.
- Qt::CoarseTimer is a rough timer. Try to keep the accuracy within 5% of the required time interval.
- Qt::VeryCoarseTimer is a very rough timer, which only retains the complete second precision.
- No matter how high the accuracy is, it also depends on the corresponding operating system interrupt. Assuming that the interrupt takes 5ms, the timer accuracy cannot be higher than 5ms.
-
QGraphicsEffect related classes consume a lot of CPU, and even have conflicts and interference with some places during drawing. It is basically not recommended to use them. If necessary, it is only recommended to use them in a small amount and in places that trigger drawing infrequently.
-
Set the registry with qssettings. If it is not run as an administrator, qssettings will be printed: failed to set subkey "XXX", You need to manually right-click the mouse to run as an administrator.
Qt development experience open source homepage (continuously updated):