Posted  by  admin

Can Qt Slot Return Value

This page describes the use of signals and slots in Qt for Python.The emphasis is on illustrating the use of so-called new-style signals and slots, although the traditional syntax is also given as a reference.

  • A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user. QDialogs may be modal or modeless. QDialogs can provide a return value, and they can have default buttons. QDialogs can also have a QSizeGrip in their lower-right corner, using setSizeGripEnabled.
  • The intermediate functions can be as simple or as complicated as you like - as well as discarding/adding parameters, you can also perform lookups to modify signals to different values. In the following example a checkbox signal Qt.Checked or Qt.Unchecked is modified by an intermediate slot into a bool value.
  • Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type.

Sending Python values with signals and slots. On the #pyqt channel on Freenode, Khertan asked about sending Python values via Qt's signals and slots mechanism. The following example uses the PyQtPyObject value declaration with an old-style signal-slot connection, and again when the signal is emitted, to communicate a Python dictionary. When the user closes the dialog, exec will provide a useful return value. To close the dialog and return the appropriate value, you must connect a default button, e.g. An OK button to the accept slot and a Cancel button to the reject slot. Alternatively, you can call the done slot with Accepted or Rejected.

The main goal of this new-style is to provide a more Pythonic syntax to Python programmers.

Can qt slot return value calculator
  • 2New syntax: Signal() and Slot()

Traditional syntax: SIGNAL () and SLOT()

QtCore.SIGNAL() and QtCore.SLOT() macros allow Python to interface with Qt signal and slot delivery mechanisms.This is the old way of using signals and slots.

The example below uses the well known clicked signal from a QPushButton.The connect method has a non python-friendly syntax.It is necessary to inform the object, its signal (via macro) and a slot to be connected to.

New syntax: Signal() and Slot()

The new-style uses a different syntax to create and to connect signals and slots.The previous example could be rewritten as:

Using QtCore.Signal()

Signals can be defined using the QtCore.Signal() class.Python types and C types can be passed as parameters to it.If you need to overload it just pass the types as tuples or lists.

In addition to that, it can receive also a named argument name that defines the signal name.If nothing is passed as name then the new signal will have the same name as the variable that it is being assigned to.

The Examples section below has a collection of examples on the use of QtCore.Signal().

Note: Signals should be defined only within classes inheriting from QObject.This way the signal information is added to the class QMetaObject structure.

Using QtCore.Slot()

Slots are assigned and overloaded using the decorator QtCore.Slot().Again, to define a signature just pass the types like the QtCore.Signal() class.Unlike the Signal() class, to overload a function, you don't pass every variation as tuple or list.Instead, you have to define a new decorator for every different signature.The examples section below will make it clearer.

Another difference is about its keywords.Slot() accepts a name and a result.The result keyword defines the type that will be returned and can be a C or Python type.name behaves the same way as in Signal().If nothing is passed as name then the new slot will have the same name as the function that is being decorated.

Examples

The examples below illustrate how to define and connect signals and slots in PySide2.Both basic connections and more complex examples are given.

  • Hello World example: the basic example, showing how to connect a signal to a slot without any parameters.
  • Next, some arguments are added. This is a modified Hello World version. Some arguments are added to the slot and a new signal is created.
  • Add some overloads. A small modification of the previous example, now with overloaded decorators.
  • An example with slot overloads and more complicated signal connections and emissions (note that when passing arguments to a signal you use '[]'):
  • An example of an object method emitting a signal:
Can qt slot return value list
  • An example of a signal emitted from another QThread:
  • Signals are runtime objects owned by instances, they are not class attributes:
Retrieved from 'https://wiki.qt.io/index.php?title=Qt_for_Python_Signals_and_Slots&oldid=35927'
  • Status:Reported
  • Resolution: Unresolved
  • Fix Version/s: None
  • Labels:

The following program reproduces the issue:

When I run that program , I get the following output to stdout:

Can qt slot return value kelley blue book

After the sections are resized from 105 (which I assume is their default value) to 210 and 196, signal sectionResized is emitted twice, once for section 0 and once for section 1. You can see that in the first call of the slot connected to that signal, sectionSize(0) and sectionPosition(1) return wrong values. In the second call of the slot, sectionSize(1) returns the wrong value. This behavior occurs not only during the initialization, but also when sections are resized at a later time (as long as sectionResizeMode is ResizeToContents), as attached file example.cpp demonstrates.

I would expect that when sectionResized is emitted, at least sectionSize(logicalIndex) would return the new value. The fact that it doesn't is surprising and can lead to hard to find bugs.

The issue seems to be in QHeaderViewPrivate::resizeSections. In that method, there is a loop that takes care of updating SectionItem objects and emitting signal sectionResized. It emits the signal for section with visual index i in its ith iteration, but it updates the corresponding SectionItem object in a later iteration, after it has reached a section with a different new length (if there is no such section, the SectionItem object is updated after the loop is finished). Therefore, the SectionItem object still contains outdated values when signal sectionResized is emitted for the corresponding section.

Attachments

Gerrit Reviews

Can Qt Slot Return Value Kelley Blue Book

No reviews matched the request. Check your Options in the drop-down menu of this sections header.

Can Qt Slot Return Value Guide

Assignee:
Qt Quick and Widgets Team
Reporter:
Jernej Krempuš

Can Qt Slot Return Value Calculator

Votes:
0Vote for this issue
Watchers:
1Start watching this issue