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. PCharm, PyQt5 and code completion
QtWS25 Last Chance

PCharm, PyQt5 and code completion

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 1.1k Views
  • 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.
  • V Offline
    V Offline
    valterhome
    wrote on 10 Jun 2023, 13:21 last edited by
    #1

    I'm fairly new to Python and sorry if this might be a trivial question, but I've searched the net without success.
    I created a form.ui with Qt Creator putting some components into it, for example lineEdit and horizontalSlider.
    Now from my Python code I search and find the component with

    lineEdit = self.findChild(QtWidgets.QLineEdit, "lineEdit_2")
    

    but when I type lineEdit followed by a dot the code completion opens but it is missing some functions, for example setText().
    I know I can write it anyway and it works fine. But why is it not shown?
    Also, in other parts of the code I interface with some QThreads through signals, and for example in this line:

    self.pushButton.clicked.connect(self.start_threads)
    

    PCharm does not suggest connect and reports it as not found, even if after works.

    When you're just starting out, it would be important to have all the tips right so you can experiment with new features.
    Anyone have any suggestions? I've already tried several things, editor preferences settings, invalidated cache... but nothing has changed.

    Thanks

    J 1 Reply Last reply 10 Jun 2023, 14:44
    0
    • V valterhome
      10 Jun 2023, 13:21

      I'm fairly new to Python and sorry if this might be a trivial question, but I've searched the net without success.
      I created a form.ui with Qt Creator putting some components into it, for example lineEdit and horizontalSlider.
      Now from my Python code I search and find the component with

      lineEdit = self.findChild(QtWidgets.QLineEdit, "lineEdit_2")
      

      but when I type lineEdit followed by a dot the code completion opens but it is missing some functions, for example setText().
      I know I can write it anyway and it works fine. But why is it not shown?
      Also, in other parts of the code I interface with some QThreads through signals, and for example in this line:

      self.pushButton.clicked.connect(self.start_threads)
      

      PCharm does not suggest connect and reports it as not found, even if after works.

      When you're just starting out, it would be important to have all the tips right so you can experiment with new features.
      Anyone have any suggestions? I've already tried several things, editor preferences settings, invalidated cache... but nothing has changed.

      Thanks

      J Offline
      J Offline
      JonB
      wrote on 10 Jun 2023, 14:44 last edited by JonB 6 Oct 2023, 14:44
      #2

      @valterhome
      In a word, to get a good editing experience in PyCharm you do not want to go down the findChild(QtWidgets.QLineEdit) route. You want to do what you would do from Qt Creator, where you run something like pyside-uic on the .ui file saved from Designer to generate a new .py file from it. That will contain classes/variables for all your UI elements and allow PyCharm to determine their types for use when code completing. You want to use the Option A approach in https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class.

      As for the connect(). ISTR PyCharm has an option where you tell it whether you are using PySide or PyQt (or nothing), have you found this?

      V 2 Replies Last reply 10 Jun 2023, 16:17
      1
      • J JonB
        10 Jun 2023, 14:44

        @valterhome
        In a word, to get a good editing experience in PyCharm you do not want to go down the findChild(QtWidgets.QLineEdit) route. You want to do what you would do from Qt Creator, where you run something like pyside-uic on the .ui file saved from Designer to generate a new .py file from it. That will contain classes/variables for all your UI elements and allow PyCharm to determine their types for use when code completing. You want to use the Option A approach in https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class.

        As for the connect(). ISTR PyCharm has an option where you tell it whether you are using PySide or PyQt (or nothing), have you found this?

        V Offline
        V Offline
        valterhome
        wrote on 10 Jun 2023, 16:17 last edited by
        #3

        @JonB Sorry, i didn't say that, but i use pyqt5 and and it seems that the link you gave me refers to pyside. I actually have no preference between the two, but I tried installing pyside2 and PCharm raises an error.
        As for the option to tell PCharm what library is in use, I couldn't find it. Do you have time to give me more specifics?

        1 Reply Last reply
        0
        • J JonB
          10 Jun 2023, 14:44

          @valterhome
          In a word, to get a good editing experience in PyCharm you do not want to go down the findChild(QtWidgets.QLineEdit) route. You want to do what you would do from Qt Creator, where you run something like pyside-uic on the .ui file saved from Designer to generate a new .py file from it. That will contain classes/variables for all your UI elements and allow PyCharm to determine their types for use when code completing. You want to use the Option A approach in https://doc.qt.io/qtforpython-6/tutorials/basictutorial/uifiles.html#option-a-generating-a-python-class.

          As for the connect(). ISTR PyCharm has an option where you tell it whether you are using PySide or PyQt (or nothing), have you found this?

          V Offline
          V Offline
          valterhome
          wrote on 10 Jun 2023, 19:28 last edited by
          #4

          @JonB Ok, thanks to your suggestion, I'm starting to understand something more.
          Using Pyuic5 I converted the form.ui into a Python module and imported it into the application.
          With some changes to the code, now the code completion works perfectly. Thank you!

          I'm just bored with those reports of "reference not found" on emit of line

          self.signals.updateLineEdit.emit("text")
          

          or unexpected argument on setRange of line

          self.ui.graph_widget.setRange(yRange=[-40000, 40000 ])
          

          I haven't really figured out how to set Python up like this.

          J 1 Reply Last reply 11 Jun 2023, 06:40
          0
          • V valterhome
            10 Jun 2023, 19:28

            @JonB Ok, thanks to your suggestion, I'm starting to understand something more.
            Using Pyuic5 I converted the form.ui into a Python module and imported it into the application.
            With some changes to the code, now the code completion works perfectly. Thank you!

            I'm just bored with those reports of "reference not found" on emit of line

            self.signals.updateLineEdit.emit("text")
            

            or unexpected argument on setRange of line

            self.ui.graph_widget.setRange(yRange=[-40000, 40000 ])
            

            I haven't really figured out how to set Python up like this.

            J Offline
            J Offline
            JonB
            wrote on 11 Jun 2023, 06:40 last edited by JonB 6 Nov 2023, 07:02
            #5

            @valterhome
            I don't recall having any issue with emit. I am slightly "surprised" to see your self.signals.updateLineEdit.emit(), I don't know what self.signals is, I would have expected self.ui.updateLineEdit.emit(). [OIC, I think this is a signal you have added.]

            I don't know what type your graph_widget to have a setRange(yRange=...). I don't see an inbuilt widget which has this.

            Have you tried Googling for pycharm qt emit as I would do for this? Does e.g. PyQt5 returnPressed.connect "Cannot find reference 'connect' in function" address this? You could post to PyCharm forum to ask.

            V 1 Reply Last reply 11 Jun 2023, 08:44
            0
            • J JonB
              11 Jun 2023, 06:40

              @valterhome
              I don't recall having any issue with emit. I am slightly "surprised" to see your self.signals.updateLineEdit.emit(), I don't know what self.signals is, I would have expected self.ui.updateLineEdit.emit(). [OIC, I think this is a signal you have added.]

              I don't know what type your graph_widget to have a setRange(yRange=...). I don't see an inbuilt widget which has this.

              Have you tried Googling for pycharm qt emit as I would do for this? Does e.g. PyQt5 returnPressed.connect "Cannot find reference 'connect' in function" address this? You could post to PyCharm forum to ask.

              V Offline
              V Offline
              valterhome
              wrote on 11 Jun 2023, 08:44 last edited by
              #6

              @JonB said in PCharm, PyQt5 and code completion:

              @valterhome
              I am slightly "surprised" to see your self.signals.updateLineEdit.emit()

              I'm still learning and experimenting, so I might make mistakes, even if the code seems to work fine. I use signals because I need to update lineEdits from a QThread, and I understand this was the way to go. I'll see if it's possible to optimize the code differently.

              I don't know what type your graph_widget to have a setRange(yRange=...). I don't see an inbuilt widget which has this.

              You are right, I found this example on the net and implemented it without checking. I have now corrected it according to the official documentation. Strangely, the wrong instruction still set the Y-axis correctly without returning errors.

              Have you tried Googling for pycharm qt emit as I would do for this? Does e.g. PyQt5 returnPressed.connect "Cannot find reference 'connect' in function" address this? You could post to PyCharm forum to ask.

              Thanks for the link, it helps.

              1 Reply Last reply
              0

              1/6

              10 Jun 2023, 13:21

              • Login

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