signals-slots

Public functions versus public slots

你离开我真会死。 提交于 2019-11-30 03:18:55
问题 In my one year of Qt programming, I have learnt a lot about signals and slots. But not enough... http://doc.qt.io/qt-5/signalsandslots.html Slots can be used for receiving signals, but they are also normal member functions. So... Is there any reason not to declare every single function in a class that inherits from QObject , as a slot, whether it needs to be one or not ? In the link above, they give an example: A small QObject-based class might read: #include <QObject> class Counter : public

(How) is it possible to bind/rebind a method to work with a delegate of a different signature?

本小妞迷上赌 提交于 2019-11-30 02:13:32
I'm a c++ developer having used signals & slots in c++ which to me seems to be analogous to delegates in c#. I've found myself at a loss in searching for the functionality provided by "bind", and feel I must be missing something. I feel like that something like the following, which is possible in c++ should be possible in c# with delegates. Here is some psudo-code for what I would do in c++: Slot<void> someCallback; int foo(int i) { std::cout << "Value: " << i << "\n"; return i; } int main() { int i = 0; Slot<int> someCallback = bind( fun_ptr(foo), i ); ++i; // added to show that late

PyQt Widget connect() and disconnect()

隐身守侯 提交于 2019-11-30 01:57:29
Depending on a conditions I would like to connect/re-connect a button to a different function. Let's say I have a button: myButton = QtGui.QPushButton() For this example let's say I check if there is an internet connection. if connected == True: myButton.clicked.connect(function_A) elif connected == False: myButton.clicked.connect(function_B) First of all I would like to disconnect a button from any function it was already connected before the button is being re-assigned/re-connected to another function (function_A or function_B). Secondly, I have already noticed that after the button is re

Create PyQt menu from a list of strings

与世无争的帅哥 提交于 2019-11-30 00:47:17
I have a list of strings and want to create a menu entry for each of those strings. When the user clicks on one of the entries, always the same function shall be called with the string as an argument. After some trying and research I came up with something like this: import sys from PyQt4 import QtGui, QtCore class MainWindow(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.menubar = self.menuBar() menuitems = ["Item 1","Item 2","Item 3"] menu = self.menubar.addMenu('&Stuff') for item in menuitems: entry = menu.addAction(item) self.connect(entry,QtCore.SIGNAL(

How to keep the source signal's parameters while using QSignalMapper?

蹲街弑〆低调 提交于 2019-11-29 19:54:17
问题 I ran into a problem that I need to keep the mapped source signal's parameters. So far I only found examples to map signals without any parameter. For example, the clicked() signal: signalMapper = new QSignalMapper(this); signalMapper->setMapping(taxFileButton, QString("taxfile.txt")); connect(taxFileButton, SIGNAL(clicked()), signalMapper, SLOT (map())); connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(readFile(QString))); However, I would need to map some signal with its own

Qt - emit a signal from a c++ thread

蓝咒 提交于 2019-11-29 17:22:50
问题 I want to emit a signal from a C++ thread (std::thread) in Qt. How can I do it? 回答1: You definitely can emit a signal from a thread ( QThread , std::thread or even boost::thread ). Only you must be careful of your connect function's fifth parameter ( Qt::ConnectionType ): If Qt::DirectConnection : The slot is invoked immediately (from the current thread), when the signal is emitted. If Qt::QueuedConnection : The slot is invoked when control returns to the event loop of the receiver's thread.

Lifetime of object in lambda connected to pyqtSignal

百般思念 提交于 2019-11-29 15:46:41
Suppose I have an object and want one of its methods to be executed when a PyQt signal is emitted. And suppose I want it to do so with a parameter that is not passed by the signal. So I create a lambda as the signal's slot: class MyClass(object): def __init__(self, model): model.model_changed_signal.connect(lambda: self.set_x(model.x(), silent=True)) Now, normally with PyQt signals and slots, signal connections don't prevent garbage collection. When a connected slot's object is garbage collected, the slot will no longer be called when the corresponding signal is emitted. However, how does this

Qt signals and slots, threads, app.exec(), and related queries

可紊 提交于 2019-11-29 14:33:13
问题 [related to this question] I wrote this piece of code to understand how qt signals and slots work. I need someone to explain the behaviour, and to tell me if I'm right about my own conclusions. My program: connectionhandler.h #ifndef CONNECTIONHANDLER_H #define CONNECTIONHANDLER_H #include <QTcpServer> class ConnectionHandler : public QObject { Q_OBJECT public: ConnectionHandler(); public slots: void newConn(); private: QTcpServer *server; }; #endif // CONNECTIONHANDLER_H connectionhandler

How to declare New-Signal-Slot syntax in Qt 5 as a parameter to function

你。 提交于 2019-11-29 13:52:38
How can I pass signal or slot (member-function, new syntax in Qt 5) as a parameter to function and then call connect ? e.g. I want to write a function that waits for a signal. Note : It is not compile - PointerToMemberFunction is my question. bool waitForSignal(const QObject* sender, PointerToMemberFunction??? signal, int timeOut = 5000/*ms*/) { if (sender == nullptr) return true; bool isTimeOut = false; QEventLoop loop; QTimer timer; timer.setSingleShot(true); QObject::connect(&timer, &QTimer::timeout, [&loop, &isTimeOut]() { loop.quit(); isTimeOut = true; }); timer.start(timeOut); QObject:

Qt: dynamic widgets signal and slot connection

戏子无情 提交于 2019-11-29 12:19:00
In my Qt app, I create some of my widgets dynamically. Among them are QToolButtons that need to have a similar behavior. Here is how the widgets are created: QMap<QString, QToolButton*> unselectButtonMap; foreach(QString instance, ...) { unselectButtonMap[instance] = new QToolButton; myLayout->addWidget(unselectButtonMap[instance]); QObject::connect(unselectButtonMap[instance],SIGNAL(clicked()),this,SLOT(unselectInstance())); } Now I would like the unselectInstence slot to know which instance is concerned. I first thougth about giving the instance name as a parameter to the slot, but slots