2013. 9. 10.

Load non utf-8 file and display QTextEdit at pyhon



1) declare coding

    # -*- coding: utf-8 -*-

2) load file  ( let's asume file is  'euc-kr')
   
     fileData = f.read()  # read from file


3) decode  to unicode
    unicodedata = unicode(fileData,"euc-kr")

4) encode 'utf-8'

    utf8data = unicodedata.encode("utf-8")

5) set text
    editor.setPlainText(utf8data)  # editor is QTextEdit


** summary ** : converting flow

    EUC-KR --->   UNICODE --> UTF-8

Enhanced by Zemanta

2013. 9. 6.

Sending Data Between Threads by using QThread, at pyhon (QT version)

##  Do Asynchronously and return result to main Thread
##
##  step 1:  Define thread class
##  step 2:  Call 'emit' to send notify+data to Main thread  
##  step 3:  Connect event and method
##  step 4:  Implement method 


from time import sleep
from PyQt4 import QtCore

class ParentThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
        self.exiting = False

    def __del__(self):
        self.exiting = True     # used for exit in whilie loop.
        self.wait()



class ChildThread(ParentThread):

    def __init__(self):
        ParentThread.__init__(self)
 
    def run(self): # this method does not use loop code, it run once.
        
        # get server version data
        conn = httplib.HTTPConnection("webserveraddress")
        conn.request("GET", "/data.htm")
        connResponse = conn.getresponse()  # reason, status
        resData = connResponse.read()

        #send message + event
        # resdata is passed as parameter of onComplete
        self.emit( QtCore.SIGNAL('onComplete(QString)') , resData)  



class MainClass:

    def __init__(self, parent=None):    
        self.childThread= ChildThread()

       #connect event
        self.connect( self.childThread, QtCore.SIGNAL('onComplete(QString)'), self.onComplete)
        
        #start Thread
        self.versionThread.start()  

   def onComplete(self, resData):
        print resData


        


Enhanced by Zemanta