signals-slots

How does Qt implement signals and slots?

落花浮王杯 提交于 2019-11-28 23:23:24
Can someone explain to me the basic idea of Qt signals&slots mechanism IMPLEMENTATION? I want to know what all those Q_OBJECT macros do "in plain C++". This question is NOT about signals&slots usage. added: I know that Qt uses moc compiler to transform Qt-C++ in plain C++. But what does moc do? I tried to read "moc_filename.cpp" files but I have no idea what can something like this mean void *Widget::qt_metacast(const char *_clname) { if (!_clname) return 0; if (!strcmp(_clname, qt_meta_stringdata_Widget)) return static_cast<void*>(const_cast< Widget*>(this)); return QDialog::qt_metacast(

Qt Model/View/Controller Example

╄→尐↘猪︶ㄣ 提交于 2019-11-28 23:15:43
问题 I am just getting started in Qt, and trying to get a simplified, working example of the model-view-controller design pattern. So far, I have been able to use signals and slots to connect basic widgets like push buttons to a QLabel , and have the view be modified as the push button is clicked/released. See code below for a working example of that (implemented in the MainWindow class). I am trying to define a class, in this case, Game , which is going to be my model. I want Game to have all of

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

梦想与她 提交于 2019-11-28 23:10:51
问题 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()

const-ref when sending signals in Qt

隐身守侯 提交于 2019-11-28 22:24:55
问题 This is a thing that I never quite got with const-ref and I really hope that someone could explain it to me. When calling a function inside of another function, I get that const-ref is the best way when passing stack objects that I don't plan to tamper with. For example: void someInnerFunction(const QString& text) { qDebug() << text; } void someFunction() { QString test = "lala"; .... someInnerFunction(test); } So far so good, I guess. But what about signals? Isn't there any risk that comes

What are signals and slots?

馋奶兔 提交于 2019-11-28 21:16:59
Can someone explain in simple terms the "signals and slots" pattern? Signals and slots are a way of decoupling a sender (the signal) and zero or more receivers (the slots). Let's say you a system which has events that you want to make available to any other part of the system interested in those events. Rather than hard-wiring the code that generates event to the code that wants to know about those events, you would use a signals and slots pattern. When the sender signals an event (usually by calling the function associated with that event/signal) all the receivers for that event are

Qt Signals and Slot connected twice… what happens?

你离开我真会死。 提交于 2019-11-28 21:01:47
What happens if the same signal and slot is connected twice? How is the mechanism handled? San Jacinto A few weeks ago, we had an intern accidentally connect a signal to a slot more than once. The idea was that under one condition, you'd have the slot connected to the signal, and under another condition you'd disconnect it. When you changed modes, you'd do the appropriate work. Well, he forgot to to disconnect when appropriate. So every time you changed modes, you had a new connection to the slot. The end result? 1 connection == 1 call to slot. 2 connections == 2 calls to slot. 3 connections =

Prevent Firing Signals in Qt

风流意气都作罢 提交于 2019-11-28 21:01:21
We have a QCheckBox object, when user checks it or removes check we want to call a function so we connect our function to stateChanged ( int state ) signal. On the other hand, according to some condition we also change the state of QCheckBox object inside code, and this causes the unwanted signal. Is there any way to prevent firing signal under some conditions? Job You can use the clicked signal because it is only emitted when the user actually clicked the check box, not when you manually check it using setChecked . If you just don't want the signal to be emitted at one specific time, you can

Is it valid to define a pure virtual signal in C++/Qt?

一曲冷凌霜 提交于 2019-11-28 20:46:18
I am making an abstract-base-class and was thinking I might want a pure virtual signal. But when I compiled I get a warning for the pure virtual signals I have defined: ../FILE1.h:27: Warning: Signals cannot be declared virtual ../FILE1.h:28: Warning: Signals cannot be declared virtual Is it valid to define a pure virtual signal in C++/Qt? Is it valid to define a virtual signal? Qt's signal and slot documentation page says you can define virtual slots but doesn't talk about signals. I can't seem to find good information on pure virtual signals. Signals don't ever have an implementation[1] (i.e

Qt signals and slots: permissions

安稳与你 提交于 2019-11-28 20:25:08
There are discrepancies between respected answers here on SO and the actual Qt docs. I've read this question and I want some further clarification. Can anyone confirm: A signal is always protected , therefore it can be emitted only by the class or any of its subclasses. I'm not sure this is true; the question above shows answers supporting this statement. But the Qt docs say: Signals are public access functions and can be emitted from anywhere, but we recommend to only emit them from the class that defines the signal and its subclasses. So which is it? Slots are just functions, and thus may be

PyQt proper use of emit() and pyqtSignal()

╄→гoц情女王★ 提交于 2019-11-28 18:22:24
I am reading through some documentation on PyQt5 to come up with a simple signal-slot mechanism. I have come to a halt due to a design consideration. Consider the following code: import sys from PyQt5.QtCore import (Qt, pyqtSignal) from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def printLabel(self, str): print(str) def logLabel(self, str): '''log to a file''' pass def initUI(self): lcd = QLCDNumber(self) sld = QSlider(Qt.Horizontal, self) vbox = QVBoxLayout() vbox.addWidget(lcd)