EzDevInfo.com

pyqt interview questions

Top pyqt frequently asked interview questions

PyQt or PySide - which one to use [closed]

I started learning a bit of python and would now like to toy around a bit with gui-building. Qt seems to be a good choice because of its cross-platformishness.
Now there seem to be two bindings available: PyQt by Riverbank Computing and PySide, originally developed by Nokia.
So which one should I choose? All I can find are two year old feature comparisons, but what differences are there nowadays?
Which one is easier to use, has more/better documentation? Are both still in active development?
Licensing isn't of much concern to me since I don't intend to write commercial applications.


Source: (StackOverflow)

PyQt sending parameter to slot when connecting to a signal

I have a taskbar menu that when clicked is connected to a slot that gets the trigger event. Now the problem is that I want to know which menu item was clicked, but I don't know how to send that information to the function connected to. Here is the used to connect the action to the function:

QtCore.QObject.connect(menuAction, 'triggered()', menuClickedFunc)

I know that some events return a value, but triggered() doesn't. So how do I make this happen? Do I have to make my own signal?


Source: (StackOverflow)

Advertisements

Hiding console window of Python GUI app with py2exe

I have a Python program uses Qt (PyQt4 in fact) and when I launch it from its main.py, I get a console window and the GUI window (on Windows, of course).

Then I compile my program with py2exe and main.exe is successfully created. However, if I run main.exe (this is what users of program will do) console window of Python still appears and all my debug text is stdout-ed to that window.

I want to hide cmd line window when my application is running and I want just my GUI to be visible to the user when executed from .exe file.

Is that possible?


Source: (StackOverflow)

Linking a qtDesigner .ui file to python/pyqt?

So if I go into QtDesigner and build a UI, it'll be saved as a .ui file. How can I make this as a python file or use this in python?


Source: (StackOverflow)

What is the correct way to make my PyQt application quit when killed from the console (Ctrl-C)?

What is the correct way to make my PyQt application quit when killed from the console (Ctrl-C)?

Currently (I have done nothing special to handle unix signals), my PyQt application ignores SIGINT (Ctrl+C). I want it to behave nicely and quit when it is killed. How should I do that?


Source: (StackOverflow)

Qt programming: More productive in Python or C++?

Trying to dive into Qt big time but haven't done a large project with it yet. Currently using Python, but I've been thinking -- which is really the better language to use in terms of programmer productivity?

In most comparisons between the languages, Python is the obvious answer, because you don't have to mess with memory management and all that.

However, with Qt I'm not so sure. It provides enough added features to C++ that (from what I can tell) a line of Python code is roughly equal to a line of C++ code most of the time (excluding some additional things like class definitions and structure components). Qt does nearly all the memory management for you as long as you stick with its classes, and provides equivalents to the nice containers you would find in Python.

I've always preferred statically typed languages, but have gotten on the Python bandwagon for various reasons. If programmer productivity is similar with C++, however, I may jump back that way for its other benefits -- more efficient code and fewer dependencies for users to install.

Thoughts?


Source: (StackOverflow)

Threading in a PyQt application: Use Qt threads or Python threads?

I'm writing a GUI application that regularly retrieves data through a web connection. Since this retrieval takes a while, this causes the UI to be unresponsive during the retrieval process (it cannot be split into smaller parts). This is why I'd like to outsource the web connection to a separate worker thread.

[Yes, I know, now I have two problems.]

Anyway, the application uses PyQt4, so I'd like to know what the better choice is: Use Qt's threads or use the Python threading module? What are advantages / disadvantages of each? Or do you have a totally different suggestion?

Edit (re bounty): While the solution in my particular case will probably be using a non-blocking network request like Jeff Ober and Lukáš Lalinský suggested (so basically leaving the concurrency problems to the networking implementation), I'd still like a more in-depth answer to the general question:

What are advantages and disadvantages of using PyQt4's (i.e. Qt's) threads over native Python threads (from the threading module)?


Edit 2: Thanks all for you answers. Although there's no 100% agreement, there seems to be widespread consensus that the answer is "use Qt", since the advantage of that is integration with the rest of the library, while causing no real disadvantages.

For anyone looking to choose between the two threading implementations, I highly recommend they read all the answers provided here, including the PyQt mailing list thread that abbot links to.

There were several answers I considered for the bounty; in the end I chose abbot's for the very relevant external reference; it was, however, a close call.

Thanks again.


Source: (StackOverflow)

Anyone know of a good resource for QT style sheets? [closed]

I know how to make my own QT style sheets but I really don't want to spend the time. Seems like there must be some online source with a bunch of different QT "themes" and style sheets that can be downloaded but I can't find any. Any help would be much appreciated. Thanks.


Source: (StackOverflow)

Developing GUIs in Python: Tkinter vs PyQt [closed]

If one wants to develop a user interface in Python, which one to go for: TkInter or PyQt?

I just started with TkInter and I was able to get some simple UIs going with elementary widgets like label, button, text box etc. Just curious to know how good PyQt would be compared to TkInter?

cheers


Source: (StackOverflow)

QListWidget adjust size to content

Is it possible to adjust QListWidget height and width to it's content?

sizeHint() always returns 256, 192 no matter what its content is.
QListWidgetItem's sizeHint() returns -1, -1, so I can not get content width.

