Skip to content

Commit 2d3ebb7

Browse files
committed
updated example based on feedback
1 parent 1ddf17e commit 2d3ebb7

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

examples/qt/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# libsigc++ with Qt
2+
3+
It is possible to use libsigc++ with Qt. However, because of the signals/slots
4+
mechanism of Qt, there is some setup that must be done in order for this to
5+
happen correctly.
6+
7+
Steps to use libsigc++ with Qt:
8+
1. In your .pro file, add `CONFIG += no_keywords`. This configures Qt to not
9+
define the keywords `emit`, `signals`, and `slot`.
10+
2. In your header files, change the `signals:` section of your class to instead
11+
be `Q_SIGNALS`
12+
3. In your header files, change the `public slots:` section of your class to
13+
instead be `public Q_SLOTS:`
14+
4. In any class that you emit a signal, change `emit` to be `Q_EMIT`.
15+
16+
Here's an example of a class before and after this conversion(note: irrelevant
17+
code has been removed):
18+
19+
```
20+
class ExampleClass : public QObject {
21+
signals:
22+
void exampleSignal();
23+
public slots:
24+
void example_slot(){
25+
emit exampleSignal();
26+
}
27+
};
28+
```
29+
30+
After conversion:
31+
```
32+
class ExampleClass : public QObject {
33+
Q_SIGNALS:
34+
void exampleSignal();
35+
public Q_SLOTS:
36+
void example_slot(){
37+
Q_EMIT exampleSignal();
38+
}
39+
};
40+
```
41+
42+
## Qt Slots Notes
43+
Since libsigc++ simply requires a slot to be a function, you can call Qt
44+
slots easily using libsigc++. Similarly, a function that is a libsigc++ slot
45+
can also be used as a Qt slot.

examples/qt/exampleclass.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
#include "exampleclass.h"
44

55
ExampleClass::ExampleClass(QObject *parent) :
6-
QObject(parent),
7-
m_times(0)
6+
QObject(parent)
87
{
98
/* Create a slot from our example_slot method. */
10-
m_slot = sigc::mem_fun( *this, &ExampleClass::example_slot );
9+
m_sigc_slot = sigc::mem_fun( *this, &ExampleClass::example_slot );
1110

11+
/* Connect our sigc++ signal to our sigc++ slot */
12+
m_sigc_signal.connect( m_sigc_slot );
13+
14+
/* Emit a sigc++ signal */
15+
m_sigc_signal.emit();
16+
17+
/* Connect the Qt signal to our Qt slot */
1218
connect( &m_timer, &QTimer::timeout,
1319
this, &ExampleClass::timer_slot );
1420
m_timer.start( 200 );
21+
22+
/* Emit a Qt signal */
23+
Q_EMIT example_signal();
1524
}
1625

1726
void ExampleClass::timer_slot(){
1827
qDebug() << "Timer slot called";
1928

20-
/* Call the slot that is used by libsigc++ */
21-
m_slot();
22-
23-
/* Emit a Qt signal */
24-
Q_EMIT example_signal();
25-
26-
/* Only run for a few times before exiting */
27-
if( m_times++ > 10 ){
28-
QCoreApplication::exit( 0 );
29-
}
29+
QCoreApplication::exit( 0 );
3030
}
3131

3232
void ExampleClass::example_slot(){

examples/qt/exampleclass.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public Q_SLOTS:
2828
void example_slot();
2929

3030
private:
31-
sigc::slot<void()> m_slot;
31+
sigc::slot<void()> m_sigc_slot;
32+
sigc::signal<void()> m_sigc_signal;
3233
QTimer m_timer;
33-
int m_times;
3434
};
3535

3636
#endif // EXAMPLECLASS_H

0 commit comments

Comments
 (0)