我是
GTK
的新手,我来自 Qt
的背景。我试图弄清楚信号在 GTK
中是如何工作的,并且我试图发出一个信号,但它不起作用。令我惊讶的是,我找不到一个像样的代码示例来发出有效的 GTK
信号。这是我到目前为止的代码(我正在使用Qt Creator
):
Test_Gtk.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2015-04-26T12:42:38
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test_Gtk
TEMPLATE = app
SOURCES += main.cpp \
myfirstobject.cpp \
mysecondobject.cpp
HEADERS += \
myfirstobject.h \
mysecondobject.h
unix:!macx{
# Make sure you install libappindicator-dev
INCLUDEPATH += /usr/include/glib-2.0
INCLUDEPATH += /usr/include/gtk-2.0
INCLUDEPATH += /usr/lib/x86_64-linux-gnu/glib-2.0/include
INCLUDEPATH += /usr/lib/x86_64-linux-gnu/gtk-2.0/include
INCLUDEPATH += /usr/include/cairo
INCLUDEPATH += /usr/include/pango-1.0
INCLUDEPATH += /usr/include/gdk-pixbuf-2.0
INCLUDEPATH += /usr/include/atk-1.0
LIBS += -L/usr/lib/x86_64-linux-gnu -lgobject-2.0
LIBS += -L/usr/lib/x86_64-linux-gnu -lgtk-x11-2.0
}
myfirstobject.h:
#ifndef MYFIRSTOBJECT_H
#define MYFIRSTOBJECT_H
#include <QObject>
#ifdef Q_OS_LINUX
#undef signals
extern "C" {
#include <gtk/gtk.h>
}
#define signals public
#endif
class MyFirstObject : public GObject
{
public:
MyFirstObject();
~MyFirstObject();
void emitMySignal();
};
#endif // MYFIRSTOBJECT_H
myfirstobject.cpp:
#include "myfirstobject.h"
MyFirstObject::MyFirstObject()
{
g_signal_new("my-signal",
G_TYPE_OBJECT, G_SIGNAL_RUN_FIRST,
0, NULL, NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
}
MyFirstObject::~MyFirstObject()
{
}
void MyFirstObject::emitMySignal()
{
g_signal_emit_by_name (this, "my-signal");
}
mysecondobject.h:
#ifndef MYSECONDOBJECT_H
#define MYSECONDOBJECT_H
#include <QObject>
#ifdef Q_OS_LINUX
#undef signals
extern "C" {
#include <gtk/gtk.h>
}
#define signals public
#endif
class MyFirstObject;
class MySecondObject : public GObject
{
public:
MySecondObject(MyFirstObject *obj);
~MySecondObject();
void mySlot();
private:
MyFirstObject *m_obj;
};
#endif // MYSECONDOBJECT_H
mysecondobject.cpp:
#include "mysecondobject.h"
#include "myfirstobject.h"
#include <QDebug>
MySecondObject::MySecondObject(MyFirstObject *obj) : m_obj(obj)
{
g_signal_connect(m_obj, "my-signal", G_CALLBACK(&MySecondObject::mySlot), 0);
}
MySecondObject::~MySecondObject()
{
}
void MySecondObject::mySlot()
{
qDebug() << "mySlot was called";
}
main.cpp:
#include <QCoreApplication>
#include "myfirstobject.h"
#include "mysecondobject.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyFirstObject *o1 = new MyFirstObject;
MySecondObject *o2 = new MySecondObject(o1);
o1->emitMySignal();
return a.exec();
}
代码很简单。我从
MyFirstObject
发出信号,并希望在 MySecondObject
中调用回调。问题是回调函数永远不会被调用。你能告诉我我做错了什么吗?有没有一个地方可以很好地解释 GTK
信号或一些代码示例?
据我所知,
GObject
中的GTK
就像QObject
中的Qt
,GtkObject
中的GTK
就像QWidget
中的Qt
。这是真的吗?
gtkmm 教程建议使用 libsigc++ 从 C++ 代码创建信号; gtkmm 内部使用 libsigc++ 将 C 中定义的信号包装在 GTK+ 本身中。
还提供了一个(太冗长,无法复制)示例: 创建您自己的信号 - 示例