Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QDialog problem with returnPressed which is used in another segment
Forum Updated to NodeBB v4.3 + New Features

QDialog problem with returnPressed which is used in another segment

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for pythonpython
4 Posts 3 Posters 626 Views 1 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.
  • W Offline
    W Offline
    WorldTeacher
    wrote on last edited by
    #1

    I'm creating an application and one function launches a QDialog window. in the window, there is a QLineEdit, a QListWidget and a QButtonBox with the buttons "OK" and "Cancel". The goal of the dialog is to add a single entry to the LineEdit, press "Enter / return", which would add the text from the LineEdit to the ListWidget, and clear the LineEdit. Once the user clicks the "OK" button, the entries from the ListWidget will be returned to the main application.

    However, I have encountered an issue and seem unable to fix it.
    this is a part of the code:

    self.lineEdit.returnPressed.connect(self.add_to_list)
    self.buttonBox.accepted.connect(Dialog.accept)  # type: ignore
    self.buttonBox.rejected.connect(Dialog.reject)  # type: ignore
    
    

    add_to_list takes the text from the line, removes unnecessary whitespaces and puts the text into the list, if the text is not an empty string.

    My issue is, that for some reason PyQt registers the "returnPressed" event for the buttonbox as well as the lineEdit, so as soon as the first entry gets added to the list, the data gets returned, as the buttonbox was triggered. I have tried disabling this behavior by using

    self.buttonBox.accepted.disconnect()
    
    

    which allows me to press return to add text to the listWidget, but disables my ability to close the Dialog.

    How can I set the buttonbox to only be activated by a manual click?

    jsulmJ 1 Reply Last reply
    0
    • W WorldTeacher

      I'm creating an application and one function launches a QDialog window. in the window, there is a QLineEdit, a QListWidget and a QButtonBox with the buttons "OK" and "Cancel". The goal of the dialog is to add a single entry to the LineEdit, press "Enter / return", which would add the text from the LineEdit to the ListWidget, and clear the LineEdit. Once the user clicks the "OK" button, the entries from the ListWidget will be returned to the main application.

      However, I have encountered an issue and seem unable to fix it.
      this is a part of the code:

      self.lineEdit.returnPressed.connect(self.add_to_list)
      self.buttonBox.accepted.connect(Dialog.accept)  # type: ignore
      self.buttonBox.rejected.connect(Dialog.reject)  # type: ignore
      
      

      add_to_list takes the text from the line, removes unnecessary whitespaces and puts the text into the list, if the text is not an empty string.

      My issue is, that for some reason PyQt registers the "returnPressed" event for the buttonbox as well as the lineEdit, so as soon as the first entry gets added to the list, the data gets returned, as the buttonbox was triggered. I have tried disabling this behavior by using

      self.buttonBox.accepted.disconnect()
      
      

      which allows me to press return to add text to the listWidget, but disables my ability to close the Dialog.

      How can I set the buttonbox to only be activated by a manual click?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @WorldTeacher Try https://doc.qt.io/qt-6/qpushbutton.html#default-prop to set the buttons to non-default (by passing false to setDefault).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      0
      • jsulmJ jsulm

        @WorldTeacher Try https://doc.qt.io/qt-6/qpushbutton.html#default-prop to set the buttons to non-default (by passing false to setDefault).

        W Offline
        W Offline
        WorldTeacher
        wrote on last edited by
        #3

        @jsulm Does this work for the QDialogButtonBox as well? I took a quick look at the documentation and added these lines:

        self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDefault(False)
        self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setAutoDefault(False)
        self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Cancel).setDefault(False)
        self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Cancel).setAutoDefault(False)
                
        

        but the Dialog is still closed after I press Return

        SGaistS 1 Reply Last reply
        0
        • W WorldTeacher

          @jsulm Does this work for the QDialogButtonBox as well? I took a quick look at the documentation and added these lines:

          self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setDefault(False)
          self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setAutoDefault(False)
          self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Cancel).setDefault(False)
          self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Cancel).setAutoDefault(False)
                  
          

          but the Dialog is still closed after I press Return

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Can you provide a minimal script that shows this behaviour ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

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