如何在没有qt信号的情况下从qt应用程序获取信号到主应用程序?

问题描述 投票:0回答:1

我需要帮助,我正在开发一个智能家居应用程序,我需要将信号从 qt 应用程序中的按钮传输到另一个模块。 在主控模块中,会进行各种计算以及与语音控制的匹配等。 我使用缓存测试了该选项,但我使用一种信号准确性方法或仅在任何转换中设置 True/False 失败了。

除了Qt Signal还有其他方法吗?

enter image description here

我这里有 2 个模块

Modul 1 QT_APP

import sys

from PySide6.QtWidgets import QApplication, QMainWindow
from ui_form import Ui_MainWindow


class MainWindow(QMainWindow):
    def __init__(self, parent=None)   
        super().__init__(parent)
        
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        self.ui.B1_button.clicked.connect(self.button1_on)
        
        def button1_on(self):
            print("button 1 click on")
        
        

def GUI_App():
    app = QApplication(sys.argv)
    widget = MainWindow()
    widget.show()
    sys.exit(app.exec())
Modul 2 Master

from modul_Qt import GUI_App , MainWindow
from PyQt5.QtCore import QObject 
import threading
import time



class Master_app(QObject):
    def __init__(self):
        print("start_master_app")

        self.calculate_button()
  
    def calculate_button(self):
        while True:
        
            button_click_on = # I need this signal from button 
            
            if button_click_on == 1: 
                print("strat socket and next...")
            
            
            time.sleep(2)
  

if __name__ == "__main__":
    
    t1 = threading.Thread(target=GUI_App())
    
    app = Master_app()
    t1.start()

我尝试了Qt Signal方法、Cache和file.txt。这有效,但我不想走那条路。

缓存方法看起来是一个非常好的方法,但是当我将该方法添加到程序中时,数据并没有传输给我。

示例

modul 1 

import sys

from PySide6.QtWidgets import QApplication, QMainWindow
from ui_form import Ui_MainWindow

from cache_manager import set_value_variable


class MainWindow(QMainWindow):
    def __init__(self, parent=None)   
        super().__init__(parent)
        
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        self.key = "key1"
        self.varible = True
        
        self.ui.B1_button.clicked.connect(self.button1_on)
        
        def button1_on(self):
            set_value_variable(self.key, self.varible)
            print("button 1 click on")
        
        

def GUI_App():
    app = QApplication(sys.argv)
    widget = MainWindow()
    widget.show()
    sys.exit(app.exec())

modul 2


from modul_Qt import GUI_App , MainWindow
from PyQt5.QtCore import QObject 
import threading
import time

from cache_manager import get_cached_value, set_value


class Master_app(QObject):
    def __init__(self):
        print("start_master_app")

        self.calculate_button()
        
        self.variable = self.get_cached_value()
        
    def get_cached_value(self):
        return get_cached_value(self.key)
    
    def calculate_button(self):
        while True:
        
            button_click_on = self.variable  # I need this signal from button 
            
            if button_click_on == 1: 
                print("strat socket and next...")
            
            
            time.sleep(2)
  

if __name__ == "__main__":
    
    t1 = threading.Thread(target=GUI_App())
    
    app = Master_app()
    t1.start()

modul 3

from cachetools import cached, LRUCache

cache = LRUCache(maxsize=100)

@cached(cache)
def get_value(key):
    return f"Value for key {key}"


def set_value_variable(key, value):
    cache[key] = value

def get_cached_value(key):
    return get_value(key)
python qt pyqt pyqt5 cache-control
1个回答
0
投票

抱歉,我使用文件在应用程序之间传输信号。谢谢大家..

Qt 模块

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
    
        self.shared_memory_filename = 'MySharedMemory.dat'
    
   
        with open(self.shared_memory_filename, 'wb') as file:
            file.write(b'None')
    
   
        button = QPushButton("click here", self)
        button.setGeometry(10, 10, 80, 30)
    
        button.clicked.connect(self.on_button_click)

        def on_button_click(self):
           with open(self.shared_memory_filename, 'wb') as file:
           file.write(b'button_click_ON')
   

def GUI_App():
   app = QApplication(sys.argv)
   widget = MainWindow()
   widget.show()
   sys.exit(app.exec_())

模块大师应用程序

import threading
import time
from Qt_App import GUI_App

class Master_app():
    def __init__(self):
         print("start_master_app")

         self.shared_memory_filename = 'MySharedMemory.dat'
    
         self.calculate_button()

    def calculate_button(self): 
         while True:
           
              with open(self.shared_memory_filename, 'rb') as file:
                   data = file.read()
        
              print(data)
        
              if data == b'button_click_ON':
                   print("Button click!")
                   with open(self.shared_memory_filename, 'wb') as file:
                        file.write(b'None')
        
          time.sleep(0.1)

if __name__ == "__main__":
      t1 = threading.Thread(target=GUI_App)
      t1.start()
      app = Master_app()
© www.soinside.com 2019 - 2024. All rights reserved.