Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change Qpushbutton text
Forum Updated to NodeBB v4.3 + New Features

Change Qpushbutton text

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 658 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sumit
    wrote on last edited by
    #1

    Hi All,

    I am struggling to change the text of the QPushbutton. I wanted to change the text when the slot is called. In my slot function, I am calling another function. The code works if I am not calling the other function from the slot but it does not work if I call the function.
    The button is record_pbutton and the slot is live_speech_to_text

    RATE = 10000
    CHUNK = int(RATE / 10) # 100ms

    class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = " FrenchoLingo"
        self.left, self.top = 10, 10
        self.width, self.height = 640, 640
        try:
            self.ui = uic.loadUi('Frenchuolingo.ui', self)
        except FileNotFoundError as e:
            print(e)
        self.initUI()
        # self.setCentralWidget()
        # self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
    
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)       
        
        self.ui.record_pbutton.setIcon(QtGui.QIcon("./icons8-audio-100.png"))
        self.ui.record_pbutton.setToolTip('Live Speech to Text')
        self.ui.record_pbutton.setEnabled(False)
        self.ui.record_pbutton.setStyleSheet(
            "QPushButton{background-color: orange}")
        self.ui.record_pbutton.clicked.connect(self.live_speech_to_text)
        self.ui.rb_livespeech.setChecked(False)
        self.ui.rb_livespeech.toggled.connect(lambda: self.btnstate(self.ui.rb_livespeech)) 
     
    def btnstate(self, btn):
        self.ui.record_pbutton.setEnabled(True)
        self.ui.record_pbutton.setStyleSheet(
            "QPushButton{background-color: green}")
    
    def live_speech_to_text(self): # For Live Speech to text
        self.ui.record_pbutton.setText("Speak Now")
        self.set_microphone()
    
        # self.ui.record_pbutton.setStyleSheet(
        #     "QPushButton{background-color: green}")
        # self.ui.record_pbutton.setEnabled(True)
    
        # self.ui.livespeech_textbox.append(text)
    
    def set_microphone(self, ):
        # See http://g.co/cloud/speech/docs/languages
        # for a list of supported languages.
        language_code = 'fr-FR'  # a BCP-47 language tag
    
        client = speech.SpeechClient()
        config = types.RecognitionConfig(
            encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
            sample_rate_hertz=RATE,
            language_code=language_code)
        streaming_config = types.StreamingRecognitionConfig(
            config=config,
            interim_results=True)
    
        with MicrophoneStream(RATE, CHUNK) as stream:
    
            # format = stream._audio_interface.get_sample_size(FORMAT)
            audio_generator = stream.generator()
            requests = (types.StreamingRecognizeRequest(audio_content=content)
                        for content in audio_generator)
    
            responses = client.streaming_recognize(streaming_config, requests)
    
            self.listen_print_loop(responses)
    
    def listen_print_loop(self, responses):
        num_chars_printed = 0
        for response in responses:
            if not response.results:
                continue
            result = response.results[0]
            if not result.alternatives:
                continue
            transcript = result.alternatives[0].transcript
            overwrite_chars = ' ' * (num_chars_printed - len(transcript))
            if not result.is_final:
                sys.stdout.write(transcript + overwrite_chars + '\r')
                sys.stdout.flush()
                num_chars_printed = len(transcript)
            else:
                self.ui.livespeech_textbox.append(transcript + overwrite_chars)
                # print(transcript + overwrite_chars)
    
                # Exit recognition if any of the transcribed phrases could be
                # one of our keywords.
                if re.search(r'\b(exit|quit)\b', transcript, re.I):
                    print('Exiting..')
                    break
                num_chars_printed = 0
                return
    

    if "main" == name:
    app = QApplication([])
    myapp = MainWindow()

    myapp.show()
    app.exec_()
    
    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      insert some debug output in set_microphone to see if it is hanging on a particular line in that method.

      If you meet the AI on the road, kill it.

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Sumit said in Change Qpushbutton text:

        The code works if I am not calling the other function

        So that means this function is blocking the event loop
        by not returning control to the caller or simply takes a long time to execute.

        So the buttons text is changed but its never given time to repaint.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved