简体   繁体   English

QtWebKit中的JavaScript和异步方法

[英]JavaScript and async methods in QtWebKit

I am developing desktop software based on: 我正在开发桌面软件基于:

  • Python: PySide (WebKit) Python:PySide(WebKit)
  • JavaScript: AngularJS JavaScript:AngularJS

Now, I need implement async feature: 现在,我需要实现异步功能:

  • while uploading images from FTP (Python part) I need to show some uploader on UI (HTML+JS) 从FTP(Python部分)上传图像时,我需要在UI上显示一些上传器(HTML + JS)

Need your advices about the elegant solution. 需要您对优雅解决方案的建议。

Very simple solution : 很简单的解决方案

  1. save some flags in Python dictionary 在Python字典中保存一些标志
  2. request value of this flags in JS by timer :) 通过计时器在JS中请求这个标志的值:)

However, I need to hard code the time and once I have long operation such as downloading files from FTP, its issue for me 但是,我需要硬编码时间,一旦我有长时间的操作,如从FTP下载文件,它的问题对我来说

Code of the solution: 解决方案代码:

templateManager.js templateManager.js

function _loadNewTemplates() {
        var deferred = $.Deferred();

        asyncManager.run($.proxy( _loadNewTemplatesCallback, null, deferred ), "get_new_templates_list" );

        return deferred;
    }

    function _loadNewTemplatesCallback ( deferred, response ) {
        template.newList = response.result;
        deferred.resolve();
    }

app_mamager.py app_mamager.py

    @QtCore.Slot(int, result=str)
    def get_new_templates_list(self, id):
       thread.start_new_thread(get_new_templates_list,
                                    (id,))

utils.py utils.py

def get_new_templates_list(id):
   config.set_response(id, json.dumps({'result': _get_new_templates_list(), 'error': None}, MyEncoder))


def _get_new_templates_list():
    request = {'category': PRODUCT_CODE}
    response = requests.post(URL,
                             data=request,
                             headers=HEADERS)
    return json.loads(response.text)

config.py config.py

def set_response(id, request):
    '''
    Put pair (id, request) to the internal dictionary
    :param id - int, request id:
    :param request - string, request string:
    :return:
    '''
    global _async_request
    _async_request[id] = request

def get_response(id):
    '''
    Returns request or empty string if request does not exist
    :param id - int
    :return - string, request string:
    '''
    global _async_request
    if _async_request.has_key(id):
        return _async_request.pop(id)
    else:
        return ''

Here is what you can do: 这是你可以做的:

  1. retrieve the file size with stat / stat.ST_SIZE , 使用stat / stat.ST_SIZE检索文件大小,
  2. decide on a chunk size, 决定chunk大小,
  3. call PySide.QtGui.QProgressBar.setRange() with the correct range, which is 0 to number of blocks ( filesize / ftp transfer chunk size ) for binary files or number of lines for text files. 使用正确的范围调用PySide.QtGui.QProgressBar.setRange() ,对于二进制文件或文本文件的行数,为0到块数(filesize / ftp传输块大小)。
  4. pass a callback to ftplib.FTP.storbinary() or ftplib.FTP.storlines() so that each block / line transferred increases your progress bar accordingly with PySide.QtGui.QProgressBar.setValue() 将回调传递给ftplib.FTP.storbinary()ftplib.FTP.storlines()以便传输的每个块/行相应地使用PySide.QtGui.QProgressBar.setValue()相应地增加进度条

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM