0

Hy guys. I need your help. So, I'm creating an a qt desktop app, and i need to call function methods from python file, inside te project.

So, my python file:

    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")

my main cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[]) {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    //call Logger.py here ?? it's possible ?

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec(); }

So, there's mode to get functions, for example warning method from Qt backend in C++? I've already added python library to the project. I need "only" to get def from py file. I don't can use another file, i need to call python file.

I use qt 5.11. Thanks.

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110

0 Answers0