def run(): """ Create launch.json with current PID """ cwd = os.path.dirname(__file__) Path(os.path.join(cwd, '.vscode/')).mkdir(exist_ok=True) launch = { "version": "0.2.0", "configurations": [ { "name" : "Pythonic: Attach", "type" : "python", "request" : "attach", "processId" : os.getpid(), "justMyCode": False, "cwd" : cwd } ] } with open(os.path.join(cwd + '/.vscode/launch.json'), 'w') as file: json.dump(launch, file, indent=4) timer = QTimer() timer.start(500) timer.timeout.connect(lambda : None) app = QCoreApplication(sys.argv) ex = MainWorker(app) ex.start(sys.argv) app.exec_()
class ProducerConsumer(unittest.TestCase): '''Basic test case for producer-consumer QThread''' def setUp(self): #Create fixtures self.app = QCoreApplication([]) def tearDown(self): #Destroy fixtures del self.app def finishCb(self): #Quits the application self.app.exit(0) def testProdCon(self): #QThread producer-consumer example bucket = Bucket() prod = Producer(bucket) cons = Consumer(bucket) prod.start() cons.start() QObject.connect(prod, SIGNAL('finished()'), self.finishCb) QObject.connect(cons, SIGNAL('finished()'), self.finishCb) self.app.exec_() prod.wait(50) cons.wait(50) self.assertEqual(prod.production_list, cons.consumption_list)
class ProducerConsumer(unittest.TestCase): '''Basic test case for producer-consumer QThread''' def setUp(self): #Create fixtures self.app = QCoreApplication([]) def tearDown(self): #Destroy fixtures del self.app def finishCb(self): #Quits the application self.app.exit(0) def testProdCon(self): #QThread producer-consumer example bucket = Bucket() prod = Producer(bucket) cons = Consumer(bucket) prod.start() cons.start() QObject.connect(prod, SIGNAL('finished()'), self.finishCb) QObject.connect(cons, SIGNAL('finished()'), self.finishCb) self.app.exec_() prod.wait() cons.wait() self.assertEqual(prod.production_list, cons.consumption_list)
def test_controller_and_worker_good(self): app = QCoreApplication(sys.argv) controller = Controller() controller.worker.finished.connect(QCoreApplication.quit) with patch.object(controller, "on_worker_result") as on_result: controller.start() app.exec_() self.assertEqual(20, len(on_result.mock_calls))
def run(): # Create home path (if not already existing) home_path = Path.home() / 'Pythonic' home_path.mkdir(exist_ok=True) # Create log path (if not already existing) logPath = home_path / 'log' logPath.mkdir(exist_ok=True) # Create directory for executables (if not already existing) execPath = home_path / 'executables' execPath.mkdir(exist_ok=True) # Create directory for vs code configuration (if not already existing) vsCodepath = execPath / '.vscode' vsCodepath.mkdir(exist_ok=True) # Append executables folder to module search path sys.path.append(str(execPath)) """ Create launch.json with current PID in hte libraries installation path """ libPath = os.path.dirname(__file__) Path(os.path.join(libPath, '.vscode/')).mkdir(exist_ok=True) launch = { "version": "0.2.0", "configurations": [{ "name": "Pythonic: Attach", "type": "python", "request": "attach", "justMyCode": False, "connect": { "host": "localhost", "port": 5678 } }] } """ Create launch.json with current PID in the user executables path """ with open(vsCodepath / 'launch.json', 'w') as file: json.dump(launch, file, indent=4) app = QCoreApplication(sys.argv) ex = MainWorker(app) ex.start(sys.argv) app.exec_()
def run(): timer = QTimer() timer.start(500) timer.timeout.connect(lambda: None) app = QCoreApplication(sys.argv) ex = MainWorker(app) #signal.signal(signal.SIGINT, ex.exitApp) ex.start(sys.argv) app.exec_()
def fix_pyside_exec(namespace): if namespace.get("__name__") == "AnyQt.QtWidgets": from PySide2.QtWidgets import QApplication, QDialog, QMenu if "exec" not in QApplication.__dict__: QApplication.exec = lambda self: QApplication.exec_() if not hasattr(QDialog, "exec"): QDialog.exec = lambda self: QDialog.exec_(self) if not hasattr(QMenu, "exec"): QMenu.exec = lambda self: QMenu.exec_(self) if namespace.get("__name__") == "AnyQt.QtGui": from PySide2.QtGui import QGuiApplication, QDrag if "exec" not in QGuiApplication.__dict__: QGuiApplication.exec = lambda self: QGuiApplication.exec_() if not hasattr(QDrag, "exec"): QDrag.exec = (lambda self, *args, **kwargs: QDrag.exec_( self, *args, **kwargs)) elif namespace.get("__name__") == "AnyQt.QtCore": from PySide2.QtCore import QCoreApplication, QEventLoop, QThread if not hasattr(QCoreApplication, "exec"): QCoreApplication.exec = lambda self: QCoreApplication.exec_() if not hasattr(QEventLoop, "exec"): QEventLoop.exec = (lambda self, *args, **kwargs: QEventLoop.exec_( self, *args, **kwargs)) if not hasattr(QThread, "exec"): QThread.exec = lambda self: QThread.exec_(self) elif namespace.get("__name__") == "AnyQt.QtPrintSupport": from PySide2.QtPrintSupport import QPageSetupDialog, QPrintDialog if "exec" not in QPageSetupDialog.__dict__: QPageSetupDialog.exec = lambda self: QPageSetupDialog.exec_(self) if "exec" not in QPrintDialog.__dict__: QPrintDialog.exec = lambda self: QPrintDialog.exec_(self)
def using_move_to_thread(): app = QCoreApplication([]) objThread = QThread() obj = SomeObject() obj.moveToThread(objThread) obj.finished.connect(objThread.quit) objThread.started.connect(obj.long_running) objThread.finished.connect(app.exit) objThread.start() sys.exit(app.exec_())
def testEmitOutsideThread(self): global thread_run app = QCoreApplication([]) source = Source() thread = ThreadJustConnects(source) QObject.connect(thread, SIGNAL('finished()'), lambda: app.exit(0)) thread.start() while not thread_run: pass source.emit_sig() app.exec_() thread.wait() self.assertTrue(thread.target.called)
def testEmitOutsideThread(self): global thread_run app = QCoreApplication([]) source = Source() thread = ThreadJustConnects(source) QObject.connect(thread, SIGNAL('finished()'), lambda: app.exit(0)) thread.start() while not thread_run: pass source.emit_sig() app.exec_() thread.wait() self.assert_(thread.target.called)
class DnsLookupTestCase(unittest.TestCase): '''Test case for QDnsLookup''' def setUp(self): self._app = QCoreApplication([]) self._lookup = QDnsLookup(QDnsLookup.ANY, 'www.qt.io') self._lookup.finished.connect(self._finished) def tearDown(self): del self._lookup def _finished(self): if self._lookup.error() == QDnsLookup.NoError: nameRecords = self._lookup.canonicalNameRecords() if nameRecords: print(nameRecords[0].name()) self._app.quit() def testLookup(self): self._lookup.lookup() self._app.exec_()
class HttpSignalsCase(unittest.TestCase): '''Test case for bug #124 - readDatagram signature QUdpSocket.readDatagram must return a tuple with the datagram, host and port, while receiving only the max payload size.''' def setUp(self): #Acquire resources self.called = False self.app = QCoreApplication([]) self.socket = QUdpSocket() self.server = QUdpSocket() self.server.bind(QHostAddress(QHostAddress.LocalHost), 45454) def tearDown(self): #Release resources del self.socket del self.server del self.app def sendPackage(self): addr = QHostAddress(QHostAddress.LocalHost) self.socket.writeDatagram('datagram', addr, 45454) def callback(self): while self.server.hasPendingDatagrams(): datagram, host, port = self.server.readDatagram( self.server.pendingDatagramSize()) self.called = True self.app.quit() def testDefaultArgs(self): #QUdpSocket.readDatagram pythonic return # @bug 124 QObject.connect(self.server, SIGNAL('readyRead()'), self.callback) self.sendPackage() self.app.exec_() self.assertTrue(self.called)
class HttpSignalsCase(unittest.TestCase): '''Test case for bug #124 - readDatagram signature QUdpSocket.readDatagram must return a tuple with the datagram, host and port, while receiving only the max payload size.''' def setUp(self): #Acquire resources self.called = False self.app = QCoreApplication([]) self.socket = QUdpSocket() self.server = QUdpSocket() self.server.bind(QHostAddress(QHostAddress.LocalHost), 45454) def tearDown(self): #Release resources del self.socket del self.server del self.app def sendPackage(self): addr = QHostAddress(QHostAddress.LocalHost) self.socket.writeDatagram('datagram', addr, 45454) def callback(self): while self.server.hasPendingDatagrams(): datagram, host, port = self.server.readDatagram(self.server.pendingDatagramSize()) self.called = True self.app.quit() def testDefaultArgs(self): #QUdpSocket.readDatagram pythonic return # @bug 124 QObject.connect(self.server, SIGNAL('readyRead()'), self.callback) self.sendPackage() self.app.exec_() self.assert_(self.called)
def headless_main(args): """ Executes a project using :class:`QCoreApplication`. Args: args (argparser.Namespace): parsed command line arguments. Returns: int: exit status code; 0 for success, everything else for failure """ application = QCoreApplication(sys.argv) startup_event_type = QEvent.Type(QEvent.registerEventType()) task = ExecuteProject(args, startup_event_type, application) application.postEvent(task, QEvent(startup_event_type)) return application.exec_()
def using_q_runnable(): app = QCoreApplication([]) runnable = Runnable() QThreadPool.globalInstance().start(runnable) sys.exit(app.exec_())
from PySide2.QtCore import QCoreApplication from .api.qgateway import QGateway import sys app = QCoreApplication(sys.argv) gateway = QGateway() gateway.new_event.connect(lambda x: print(str(x)[0:200], "...")) sys.exit(app.exec_())
# 构造函数 def __init__(self, name, parent=None): # 调用父类的构造函数,组织对象树 super().__init__(parent) self.name = name # 定义槽,需要用result指定返回值类型(没有返回值可以省略) @QtCore.Slot(str, str, result=None) # 参数类型为str,str @QtCore.Slot(str, int, result=None) # 或 str,int def hear(self, speaker, text): print(self.name, "hear", speaker, "speak :", text) if __name__ == "__main__": # 创建应用 app = QCoreApplication(sys.argv) tom = People("Tom") jerry = People("Jerry") # 绑定信号和槽 tom.speak.connect(jerry.hear) # 默认第一组参数类型 jerry.speak[str, int].connect(tom.hear) # 指定参数类型 # 发送信号 tom.speak.emit(tom.name, "How old are you?") # 默认第一组参数类型 jerry.speak[str, int].emit(jerry.name, 18) # 指定参数类型 # 启动应用 app.exec_()
def using_q_thread(): app = QCoreApplication([]) thread = AThread() thread.finished.connect(app.exit) thread.start() sys.exit(app.exec_())