Python脚本 编译Qt工程


1. 基本信息

1.1 判断QT版本
import sys
if __name__ == '__main__':
    print(sys.version_info)
    if(sys.version_info.major != 2):
        print('当前版本为: ', sys.version)

输出:
sys.version_info(major=3, minor=8, micro=2, releaselevel=‘final’, serial=0)
当前版本为: 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]

1.2 获取参数
import sys
if __name__ == '__main__':
   print(sys.argv)

输出:
python Untitled-1.py aa bb
[’./Untitled-1.py’, ‘aa’, ‘bb’]

2. 数据类型

2.1 类枚举
class BuildType():
    win = 10
    win_debug = 11
    win_release = 12
    default = 10000

if __name__ == '__main__':
   aa = BuildType.win
   print(BuildType.win)
   print(aa)

输出:
10
10

2.2 类的继承
#!/usr/bin/env python3
import sys
class BuildType_X:
    def __init__(self, buildType,name):
        self.buildType = buildType
        self.name = name
    def getName(self):
        return self.name
class WindowBuildType(BuildType_X):
    def __init__(self, buildType):
        self.name = "window"
        super().__init__(buildType,self.name)

class LinuxBuildType(BuildType_X):
    def __init__(self, buildType):
        self.name = "linux"
        super().__init__(buildType,self.name)
if __name__ == '__main__':
    win = WindowBuildType(None)
    linux = LinuxBuildType(None)
    print(win.getName())
    print(linux.getName())

输出:
window
linux

2.3 Map 的使用
#!/usr/bin/env python3
import sys
class BuildType():
    win = 10
    win_debug = 11
    win_release = 12
    default = 10000
    
BuildTypeMap = {
    '-win': BuildType.win,
    '-win_debug': BuildType.win_debug,
    '-win_release': BuildType.win_release,
    '-default': BuildType.default
}
def getBuildType(paramter):
    return BuildTypeMap.get(paramter, BuildType.default) #默认值为default
if __name__ == '__main__':
    if(len(sys.argv) > 1):
        paramter = sys.argv[1]
        type = getBuildType(paramter)
        print('paramter:', paramter)
        print('type:', type)

输出
c:/Users/hp/Desktop/Untitled-1.py -win
paramter: -win
type: 10

c:/Users/hp/Desktop/Untitled-1.py -aaaaaa
paramter: -aaaaaa
type: 10000

3. 配置文件读写

3.1 生成配置文件
#!/usr/bin/env python3
import os
import configparser

_configPath = None

class BuildType():
    win = 10
    win_debug = 11
    win_release = 12
    default = 10000

def initconfigPath():
    global _configPath
    #获取py文件的当前路径
    if(_configPath == None):
        proDir = os.path.split(os.path.realpath(__file__))[0]
        _configPath = os.path.join(proDir, "config.ini")

def createIniFile(buildType):
    config = configparser.ConfigParser()
    # 生成配置文件
    strType = str(buildType)
    config.add_section(strType)
    config[strType]['buildType'] = strType
    config[strType]['project'] = r'./test.pro'
        
    # 写入配置文件
    with open(_configPath, 'w') as configfile:
        config.write(configfile)
    pass


if __name__ == '__main__':
	initconfigPath()
    createIniFile(BuildType.win)

输出 [config.ini]
[10]
buildtype = 10
project = ./test.pro

3.2 读取配置文件
#!/usr/bin/env python3
import os
import configparser
_configPath = None

class BuildType():
    win = 10
    win_debug = 11
    win_release = 12
    default = 10000

def initconfigPath():
    global _configPath
    if(_configPath == None):
        proDir = os.path.split(os.path.realpath(__file__))[0]
        _configPath = os.path.join(proDir, "config.ini")

def readBuildInfo():
    config = configparser.ConfigParser()
    config.read(_configPath)
    sections = config.sections()
    buildTypes = []
    for section in sections:
        buildType = config[section]['buildType']
        project = config[section]['project']
        buildTypes.append({
            'buildType': buildType,
            'project': project
        })
    return buildTypes

if __name__ == '__main__':
    initconfigPath()
    print(readBuildInfo())

输出
[{‘buildType’: ‘10’, ‘project’: ‘./test.pro’}]

4. 编译准备

4. 1 执行命令
#!/usr/bin/env python3
import os
import subprocess
def check_error(line):
    # 自己的判断逻辑
    return True

def process(length, strhelp):
    print('{0} 进度  完成 {1}'.format(strhelp, length))

def execute(cmd, strhelp, showInfo=False):
    P = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    length = 0
    while P.poll() is None:
        line = P.stdout.readline().decode('gb2312', 'ignore')
        if(showInfo or check_error(line)):
            print(line)  # 出错打印当前行
        else:
            length = length + 1
            process(length, strhelp)  # 正常的话
    return True if P.returncode == 0 else False

if __name__ == '__main__':
    execute(r'notepad.exe', 'qmake')

输出 【打开记事本】

4.2 qt的编译命令

window debug
“qmake.exe” “D:\project\build\test.pro” -spec win32-msvc “CONFIG+=debug” “CONFIG+=qml_debug”
“jom.exe” -f D:/project/build/Makefile qmake_all

window release
“qmake.exe” “D:\project\build\test.pro” -spec win32-msvc “CONFIG+=qtquickcompiler”
“jom.exe” -f D:/project/build/Makefile qmake_all

Linux debug
“qmake” /home/adimn/project/build/test.pro -spec linux-g++ “CONFIG+=debug” “CONFIG+=qml_debug”
“make” -f /home/adimn/project/build/Makefile qmake_all

Linux release
“qmake” /home/adimn/project/build/test.pro -spec linux-g++ “CONFIG+=qtquickcompiler”
“make” -f /home/adimn/project/build/Makefile qmake_all

总结

还有些不高兴写了… 2022.01.14
🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️🏃‍♂️
环境变量
更新代码
打印时,拼接字符的几个方法 +号,format,逗号
字符串处理, 大小写转换,字符匹配,路径的拼接

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt是一个跨平台的应用程序开发框架,常用于C++开发。Python是一种动态类型的脚本语言。混合编程指的是在一个项目中同时使用Qt/C++和Python进行开发。 混合编程的好处是可以充分发挥QtPython各自的优势。Qt/C++可以提供高性能和可靠性,适用于底层开发和系统级编程。Python则提供了简洁易懂、高效编程以及大量的第三方库,适用于快速开发和原型设计。 混合编程的打包过程可以分为以下几个步骤: 首先,需要安装相应的编译工具和开发环境,如Qt、C++编译器和Python解释器。确定使用的Qt版本与Python版本兼容。 其次,需要为C++部分编写Qt代码,并将其编译成dll或so动态链接库,以供Python调用。这需要使用Qt提供的相关工具和库进行编译和链接。 然后,使用Python的相关库(如PyQt或PySide)来调用C++部分的Qt代码,并将其与Python代码结合起来。这样,就可以实现Qt界面与Python逻辑的交互。 最后,将项目打包成可执行文件、二进制文件或安装包。这可以使用Qt提供的打包工具,如Qt Installer Framework,或者使用第三方工具和脚本来完成。 需要注意的是,在混合编程和打包过程中,需要仔细处理Qt的信号与槽机制与Python的回调机制之间的交互,以确保二者能够正常工作。 总之,Qt C++和Python的混合编程可以充分利用两者的优势,打包则需要注意兼容性和交互的处理。这种方式可以更灵活地开发应用程序,并能够适应不同的需求和平台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值