Qt development experience tips 91-100

  1. If the thread is not the main thread, it is recommended to open the database. If the thread is not the main thread, it is also recommended to open the database. If the thread is not the main thread, it is also recommended to open the database.

  2. The new QTcpServer class may not enter the incomingConnection function under the 64 bit version of QT. That is because the incomingConnection function parameter corresponding to Qt5 has changed from int to qintptr. Changing to qintptr has an advantage. It is automatically quint32 on 32-bit and quint64 on 64-bit, If the parameter to continue writing in Qt5 is int, there is no problem on 32-bit and only on 64 bit. Therefore, in order to be compatible with Qt4 and Qt5, you must write according to different parameters.

    void incomingConnection(qintptr handle);
    void incomingConnection(int handle);
  1. Qt supports all interface controls, such as QPushButton and QLineEdit automatic association on_ Control name_ Signal (parameter) signal slot, such as button click signal on_pushButton_clicked(), and then directly implement the slot function.

  2. QWebEngineView control uses OpenGL. On some computers, the low drive of OpenGL may lead to flower screen or various strange problems. For example, in the case of showfullscreen, the right mouse button fails. You need to enable software opengl rendering in the main function.

    //The following two methods can be used. Qt adopts AA by default_ UseDesktopOpenGL
    QApplication a(argc, argv);

Another method is to solve the bug that the right-click menu cannot pop up when the full screen + QWebEngineView control together, and it needs to move up one pixel

QRect rect = qApp->desktop()->geometry();
  1. Qstype has many built-in methods, which are very useful, such as accurately obtaining the value of the slider under the mouse button.
QStyle::sliderValueFromPosition(minimum(), maximum(), event->x(), width());
  1. When reading and writing files with QFile, it is recommended to use QTextStream file stream to read and write files. The speed is much faster, and there will be basically a 30% improvement. The larger the file, the greater the performance difference.
//Load the comparison table of English attributes and Chinese attributes from the file
QFile file(":/propertyname.txt");
if (file.open(QFile::ReadOnly)) {
    //The QTextStream method reads at least 30 percent faster
#if 0
    while(!file.atEnd()) {
        QString line = file.readLine();
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
  1. Use qfile Readall() reads the QSS file in ANSI format by default and does not support UTF8. If you open the QSS file in QtCreator to edit and save, it may cause no effect after QSS is loaded.
void frmMain::initStyle()
    //Load style sheet
    QString qss;
    //QFile file(":/qss/psblack.css");
    //QFile file(":/qss/flatwhite.css");
    QFile file(":/qss/lightblue.css");
    if (file.open(QFile::ReadOnly)) {
#if 1
        //Use QTextStream to read style files without distinguishing between file encoding and bom
        QStringList list;
        QTextStream in(&file);
        while (!in.atEnd()) {
            QString line;
            in >> line;
            list << line;

        qss = list.join("\n");
        //Reading with readAll supports ANSI format by default. If you accidentally open it with creator and edit it, you may not open it
        qss = QLatin1String(file.readAll());
        QString paletteColor = qss.mid(20, 7);
  1. QString has many built-in conversion functions. For example, you can call toDouble to convert to double data, but when you finish the conversion and print, you will find that the accuracy is less, and there are only three digits left. In fact, the original data is still complete and accurate, but it is optimized to three digits when printing. If you want to ensure the complete accuracy, You can call the qsetralnumberprecision function to set the precision digits.
QString s1, s2;
s1 = "666.5567124";
s2.setNum(888.5632123, 'f', 7);
qDebug() << qSetRealNumberPrecision(10) << s1.toDouble() << s2.toDouble();
  1. When parsing data with QScriptValueIterator, you will find that there is always one more node content, and the content is empty. If you need to skip, add a line of code.
while (it.hasNext()) {
    if (it.flags() & QScriptValue::SkipInEnumeration)      
    qDebug() << it.name();
  1. setPixmap is the worst mapping method. Generally, it is only used for simple and infrequent mapping. It is recommended to paint with painter frequently. It is double buffered by default. It is drawn with opengl at advanced points and GPU.

Qt development experience open source homepage:

Added by markosjal on Sat, 05 Mar 2022 05:11:42 +0200