Qt common library
GPL-3.0 License
================================================
AuthorKang [email protected])
Qt common library. include follow functions:
GUI
Tools
cmake functions
Cross-platform, support multiple operating systems
Mac os and IOS, I don't have the corresponding equipment, please compile and test the students with the corresponding equipment.
Create and entry build directory
git clone --recursive https://github.com/KangLin/RabbitCommon.git
cd RabbitCommon
mkdir build
Compile
Use cmake
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
-DQT_DIR=... \
-DQt5_DIR=${QT_ROOT}/lib/cmake/Qt5
cmake --build .
NOTE: If you use MSVC, you need to add -DCMAKE_BUILD_TYPE=Debug, otherwise the following error will occur when compiling Debug:
RabbitCommonTools.obj : error LNK2019: An external symbol that cannot be resolved "int __cdecl qInitResources_translations_RabbitCommon(void)" (?qInitResources_translations_RabbitCommon@@YAHXZ) "void __cdecl g_RabbitCommon_InitResource(void)" (?g_RabbitCommon_InitResource@@YAXXZ)
RabbitCommonTools.obj : error LNK2019: An external symbol that cannot be resolved "int __cdecl qCleanupResources_translations_RabbitCommon(void)" (?qCleanupResources_translations_RabbitCommon@@YAHXZ) "void __cdecl g_RabbitCommon_CleanResource(void)" (?g_RabbitCommon_CleanResource@@YAXXZ)
linux
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=`pwd`/install \
-DCMAKE_BUILD_TYPE=Release \
-DQT_DIR=... \
-DQt5_DIR=${QT_ROOT}/lib/cmake/Qt5
cmake --build . --config Release --target install
windows
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=`pwd`/install ^
-DCMAKE_BUILD_TYPE=Release ^
-DQT_DIR=... ^
-DQt5_DIR=${QT_ROOT}/lib/cmake/Qt5
cmake --build . --config Release --target install
Android
Qt6 or laster
cd build
${Qt6_DIR}/bin/qt-cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --config Release
Or:
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=`pwd`/android-build \
-DCMAKE_TOOLCHAIN_FILE=$Qt6_DIR/lib/cmake/Qt6/qt.toolchain.cmake
cmake --build . --config Release
The host is linux
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=`pwd`/android-build \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="armeabi-v7a with NEON" \
-DANDROID_PLATFORM=android-18 \
-DQT_DIR=... \
-DQt5_DIR=${QT_ROOT}/lib/cmake/Qt5
cmake --build . --config Release --target all
The host is windows
cd build
cmake .. -G"Unix Makefiles" ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_INSTALL_PREFIX=`pwd`/android-build ^
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake ^
-DCMAKE_MAKE_PROGRAM=${ANDROID_NDK}/prebuilt/windows-x86_64/bin/make.exe ^
-DANDROID_PLATFORM=android-18 ^
-DANDROID_ABI=arm64-v8a ^
-DANDROID_ARM_NEON=ON ^
-DQT_DIR=... \
-DQt5_DIR=${QT_ROOT}/lib/cmake/Qt5
cmake --build . --config Release --target all
Parameter Description: https://developer.android.google.cn/ndk/guides/cmake
Install apk to devices
adb install android-build-debug.apk
Qt6 and later
cd build ${Qt6_DIR}/bin/qt-cmake .. -DCMAKE_BUILD_TYPE=Release cmake --build . --config Release
Or:
cmake .. -DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=pwd
/android-build
-DCMAKE_TOOLCHAIN_FILE=$Qt6_DIR/lib/cmake/Qt6/qt.toolchain.cmake
cmake --build . --config Release
Use qmake. (Deprecated, please use CMake for new programs)
cd build
qmake ../RabbitCommon.pro
make install
Compilation notes: Use Qtcreate compile for android in windows
qmake. It may appear that the dependent library could not be found. (Deprecated, please use CMake for new programs)
Don't find D:\Source\build-RabbitCommon-Android_for_armeabi_v7a_Clang_Qt_5_12_4_for_Android_ARMv7-Debug\bin\libRabbitCommon.so move libRabbitCommon.so ..\bin\libRabbitCommon.so process_begin: CreateProcess(NULL, move libRabbitCommon.so ..\bin\libRabbitCommon.so, ...) failed. make (e=2): The system can not find the file specified make[1]: [..\bin\libRabbitCommon.so] Error 2 (ignored)
The reason is that the make program under windows cannot convert the \ in the path. Solution: Use a make program that recognizes the \ in the path, for example: mingw32-make.exe.
cmake
Install notes:
windows If you build app. Qt does not provide openssl dynamic library for copyright reasons, so you must copy the dynamic library of openssl to the installation directory.
linux
sudo apt-get install libssl1.1
if(NOT RabbitCommon_DIR)
set(RabbitCommon_DIR $ENV{RabbitCommon_DIR})
if(NOT RabbitCommon_DIR)
set(RabbitCommon_DIR ${CMAKE_SOURCE_DIR}/../RabbitCommon)
endif()
endif()
if(RabbitCommon_DIR AND EXISTS ${RabbitCommon_DIR}/Src)
message("Use RabbitCommon source code")
add_subdirectory(${RabbitCommon_DIR}/Src ${CMAKE_BINARY_DIR}/RabbitCommon)
else()
find_package(RabbitCommon)
if(NOT RabbitCommon_FOUND)
message("RabbitCommon_DIR is not set. Please use one of the following ways to set it:")
message("1. Set RabbitCommon_DIR to the install prefix of RabbitCommon.")
message("2. Set RabbitCommon_DIR to source code root of RabbitCommon.")
message("2.1 Please download the source code of RabbitCommon from https://github.com/KangLin/RabbitCommon")
message(" ag:")
message(" git clone https://github.com/KangLin/RabbitCommon.git")
message("2.2 Then set cmake variable or environment variable RabbitCommon_DIR to download root directory.")
message(" ag:")
message(" cmake -DRabbitCommon_DIR= ")
message(FATAL_ERROR "RabbitCommon_DIR isn't set.")
endif()
endif()
Use in library mode
cmake
Set RabbitCommon_DIR to specify the root directory of the installed RabbitCommon
find_package(RabbitCommon)
Use the source code directly
Submodule
add_subdirectory(3th_libs/RabbitCommon/Src)
No submodule
Download RabbitCommon to the same directory of the application
git clone https://github.com/KangLin/RabbitCommon.git
Introduced to add_subdirectory this directory
if(NOT RabbitCommon_DIR)
set(RabbitCommon_DIR $ENV{RabbitCommon_DIR})
if(NOT RabbitCommon_DIR)
set(RabbitCommon_DIR ${CMAKE_SOURCE_DIR}/../RabbitCommon)
endif()
endif()
if(DEFINED RabbitCommon_DIR AND EXISTS ${RabbitCommon_DIR}/Src)
add_subdirectory(${RabbitCommon_DIR}/Src ${CMAKE_BINARY_DIR}/RabbitCommon)
else()
message("RabbitCommon_DIR is not set. Please use one of the following ways to set it:")
message("1. Set RabbitCommon_DIR to the install prefix of RabbitCommon.")
message("2. Set RabbitCommon_DIR to source code root of RabbitCommon.")
message("2.1 Please download the source code of RabbitCommon from https://github.com/KangLin/RabbitCommon")
message(" ag:")
message(" git clone https://github.com/KangLin/RabbitCommon.git")
message("2.2 Then set cmake variable or environment variable RabbitCommon_DIR to download root directory.")
message(" ag:")
message(" cmake -DRabbitCommon_DIR= ")
message(FATAL_ERROR "RabbitCommon_DIR isn't set.")
endif()
Add the following code to the CMakeLists.txt file of the project
target_link_libraries(${PROJECT_NAME} RabbitCommon)
Static library
target_compile_definitions(${PROJECT_NAME} PRIVATE RABBITCOMMON_STATIC_DEFINE)
Initialization functions is called at the beginning of main() of the program and after QApplication
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
a.setApplicationName(......);
// Because it depend on QApplication::applicationName()
RabbitCommon::CTools::Instance()->Init();
......
}
Install Authors License ChangeLog files. File name naming rules: Authors.mdLicense.mdChangeLog.md is the default file. The local file naming rule is to add the local name after the default file name. For example: Chinese file: Authors_zh_CN.mdLicense_zh_CN.mdChangeLog_zh_CN.md
SET(OTHER_FILES
${CMAKE_SOURCE_DIR}/License.md
${CMAKE_SOURCE_DIR}/Authors.md
${CMAKE_SOURCE_DIR}/Authors_zh_CN.md
${CMAKE_SOURCE_DIR}/ChangeLog.md
)
INSTALL_FILE(SOURCES ${OTHER_FILES} DESTINATION "." COMPONENT Runtime)
Used in code
QApplication a(argc, argv);
a.setApplicationVersion(RabbitCommon_VERSION);
a.setApplicationName("SerialPortAssistant");
a.setApplicationDisplayName(QObject::tr("SerialPortAssistant"));
#ifdef RABBITCOMMON
CDlgAbout about(this);
about.m_AppIcon = QImage(":/icon/SerialPortAssistant");
about.m_szHomePage = "https://github.com/KangLin/SerialPortAssistant";
#if defined (Q_OS_ANDROID)
about.showMaximized();
about.exec();
#else
about.exec();
#endif
#endif
See: FrmUpdater.h
#ifdef RABBITCOMMON
CFrmUpdater *fu = new CFrmUpdater();
fu->SetTitle(QImage(":/image/SerialPortAssistant"), qApp->applicationDisplayName());
#if defined (Q_OS_ANDROID)
fu->showMaximized();
#else
fu->show();
#endif
#endif
Use CFrmUpdater::GenerateUpdateXml() to generate update xml file, use --help look up parameter
./TasksApp --help
Usage: ./TasksApp [options]
Options:
-h, --help Displays this help.
-v, --version Displays version information.
-f, --file <xml file name> xml file name
--pv <Package version> Package version
-t, --time <Time> Time
-i, --info <Information> Information
-s, --system <Operating system> Operating system
-p, --platform <Platform> Platform
-a, --arch <Architecture> Architecture
-c, --md5 <MD5 checksum> MD5 checksum
-u, --url <Download url> Package download url
--home <Project home url> Project home url
-m, --min <Min update version> Min update version
Add Update/update.xml in project source root directory, then add the url to CFrmUpdater::DownloadFile.
<?xml version="1.0" encoding="UTF-8"?>
<REDIRECT>
<VERSION>v2.2.6</VERSION>
<WINDOWS>
<URL>url</URL>
</WINDOWS>
<LINUX>
<URL>url</URL>
</LINUX>
<LINUX_APPIMAGE>
<URL>url</URL>
</LINUX_APPIMAGE>
<ANDROID>
<URL>url</URL>
</ANDROID>
</REDIRECT>
Internal implementation
QString szCmd = "mkdir";
QStringList paras;
paras << "-p" << "/opt/RabbitCommonAdminAuthoriseTest";
qDebug() << "RabbitCommon::AdminAuthoriser::Instance()->execute(szCmd, paras):"
<< RabbitCommon::AdminAuthoriser::Instance()->execute(szCmd, paras);
Public interface:
RabbitCommon::CTools::executeByRoot("regedit", QStringList());
The program starts automatically
static int InstallStartRun(const QString &szName = QString(),
const QString &szPath = QString(),
bool bAllUser = false);
static int RemoveStartRun(const QString &szName = QString(),
bool bAllUser = false);
static bool IsStartRun(const QString &szName = QString(),
bool bAllUser = false);
Get current login user name:
RabbitCommon::CTools::GetCurrentUser();
Get host name:
RabbitCommon::CTools::GetHostName();