当前位置:网站首页>[turn] flying clouds_ Qt development experience

[turn] flying clouds_ Qt development experience

2022-06-11 06:33:00 Quiet Zhiyuan 2021

【 turn 】 Flying clouds _Qt Development experience

Link to the original text :https://blog.csdn.net/sinolover/article/details/121392753

One 、 Development experience
01:001-010
When a large number of errors are found in the compilation , We should start from the first one , One by one solution , Don't rush to see the next mistake , Often the latter mistakes are caused by the former ones , After the first one, it's likely to be solved .
Timers are a good thing , Learn to use it well , Sometimes used QTimer::singleShot Single timer and QMetaObject::invokeMethod Can solve unexpected problems . For example, a time-consuming operation is loaded when the form is initialized , It is easy to display the main interface of the card , The interface will not be displayed until after loading , This is too laggy to experience. , At this point, you can load the time-consuming ( Sometimes these loads must be in the main thread , For example, use QStackWidget Stack form loads some subforms ), Load late or asynchronously , This will be executed after the interface is displayed , Instead of the card main interface .
// Asynchronous execution load function
QMetaObject::invokeMethod(this, “load”, Qt::QueuedConnection);
// Time delay 10 Execution in milliseconds load function
QTimer::singleShot(10, this, SLOT(load()));
Default QtCreator It's single threaded compilation , At the beginning of the design, it may be considered that the system resources should not be occupied as much as possible , And today's computers are multi-core , Default msvc The compiler is multithreaded and does not need to be set manually , For other compilers , You need to set it manually .
Method 1 : In the build settings of each project ( You can check one shadow build Where is your page ) Of build step ,make arguments Add a row -j16 that will do , This setting will be saved in pro.user In file , Once removed, it needs to be reset , This method is not recommended ;
Method 2 : Add... To the build suite environment , Tools -> Options -> Build Suite (kits)-> Select a build Suite ->environment-> On the right side change Button -> Fill in the open input box with MAKEFLAGS=-j4 , So you don't have to set up multithreaded compilation every time , This compilation parameter will be added to any project that applies the component suite ;
Be careful :-j Then the core number of the computer , Too much writing doesn't work , Look at the parameters of the computer , Or fill in a -j4 Just go , After all, computers now 4 The core should be the most basic ;
Probably from 2019 A new version of QtCreator By default, multithreading compilation will be set automatically according to the core of the computer , For example, recognize that your computer is 16 The core will be set by default -j16 Parameter compilation ;
If you want to take advantage of QtCreator Deploy Android programs , First you have to be in the AndroidStudio Internal configuration succeeded , Level all the holes .

A lot of times I find Qt After corresponding encapsulation method , Remember to take a look at the overload of this function , Multiple parameters , You will find a different world , Sometimes I suddenly realize , original Qt It's sealed for us , such as QString、QColor The overload parameters of are extremely rich .

Can be in pro Mark the version number in the document +ico Icon (Qt5 To support ), Actually in windows Up is qmake This information will be automatically converted into rc file .

VERSION = 2025.10.01
RC_ICONS = main.ico
The administrator runs the program , Limit to MSVC compiler .
QMAKE_LFLAGS += /MANIFESTUAC:“level=‘requireAdministrator’ uiAccess=‘false’” # Run as an administrator
QMAKE_LFLAGS += /SUBSYSTEM:WINDOWS,“5.01” #VS2013 stay XP function
The debug output window is attached to the run file , This is very useful , A lot of times when we release the program phase , We will encounter the program double-click can not run, also do not report an error prompt ( Everything is OK on the development machine ), I don't know what happened , Even the task manager can see it running, but no interface pops up , At this point, we need to be in the pro Add this to the file , The program with interface will also automatically pop up the debug window and print out information , Easy to find questions , Generally, programs that can't run normally will print some prompt information, such as what's missing .
TEMPLATE = app
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
# The following line is used to set the run file with debug output window
CONFIG += console
Draw a tiled background QPainter::drawTiledPixmap, Draw a rounded rectangle QPainter::drawRoundedRect(), instead of QPainter::drawRoundRect();

