文章目录
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,逗号
字符串处理, 大小写转换,字符匹配,路径的拼接