Qt/C++ development experience tips 291-295

Domestic site: https://gitee.com/feiyangqingyun
International site: https://github.com/feiyangqingyun

  1. Regarding distinguishing Linux systems in pro, the Qt4 suite does not recognize the linux tag and needs to be represented by unix:!macx. Therefore, if there is a need to be compatible with Qt4, it is recommended to use unix:!macx.
//如果是linux上的Qt4套件则下面只会打印 unix linux
//如果是linux上的Qt5/Qt6套件则下面会打印 linux unix linux
linux {
    
    message(linux)}
unix {
    
    message(unix)}
unix:!macx {
    
    message(linux)}
  1. For some cross-platform projects, especially those that require the introduction of third-party libraries, the library files in the corresponding folders need to be introduced according to the different bit numbers of different systems, which requires identification and processing in the project.
#区分不同的系统
path_sys = win
win32 {
    
    
path_sys = win
}

linux {
    
    
path_sys = linux
}

#Qt4套件不认识linux标记
unix:!macx {
    
    
path_sys = linux
}

macx {
    
    
path_sys = mac
}

android {
    
    
path_sys = android
}

#区分不同的位数 x86_64/amd64/arm64/arm64-v8a
path_bit = 32
contains(QT_ARCH, x.*64) {
    
    
path_bit = 64
} else:contains(QT_ARCH, a.*64) {
    
    
path_bit = 64
} else:contains(QT_ARCH, a.*64.*) {
    
    
path_bit = 64
}

#对应系统和位数的库目录
path_lib = lib$$path_sys$$path_bit
//下面会打印 libwin32/libwin64/liblinux32/liblinux64/libmac32/libmac64/libandroid32/libandroid64
message($$path_lib)

//使用方式
INCLUDEPATH += $$PWD/include
LIBS += -L$$PWD/$$path_lib/ -lxxx
  1. When it is detected that some Qt build environments do not meet the current project requirements to avoid project compilation failure, the project can be disabled.
#禁用项目后整个项目的代码文件是灰色的不可用,编译会跳过。
lessThan(QT_MAJOR_VERSION, 6) {
    
    
error("最低要求Qt6才能用")
}
  1. When adding a button using the QButtonGroup button group, if you need to use the buttonClicked(int) signal, you must manually specify the button number when adding the button. Otherwise, the default number is -1, so the value is a negative number when buttonClicked(int) is triggered. , resulting in inconsistency with expectations. I originally thought that by default, the index will be automatically incremented in the order of adding buttons. In fact, it is not the case, and it will not be handled this way. Always remember to specify the button number.
QButtonGroup *btnGroup = new QButtonGroup(this);
connect(btnGroup, SIGNAL(buttonClicked(int)), ui->stackedWidget, SLOT(setCurrentIndex(int)));
//第二个参数指定按钮编号
btnGroup->addButton(ui->btn1, 0);
btnGroup->addButton(ui->btn2, 1);
  1. Regarding improvements to QCustomplot drawing performance.
  • Try to avoid lines with a pen width greater than 1. The default is 1. If the amount of data drawn is large, it is strongly not recommended to set the line width greater than 1, as the performance will be greatly reduced.
  • Avoid complex fills such as channel fills between shapes with thousands of points.
  • During chart dragging, you can set setNoAntialiasingOnDrag(true) to improve response speed.
  • Avoid using alpha (transparent) colors of any kind, especially in fills.
  • Try not to turn on anti-aliasing, setNotAntialiasedElements(true).
  • Avoid setting the complete data set repeatedly, for example using setData. If most data points remain the same, such as in running measurements, use addData instead.
  • It is recommended to access and operate existing data through QCPGraph::data(), which is more efficient.
  • Turn on opengl acceleration. The first step is to turn on the mark and add a line of DEFINES += QCUSTOMPLOT_USE_OPENGL in pro. The second step is to link the opengl library, LIBS += -lopengl32 -lglu32. Some Qt versions may also need to actively introduce QT += widgets. After actual testing, it was found that high-frequency drawing, such as 60fps, has improved performance when opengl is turned on, mainly due to reduced CPU usage. Low-frequency drawing also increases CPU usage. Therefore, it is recommended to handle it according to the actual scenario. This seems to have nothing to do with the amount of data, but to do with the speed of drawing data.
  • Turn off the anti-aliasing property of the canvas, graph->setAntialiased(false) graph->setAntialiasedFill(false) graph->setAntialiasedScatters(false). The default is true.
  • Turn on adaptive sampling on the canvas, graph->setAdaptiveSampling(true). The default is true, so there is no need to actively set it.

Guess you like

Origin blog.csdn.net/feiyangqingyun/article/details/135081188