Remove old styles

// Remove the original style
style()->unpolish(ui->btn);
// There must be the following line, otherwise it will not be unloaded
ui->btn->setStyleSheet("");
// Reset the style of the new control .
style()->polish(ui->btn);
02:011-020
Get the properties of the class
const QMetaObject *metaobject = object->metaObject();
int count = metaobject->propertyCount();
for (int i = 0; i < count; ++i) {
QMetaProperty metaproperty = metaobject->property(i);
const char *name = metaproperty.name();
QVariant value = object->property(name);
qDebug() << name << value;
}
Qt The built-in icon is encapsulated in QStyle in , About 70 icons , You can use it directly .
SP_TitleBarMenuButton,
SP_TitleBarMinButton,
SP_TitleBarMaxButton,
SP_TitleBarCloseButton,
SP_MessageBoxInformation,
SP_MessageBoxWarning,
SP_MessageBoxCritical,
SP_MessageBoxQuestion,

// Take it out and use it like this
QPixmap pixmap = this->style()->standardPixmap(QStyle::SP_TitleBarMenuButton);
ui->label->setPixmap(pixmap);
Load according to the number of bits of the operating system
win32 {
contains(DEFINES, WIN64) {
DESTDIR = KaTeX parse error: Expected 'EOF', got '}' at position 18: …D/../bin64 }̲ else { …PWD/…/bin32
}
}
Qt5 Enhanced a lot of security validation , If appear setGeometry: Unable to set geometry, Please move the visibility of the control after adding the layout .

You can put the control A Add to layout , And then the control B Set the layout , This flexibility improves the composability of controls , For example, you can add a search button to the left and right of the text box , Button to set the icon .

QPushButton *btn = new QPushButton;
btn->resize(30, ui->lineEdit->height());
QHBoxLayout *layout = new QHBoxLayout(ui->lineEdit);
layout->setMargin(0);
layout->addStretch();
layout->addWidget(btn);
Yes QLCDNumber Control to set the style , Need to put QLCDNumber Of segmentstyle Set to flat, Or you'll find that it doesn't work .

Clever use findChildren You can find all the child controls under this control . findChild To find a single .

// Find the specified class name objectName Control for
QList<QWidget *> widgets = fatherWidget.findChildren<QWidget *>(“widgetname”);
// Find all QPushButton
QList<QPushButton *> allPButtons = fatherWidget.findChildren<QPushButton *>();
// Find the first level child control , Otherwise, all the child controls will be traversed all the time
QList<QPushButton *> childButtons = fatherWidget.findChildren<QPushButton *>(QString(), Qt::FindDirectChildrenOnly);
Clever use inherits Judge whether it belongs to a certain kind .
QTimer *timer = new QTimer; // QTimer inherits QObject
timer->inherits(“QTimer”); // returns true
timer->inherits(“QObject”); // returns true
timer->inherits(“QAbstractButton”); // returns false
Use the weak attribute mechanism , You can store temporary values for passing judgment . Can pass widget->dynamicPropertyNames() List all weak attribute names , And then through widget->property(“name”) Take out the value of the corresponding weak attribute .

At development time , Whether it's for the convenience of maintenance , Or to save memory resources , There should be one qss File to store all the style sheets , Instead of putting setStyleSheet It's all over the place . If it's the beginning stage or the testing stage, you can directly UI Right click to set style sheet , Formal projects are still suggested to be unified into one qss Style sheet files are better , Unified management .

03:021-030
If appear Z-order assignment: is not a valid widget. Error message , Use Notepad to open the corresponding ui file , Find the empty space , Delete it .

Be good at using QComboBox Of addItem The second parameter sets the user data , Can achieve a lot of results , Use itemData out .

If used webengine modular , Bring... With you when you release the program QtWebEngineProcess.exe+translations Folder +resources Folder .

Default Qt It's a form, a handle , If you want each control to have its own handle , Settings a.setAttribute(Qt::AA_NativeWindows);

Qt+Android Prevent the program from being closed .

原网站

版权声明
本文为[Quiet Zhiyuan 2021]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020526288174.html