Qt develops Dugu Jiujian

Original link

In the Qt development process, there are many tips that can greatly improve the development efficiency. Here, I hope to help some beginners of QT framework avoid detours.

1. Remove the old style and add a new style

During QT development, we usually modify the style of controls through QSS similar to CSS. In order to modify the style of the control, we often need to dynamically remove the old style and add a new style. At this time, we need the following operations.

//Remove existing style
style()->unpolish(ui->btn);
//Re style the new control.
style()->polish(ui->btn);

2.QT compression and decompression

I didn't know QT's own decompressed library before. Every time I want to decompress files, I have to spend a lot of effort to introduce zlib library. In fact, QT's own decompressed library is only set to private and is not exposed to the outside world. Here's the calling method.

The zip file decompression class qziprader and compression class QZipWriter are in the GUI private module. QT + = GUI private needs to be introduced into pro to use.

#include "QtGui/private/qzipreader_p.h"
#include "QtGui/private/qzipwriter_p.h"

QZipReader reader(dirPath);
QString path("");
//Unzip the folder to the current directory
reader.extractAll(path);
//Folder name
QZipReader::FileInfo fileInfo = reader.entryInfoAt(0);
//Unzip file
QFile file(filePath);
file.open(QIODevice::WriteOnly);
file.write(reader.fileData(QString::fromLocal8Bit("%1").arg(filePath)));
file.close();
reader.close();

QZipWriter *writer = new QZipWriter(dirPath);
//add folders
writer->addDirectory(unCompress);
//Add file
QFile file(filePath);
file.open(QIODevice::ReadOnly);
writer->addFile(data, file.readAll());
file.close();
writer->close();

3. Compile QT+MSVC project through script

Many times, in order to realize the automation of packaging, we need to compile the project through script. Here is how to compile QT5 program under windows through bash script. The compiler used here is MSVC. The compiler through MSVC runs more efficiently on windows.

rem In order for the program to find QT The installation path will be QT Add installation path to environment variable
echo off
pushd %~dp0
echo Setting up environment for Qt usage...
set PATH=C:\Qt\Qt5.9.0\5.9\msvc2015\bin\;%PATH%

rem By running in a pure command prompt window vcvarsall.bat,Environment variables can be set to configure the command line for 32-bit or 64 bit native compilation

echo build special-symbol
CALL "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64_x86 
echo vcvarsall finished

rem Compile the corresponding project file
make ..\myapp.pro -spec win32-msvc
jom qmake_all
jom.exe -f Makefile.Release
jom.exe -f Makefile.Release clean

4.QT implementation garbled code problem solving

There are two important concepts in QT:

The source character set

What code is used to save the source file

The execution character set

What kind of encoding is stored in the executable program (string encoding in memory when the program is executed)

The reason why QT programs are garbled when transplanted to windows is often caused by the inconsistency between the source code character set and the execution character set. The QT source code transplanted through Linux is often encoded by utf-8, and the execution code is often local code, that is, GBK. In order to ensure that there is no garbled code, we set the execution code and file code to be consistent.

In general, the solution is one: you must know the source code and the code executed.

The executed code can also be modified as follows:

#pragma execution_character_set("utf-8")

5. Add post compilation event for QT project

When using visual studio, we can add some post compilation events to operate the compiled executable programs, such as copying files to other places. When using QT creator, we also support adding post compilation events. The adding methods are as follows

sourceExePath = $$OUT_PWD\release\myapp.exe
targetExePath = $$OUT_PWD\..\..\resource\tools\myapp.exe
sourceExePathWin = $$replace(sourceExePath, /, \\)
targetExePathWin = $$replace(targetExePath, /, \\)
QMAKE_POST_LINK += copy $$sourceExePathWin  $$targetExePathWin

The above code should be added to the project file, that is, the pro file. The effect is to copy the executable file to other directories after compilation.

6. Translate English controls (taking QColorDialog as an example)

The default display labels of the system's own controls such as QColorDialog are in English. In order to support Chinese display, a translation plug-in needs to be introduced by importing qt_zh_CN.qm file to translate windows

QColorDialog *pColorDialog = new QColorDialog(this);
pColorDialog->setWindowTitle("Select color");
pColorDialog->show();

QTranslator *pTranslator = new QTranslator(this);
if (pTranslator->load(":/qt_zh_CN.qm"))
{
    qApp->installTranslator(pTranslator);
}

However, due to the imperfect translation module of the system, the translation is incomplete and needs to be repaired manually. The repair is in QT_ zh_ CN. The translation that is not imported is introduced into the TS, and then the QT comes with the tool lrelease Exe compiles it into the corresponding qt_zh_CN.qm file.

The imported external translation code is shown in the following figure:

Take color button and select screen color information

<message>
        <source>&amp;Pick Screen Color</source>
        <translation>Pick screen color</translation>
 </message>

<message>
        <source>Cursor at %1, %2
Press ESC to cancel</source>
		<translation>coordinate: %1, %2
 Press ESC Key cancel</translation>
    </message>

//Determines and cancels the style of the button
<context>
    <name>QPlatformTheme</name>
    <message>
        <location filename="../src/widgets/qdialogbuttonbox.cpp" line="+42"/>
        <location line="+18"/>
        <source>OK</source>
        <translation>determine</translation>
    </message>
    <message>
        <location line="+54"/>
        <source>Cancel</source>
        <translation>cancel</translation>
    </message>
</context>

qt_zh_CN.ts files are generally in the source code. If you can't find them, you can contact me.

7. Set that the current program does not preempt the input focus of other programs

In Qt4, use setwindowflags (QT:: frameleswindowhint | QT:: windowstaysontophint | QT:: tool); Realize no focus window;

In Qt5, use setwindowflags (QT:: frameleswindowhint | QT:: windowstaysontophint | QT:: tool | QT:: windowdoesnotaceptfocus)); Realize no focus window;

QT:: windowdoesnotaceptfocus is an option added to Qt5. My understanding is that this is the part separated from the non focus feature of Qt::Tool in Qt4.

If you want the window to have a border, remove the first item QT:: frameleswindowhint.

After setting the window through the above method, the process still exists after the window is closed. Solution:

First, in MainWindow Add the following code to the cpp file

setAttribute(Qt::WA_QuitOnClose);

Then in main Cpp file in the main function to add

a.connect( &a,SIGNAL(lastWindowClosed()),&a,SLOT(quit()));

a is an object of type QApplication, which is generated by default.

8.QT adapts to high score screen

QT5. After 6, QT supports automatic adaptation of high score screen. You only need to add the corresponding high score screen configuration before the function construction entry. The configuration is as follows:

//Set the corresponding properties before QApplication construction
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
//Configure and use the corresponding high score screen picture to prevent the picture from becoming very blurred
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

9. Cut off message transmission between parent and child controls

After the corresponding message is accept ed or ignore d in the child control, the parent window will not process the corresponding event message.

void CharacterWidget::mousePressEvent(QMouseEvent *event)
{
    event->accept();
}

Keywords: Qt

Added by jf3000 on Mon, 24 Jan 2022 05:56:24 +0200