qt调用python添加库_从Qt C造饰者C++中调用Python文件方法

本文介绍如何在Qt C++应用中调用Python文件以利用自定义的日志类Logger,该类包含多种日志级别(如info、debug、warning等),并能将日志保存到文件。在Qt应用中,作者询问如何直接调用Python文件中的特定函数,而不需要引入额外的文件。已知Python库已添加到项目中,使用的Qt版本为5.11。
摘要由CSDN通过智能技术生成

嗨伙计们。

我需要你的帮助。所以,我正在创建一个qt桌面应用程序,我需要从te项目内部的python文件调用函数方法。在

所以,我的python文件:import logging

import os

from time import gmtime, strftime

class Logger():

LOG_TO_FILE=True

@staticmethod

def currentTime():

return strftime("%Y-%m-%d %H:%M:%S", gmtime())

@staticmethod

def log(msg):

print msg

Logger.saveToFile(msg)

@staticmethod

def info(label, msg):

msg = "%s Info : [%s] -> %s" % (Logger.currentTime(), label, msg)

Logger.saveToFile(msg)

print msg

@staticmethod

def debug(label, msg):

msg = "%s Debug : [%s] -> %s" % (Logger.currentTime(), label, msg)

Logger.saveToFile(msg)

print msg

@staticmethod

def warning(label, msg):

msg = "%s Warn : [%s] -> %s" % (Logger.currentTime(), label, msg)

Logger.saveToFile(msg)

print msg

@staticmethod

def critical(label, msg):

msg = "%s Critic : [%s] -> %s" % (Logger.currentTime(), label, msg)

Logger.saveToFile(msg)

print msg

@staticmethod

def error(label, msg):

msg = "%s Error : [%s] -> %s" % (Logger.currentTime(), label, msg)

Logger.saveToFile(msg)

print msg

#log.critical(msg)

@staticmethod

def data(label, msg):

msg = "%s Data : [%s] -> \t%s" % (Logger.currentTime(), label, msg)

Logger.saveToFile(msg)

print msg

@staticmethod

def saveToFile(message):

if Logger.LOG_TO_FILE is True:

if os.path.exists('/var/log/iot-pi') is not True:

os.makedirs('/var/log/iot-pi')

try:

filePath='/var/log/iot-pi/main.log'

f = open(filePath,'a')

f.write(message + '\n')

f.close()

bytes = os.path.getsize(filePath)

if bytes >= 10000000:

os.remove(filePath)

Logger.info("SYSTEM", "Log file removed")

except Exception as inst:

print "save to file error (%s)" % inst

if __name__ == "__main__":

Logger.info("Test", "Message test")

我的主要cpp

^{pr2}$

<> P>有函数获取模式,例如C++中QT后端的警告方法?

我已经在项目中添加了python库。我只需要从py文件中获取def。我不能使用其他文件,我需要调用python文件。在

我用qt5.11。

谢谢。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值