2012-12-02 475 views
1

我試圖從PyQt中的非主線程發送信號,但我不知道什麼是做錯了!當我執行程序失敗,出現此錯誤:QObject :: connect:無法排隊'QTextCursor'類型的參數

QObject::connect: Cannot queue arguments of type 'QTextCursor' 
(Make sure 'QTextCursor' is registered using qRegisterMetaType().) 

這裏是我的代碼:

class Sender(QtCore.QThread): 
     def __init__(self,q): 
      super(Sender,self).__init__() 
      self.q=q 
     def run(self): 

      while True: 
       pass 
       try: line = q.get_nowait() 
      # or q.get(timeout=.1) 
       except Empty: 
        pass 
       else: 
        self.emit(QtCore.SIGNAL('tri()')) 
class Workspace(QMainWindow, Ui_MainWindow): 
    """ This class is for managing the whole GUI `Workspace'. 
     Currently a Workspace is similar to a MainWindow 
    """ 

    def __init__(self): 
try: 
      from Queue import Queue, Empty 
     except ImportError: 
      while True: 
    #from queue import Queue, Empty # python 3.x 
       print "error" 

     ON_POSIX = 'posix' in sys.builtin_module_names 

     def enqueue_output(out, queue): 
      for line in iter(out.readline, b''): 
       queue.put(line) 
      out.close() 

     p= Popen(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/',stdout=PIPE, shell=True, bufsize= 4024) 
     q = Queue() 
     t = threading.Thread(target=enqueue_output, args=(p.stdout, q)) 
      t.daemon = True # thread dies with the program 
     t.start() 
     self.sender= Sender(q) 
     self.connect(self.sender, QtCore.SIGNAL('tri()'), self.__action_About) 
     self.sender.start() 

我覺得我送參數給線程的方式是錯誤的... 我需要知道如何將參數發送到線程,在我的情況下,我需要發送q到工作線程。

+0

可能重複[如何參數發送到的QThread(http://stackoverflow.com/questions/13675140/how-to-send-parameters-to- a-qthread) – ekhumoro

+0

你已經提出過這個問題,而且你還沒有提供任何實際使用QTextCursor的代碼。 –

+4

提問者很可能實際上並未直接使用QTextCursor,而是使用不是GUI線程的線程的GUI代碼。試圖這樣做似乎導致這種由Qt內部代碼引起的錯誤,例如,爲QTextEdit.append()。請參閱:http://stackoverflow.com/questions/2104779/qobject-qplaintextedit-multithreading-issues – svk

回答

2

Make sure 'QTextCursor' is registered using qRegisterMetaType().

您是否嘗試過使用qRegisterMetaType函數?

官方手冊says

The class is used as a helper to marshall types in QVariant and in queued signals and slots connections. It associates a type name to a type so that it can be created and destructed dynamically at run-time. Declare new types with Q_DECLARE_METATYPE() to make them available to QVariant and other template-based functions. Call qRegisterMetaType() to make type available to non-template based functions, such as the queued signal and slot connections.

+6

OP正在使用PyQt,它不會封裝'qRegisterMetaType'。 – ekhumoro

相關問題