File tree Expand file tree Collapse file tree 3 files changed +60
-15
lines changed Expand file tree Collapse file tree 3 files changed +60
-15
lines changed Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change 3
3
#include " exampleclass.h"
4
4
5
5
ExampleClass::ExampleClass (QObject *parent) :
6
- QObject(parent),
7
- m_times(0 )
6
+ QObject(parent)
8
7
{
9
8
/* 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 );
11
10
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 */
12
18
connect ( &m_timer, &QTimer::timeout,
13
19
this , &ExampleClass::timer_slot );
14
20
m_timer.start ( 200 );
21
+
22
+ /* Emit a Qt signal */
23
+ Q_EMIT example_signal ();
15
24
}
16
25
17
26
void ExampleClass::timer_slot (){
18
27
qDebug () << " Timer slot called" ;
19
28
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 );
30
30
}
31
31
32
32
void ExampleClass::example_slot (){
Original file line number Diff line number Diff line change @@ -28,9 +28,9 @@ public Q_SLOTS:
28
28
void example_slot ();
29
29
30
30
private:
31
- sigc::slot<void ()> m_slot;
31
+ sigc::slot<void ()> m_sigc_slot;
32
+ sigc::signal<void ()> m_sigc_signal;
32
33
QTimer m_timer;
33
- int m_times;
34
34
};
35
35
36
36
#endif // EXAMPLECLASS_H
You can’t perform that action at this time.
0 commit comments