2

I'm fairly new to Python, but have gotten it set up and working in Visual Studio Code (Note: this is not the same as Visual Studio).

I'm working through a GUI tutorial for Qt for Python, using the PySide2 package. Most symbols are giving me code completion through the VS Code's Microsoft Python extension's Intellisense feature, but one, the PySide2.QtCore.Qt module is not.

Intellisense still does give some symbols, but they are all symbols with double underscores at the start and end, and one named mro. As an example, the working PySide2.QtCore.Qt.AlignHCenter symbol, which compiles and works fine, does not show up for tab completion/Intellisense.

enter image description here

If I type in directly QtCore.Qt.AlignHCenter, it is accepted and works fine.

Question

How do I get Intellisense/tab completion to work for the PySide2.QtCore.Qt module in the PySide2 package?

More Information

PySide2 is an open source Python adapter for Qt. There is a mostly identical separate Python binding for Qt, PyQt5, with the primary difference being licensing (see What's the difference between PyQt5 and PySide2).

The Qt module appears to be a virtual module, at least in PySide2, per this answer, which might be part of the issue, although substituting PyQt5.Qt in for PySide2.QtCore.Qt doesn't seem to work either (i.e. Intellisense seems broken for both binding projects).

I have the following extensions installed in VS Code that appear relevant (many more for generic things and other languages also installed):

  • Python 2019.9.34474
  • Python Extension Pack 1.6.0 (pack with Python, MagicPython, Jinja, Django, VS IntelliCode)
  • Qt for Python 0.4.1 (PyQt5 and PySide2 support)

Things I've tried

I have working code, posted below. The QtCore.Qt.AlignHCenter in line 27 is the element that won't provide any Intellisense under the Qt symbol.

I've tried importing the Qt module in various ways, including directly from PyQt5:

import PyQt5.Qt

(NOTE: per comments, PyQt5 is a different project from PySide2, but most of the API is the same and the Qt for Python VS Code extension supports both binding sets for Qt)

Which appears to do nothing.

I've tried other methods to get the specific elements to be imported, such as:

from PySide2.QtCore import Qt
from PySide2.QtCore.Qt import *

The second line here fails, saying there is an unresolved import: enter image description here

The first line importing Qt works to provide Qt as a known module, so I can enter just Qt.xxx rather than QtCore.Qt.xxx, but Intellisense still fails the same way:

The code:

### PyDownloader
# Simple GUI to Download a file from a URL
#
# Can test with this URL:
# - https://github.com/gitextensions/gitextensions/raw/master/.nuget/NuGet.exe
# which is an ~ 5Mb file

import sys
from PySide2 import QtWidgets, QtGui, QtCore

import urllib.request

class Downloader(QtWidgets.QDialog):
    
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self)

        self.url = QtWidgets.QLineEdit()
        self.save_location = QtWidgets.QLineEdit()
        self.progress = QtWidgets.QProgressBar()
        download = QtWidgets.QPushButton("Download")

        self.url.setPlaceholderText("URL")
        self.save_location.setPlaceholderText("File save location")

        self.progress.setValue(0)
        self.progress.setAlignment(QtCore.Qt.AlignHCenter)

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.url)
        layout.addWidget(self.save_location)
        layout.addWidget(self.progress)
        layout.addWidget(download)
        self.setLayout(layout)

        self.setWindowTitle("PyDownloader")
        self.setFocus()

        download.clicked.connect(self.download)
    
    def download(self):
        url = self.url.text()
        save_location = self.save_location.text()
        urllib.request.urlretrieve(url, save_location, self.report)
        # Assume success
        self.progress.setValue(100)

    def report(self, blocknum, blocksize, totalsize):
        readsofar = blocknum * blocksize
        if totalsize > 0:
            percent = readsofar * 100 / totalsize
        else:
            # Just have it progress continuously as each block comes in
            percent = (blocknum % 100)
        self.progress.setValue(int(percent))


def main():
    app = QtWidgets.QApplication(sys.argv)
    dialog = Downloader()
    dialog.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Community
  • 1
  • 1
LightCC
  • 9,804
  • 5
  • 52
  • 92
  • PySide2 has nothing whatsoever to do with PyQt5. They both provide Python bindings for the Qt library, but are otherwise completely separate projects, Also the Qt module is specific to PyQt5, and is not the same thing as QtCore.Qt. Please therefore edit your question and remove all the confusing references to PyQt5 and its virtual Qt module. As for the problem itself: it seems to be a bug in the Qt for Python extension, so you should report it to the appropriate issue tracker. – ekhumoro Sep 07 '19 at 22:29
  • I'll clean up the question to make the distinction clear, but please talk to Qt Company and the Qt for Python project and have them clean up all references to these two projects which are often used interchangeably, even by Qt Company employees in Qt Company tutorials; which, also, can be dual targeted by projects, and which have 99%+ duplicate APIs, and besides a few minor details are only different in their licenses. In terms of figuring out why the tab completion isn't working, isn't it useful to attempt targeting each binding? – LightCC Sep 08 '19 at 04:07

0 Answers0