修改了This Answer
中的代码class
一切都运行良好但我想通过简单的代码行而不是事件信号在单独的线程中启动方法。
有没有办法执行此操作?
类似于$(document).ready(function() {
$('#example').DataTable( {
initComplete: function () {
this.api().columns().every( function () {
var column = this;
if(column[0]==0){ /* is the first column you want to have dropdown filter */
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.header()))
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
}
} );
}
} );
} );
而不是来自按钮from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import time
class MyWorker(QObject):
@pyqtSlot()
def firstWork(self):
print ('doing first work')
time.sleep(2)
print ('first work done')
@pyqtSlot()
def secondWork(self):
print ('doing second work')
time.sleep(2)
print ('second work done')
class Window(QWidget):
def __init__(self, parent = None):
super(Window, self).__init__()
self.initUi()
self.setupThread()
def initUi(self):
layout = QVBoxLayout()
self.button1 = QPushButton('1')
self.button2 = QPushButton('2')
layout.addWidget(self.button1)
layout.addWidget(self.button2)
self.setLayout(layout)
self.show()
def setupThread(self):
self.thread = QThread()
self.worker = MyWorker()
self.worker.moveToThread(self.thread)
self.button2.clicked.connect(self.worker.secondWork)
self.button1.clicked.connect(self.worker.firstWork)
# Start thread
self.thread.start()
if __name__ == "__main__":
app = QApplication([])
w = Window()
app.exec_()
的事件
(这当然不起作用)
我用我的技能找到的唯一方法是创建隐形按钮并模拟他的点击事件,但当然不是很好的解决方案!
答案 0 :(得分:0)
我通过添加自定义PyQt信号解决了这个问题。
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import time
class MyWorker(QObject):
@pyqtSlot()
def firstWork(self):
print ('doing first work')
time.sleep(2)
print ('first work done')
@pyqtSlot()
def secondWork(self):
print ('doing second work')
time.sleep(2)
print ('second work done')
class Window(QWidget):
Signal = pyqtSignal()
def __init__(self, parent = None):
super(Window, self).__init__()
self.initUi()
self.setupThread()
def initUi(self):
layout = QVBoxLayout()
self.button1 = QPushButton('1')
self.button2 = QPushButton('2')
layout.addWidget(self.button1)
layout.addWidget(self.button2)
self.setLayout(layout)
self.show()
def setupThread(self):
self.thread = QThread()
self.worker = MyWorker()
self.worker.moveToThread(self.thread)
self.Signal.connect(self.worker.secondWork)
self.Signal.connect(self.worker.firstWork)
self.thread.start()
self.Signal.emit()
if __name__ == "__main__":
app = QApplication([])
w = Window()
app.exec_()