4/13/2022

Can Qt Slot Return Value

Can Qt Slot Return Value Rating: 3,6/5 2192 votes

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. Several of the example programs connect the valueChanged signal of a QScrollBar to the display slot, so the LCD number continuously shows the value of the scroll bar. Note that display is overloaded; Qt will select the appropriate version when you connect a signal to the slot.

  1. It is for reading the property value. Ideally, a const function is used for this purpose, and it must return either the property's type or a const reference to that type. E.g., QWidget::focus is a read-only property with READ function, QWidget::hasFocus. A WRITE accessor function is optional. It is for setting the property value.
  2. Qt is well known for its signals and slots mechanism. But how does it work? In this blog post, we will explore the internals of QObject and QMetaObject and discover how signals and slot work under the hood.
  • 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:

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

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š
Votes:
0Vote for this issue
Watchers:
1Start watching this issue

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.

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

  • 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

Can Qt Slot Return Value Guide

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:
  • An example of a signal emitted from another QThread:
  • Signals are runtime objects owned by instances, they are not class attributes:

Can Qt Slot Return Value Kelley Blue Book

Retrieved from 'https://wiki.qt.io/index.php?title=Qt_for_Python_Signals_and_Slots&oldid=35927'