Qt Signal Slot Pass Reference

Qt Signal Slot Pass Reference 5,7/10 7346 reviews

My GUI consists of a LineEdit and a PushButton. When the PushButton is clicked, the slot clicked() is called. I want to setup a signal-slot relationship between clicked() as the signal and doSomething() as the slot. The issue is that doSomething() does not have access to the UI and doSomething() relies on the text of LineEdit to work.

  1. Qt Signal Slot Example
  2. Qt Signal Slot Pass Reference Chart
  3. Qt Signal Slot Pass Reference Tool

I see two solutions:

  1. Grant doSomething() access to the UI.

  2. I want to be able to pass a QString to doSomething() as an argument. clicked() does not accept any arguments. Qsignalmapper might be what I'm looking for. However, it seems like the mapper only passes arguments based on what the signal is. The arguments therefore need to be setup in advance and it seems like it does not help me here.

How would I do solution 2?

Assuming a C++ solution, you might consider using an intermediate signal like the following for #2:

connect(thisWidget, SIGNAL(clicked()),
thisWidget, SLOT(slotClickHandler()));
Qt signal slot parameter

then:

void slotClickHandler()
{
QString s ='my variable string here';
emit sigDoSomething(s);
}

and them:

void slotDoSomething(const QString &s)
{
// Do something interesting with s here
}

Note that this snippet assumes you've defined a new signal sigDoSomething(const QString &) and a private slot void slotDoSomething(const QString &).

Discussion

Qt Signal Slot Example

  • Brilliant! Which would you say is cleaner? Method 1 or method 2? It depends, right? The problem with method 2 is duplicating the number of signals/slots can get messy when you're dealing with a lot of GUI elements, correct? Also, why is it const QString &s? Shouldn't it just be QString s?
  • As you state, which is 'cleaner' depends. What does cleaner mean? Easier to read? Easier to debug? Easier on the compiler? The developer is usually the weakest link, so bias 'cleaner' accordingly. Map signals and slots clearly to their respective widgets (with meaningful names, etc.). Let the QT moc and gcc worry about the number of calls. Simple chained function calls will likely be optimized anyway. Lastly, the seeming duplication of calls will be dependent on your implementation: how many pushbuttons are there? what exactly do they need to convey>?, etc.
  • As for const QString &, I'm assuming that the slot implementation does not need to modify the QString s. If so, a pass-by-value (i.e. QString s), will make a needless copy of the QString object. On the other hand, passing the QString by reference (QString &) allows the compiler to pass a more efficient reference (in essence a pointer to the object). Adding the const qualifier assures that the compiler is aware that the QString is not expected to be modified. (Indeed, the compiler will assert an error if you attempt to modify a const object).
  • If you wish the slot to modify the QString, then you probably need to rethink the design. Nasty issues can arise.
Signal

Qt Signal Slot Pass Reference Chart

The only other way I can think of getting this information is creating a blocking signal/slot for the main window to the socket thread that passes a QString (ip address) by reference and an unsigned int (port) by reference to the thread, and just filling the objects from the socket thread. 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.

Qt Signal Slot Pass Reference Tool

Warning: When passing a QString to the constructor or calling setText, make sure to sanitize your input, as QLabel tries to guess whether it displays the text as plain text or as rich text. You may want to call setTextFormat explicitly, e.g. In case you expect the text to be in plain format but cannot control the text source (for instance when displaying data loaded from the Web). The Qt signals/slots and property system are based on the ability to introspect the objects at runtime. Introspection means being able to list the methods and properties of an object and have all kinds of information about them such as the type of their arguments. QtScript and QML would have hardly been possible without that ability. Jun 29, 2013 Even if the sender of the signal and the receiver of the slot are in different threads, we should still pass arguments by const reference. Qt takes care of copying the arguments, before they cross the thread boundaries – and everything is fine.