Problem the same as here - http://www.qtcentre.org/threads/31787-QListWidget-width , but there is no solution.

enter image description here

import sys
from PyQt4.QtGui import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        list = QListWidget()
        list.addItem('111111111111111')

        vbox = QVBoxLayout(self)
        vbox.addWidget(list)

app = QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())

Source: (StackOverflow)

Selecting GUI on windows (wxPy vs pyQt)

We are planning to develop an application for monitoring and configuring our service (which is running on remote server). After long time of discussion, we decided for python as platform for our app, because we love and know python. But we don't know, what GUI toolkit preferred for our aims. We need fast (for development and running) app, whose users will be Admins, Maintainers and Account managers.

There are two GUI toolkit for python, which we know: wxPython and pyQT. Anybody have arguments regarding pro and cons? And maybe someone knows any commercial applications, using these products (only python version of toolkits)? Links are desirable.

Thanks.


Source: (StackOverflow)

Integrate Qt with Windows 7 taskbar using python?

I'm looking for a way to using some of the Windows 7 task bar features - specifically progressbar and jump lists - in a python application that uses Qt for the UI. The questions I've seen on SO are all 1-2 years old and either say Qt isn't there yet but the next version will have it (which has since been released) or point me to Q7Goodies which I'd prefer not to use for cost reasons.

Does anyone know if Qt and a python binding, either PyQt or PySide, can interact with the Windows 7 taskbar?

I am using Python 2.7.x and Qt 4.6. I can upgrade to 4.7 if needed, I can't move up to Python 3.x just yet.


Source: (StackOverflow)

Should wildcard import be avoided?

I'm using PyQt and am running into this issue. If my import statements are:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

then pylint gives hundreds of "Unused import" warnings. I'm hesitant to just turn them off, because there might be other unused imports that are actually useful to see. Another option would be to do this:

from PyQt4.QtCore import Qt, QPointF, QRectF
from PyQt4.QtGui import QGraphicsItem, QGraphicsScene, ...

and I end up having 9 classes on the QtGui line. There's a third option, which is:

from PyQt4 import QtCore, QtGui

and then prefix all the classes with QtCore or QtGui whenever I use them.

At this point I'm agnostic as to which one I end up doing in my project, although the last one seems the most painful from my perspective. What are the common practices here? Are there technical reason to use one style over the other?


Source: (StackOverflow)

Qt: Erase background (Windows Aero Glass)

Update

see Using Blur Behind on Windows for an example of using Qt and DWM.alt text


Original question:

I want to create a Windows Aero Glass window with Qt, now it looks like this: alt text

But after calling some my_window->repaint() my window's label becomes broken: alt text

But now if I resize the window slightly, it repaints properly.


The question is: how do I erase the window background, so that widgets would paint themselves on a clean glass?


The short code to reproduce the problem is (Vista with Aero):

class Window(QWidget):
	def __init__(self, *args):
		QWidget.__init__(self, *args)
		self.setLayout(QVBoxLayout())
		self.layout().addWidget(QLabel("This is the text"))

		# let the whole window be a glass
		self.setAttribute(Qt.WA_NoSystemBackground)
		from ctypes import windll, c_int, byref
		windll.dwmapi.DwmExtendFrameIntoClientArea(c_int(self.winId()), byref(c_int(-1)))
	def mousePressEvent(self, event):
		self.repaint()

You can click the window now, or just hit Alt-Tab several times.

Anyway, using labels with Aero Glass is not what I need, because QLabel doesn't know how to paint itself with a while glow (like the title of the window). What I need is a general way to clean the "glass".


Source: (StackOverflow)

QWebView undersampled SVG rendering

I'd like to use QWebView for an SVG thumbnailing job because it supports filters (unlike QSvgRender which only supports SVG Tiny 1.2). It seems to work quite well except for font rendering but that can be tweaked using fontconfig package. The problem is that it seems to be undersampling elements with filters. Elements without filters look good and sharp, while those with filters are pixelated and blurry.

from  PyQt4.QtGui import *
from  PyQt4.QtCore import *
from PyQt4.QtSvg import *
from PyQt4.QtWebKit import *
import sys
import time

if __name__ == '__main__':

    app = QApplication(sys.argv)

    data = open('/home/xxx/workspace/yyy/zzz/out.svg').read()

    # svg = QSvgRenderer(QByteArray(data))
    qim = QImage(int(1024), int(768), QImage.Format_ARGB32)                                                                                                                                                                                 
    web = QWebView()
    web.setRenderHint(QPainter.SmoothPixmapTransform)
    web.setRenderHint(QPainter.Antialiasing)
    web.setRenderHint(QPainter.TextAntialiasing)
    painter = QPainter()

    def load_finished(ok):
        web.resize(1024,768)
        painter.begin(qim)
        # svg.render(painter)
        web.render(painter)
        painter.end()

        print "null:", qim.isNull()
        qim.save('test2.png')
        sys.exit()

    web.connect(web, SIGNAL('loadFinished(bool)'), load_finished)
    web.load(QUrl('file:///home/xxx/workspace/yyy/zzz/out.svg'))
    sys.exit(app.exec_())

Qt 4.7. Same SVG file looks OK when rendered using Inkscape, rsvg or in Chrome, Firefox.

result


Source: (StackOverflow)