在 gtk 的回调函数中调用函数
Calling a function in gtk's calllback funtion
困了几天,遇到了问题。我被迫为包括gtk在内的项目任务学习c ++。我已经管理了对我想要的窗口进行编程,并且大多数功能都很棒,但我可以围绕如何解决从回调函数存储数据的问题!
在我继续之前,我只想补充一点,我已经在网上搜索了答案,但没有偶然发现同样的问题。我知道我不能从静态回调函数调用函数
无论如何,涉及3个主要类(以及其他类):
- 窗口 - 仅负责小部件,并写入缓冲区。
- 键盘 - 有一个循环,用于侦听用户输入并更新状态消息,从缓冲区读取。
- 缓冲区 - 用户在缓冲区中添加数据。Window 类应该能够写入缓冲区,键盘应该能够读取。
在某个时候,当运行程序时,用户将希望输入新的坐标。键盘侦听器在按下特定按钮时听到,并且在类中,调用 Window 类函数并显示输入对话框。
Window *window; //Header
....
void Keyboard::GetInputCoord(){ //.cpp
window->display();
}
窗口在 Keyboard.h 中引用并在主类中链接,如下所示:
....
keyboard.window = &window;
keyboard.buff_read = &buff;
window.buff_write = &buff;
....
我在 Window 类 display() 中管理将我的条目数组发送到静态回调函数,但现在我无法弄清楚如何将其发送到我的缓冲区?我尝试从同一个类调用一个函数,但没用......
static void entry_coord(GtkButton *widget, GtkWidget **entry)
{
GtkWidget *entry_x_in = entry[0];
GtkWidget *entry_y_in = entry[1];
const char *x, *y;
x = gtk_entry_get_text(GTK_ENTRY(entry_x_in));
y = gtk_entry_get_text(GTK_ENTRY(entry_y_in));
// write_buff(x, y); <- something like this
}
由于我无法调用我在 Window.h 中声明并在 .cpp 中实现的这个函数,我有什么选项?有什么解决方法吗?
编辑2015年1月10日
窗口.h
#ifndef def_Window
#define def_Window
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ncurses.h>
#include <gtk/gtk.h>
#include "Buffer.h"
using namespace std;
class Window
{
public:
Window();
void display();
void write_buff(const char, const char);
Buffer *buff_write; //Sound maybe be in private
private:
};
Buffer.h
#ifndef def_buffer
#define def_buffer
#include <stdio.h>
#include <stdlib.h>
#include <string>
#define BUFF_ROW 10
#define BUFF_COL 2
using namespace std;
class Buffer
{
public:
Buffer();
void write_to_buffer(string msg);
void read_from_buffer();
private:
int front, back, size, count;
string buffer[][];
};
#endif
键盘.h
#ifndef def_Keyboard
#define def_Keyboard
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
#include <gtk/gtk.h>
#include "Kinematic.h"
#include "Window.h"
#include "Buffer.h"
#define MSG 10
using namespace std;
class Keyboard
{
public:
Keyboard();
void UpdateMessages();
void SetStatus(string msg);
void SetMessage(string msg);
string messages[10];
int indexMsg;
string statusString;
....
Window *window;
Buffer *buff_read;
private:
};
我已经尝试了多种写入缓冲区的方法。我尝试调用缓冲区函数 write_to_buffer(),并尝试从调用write_to_buffer的窗口函数 write_buff() 调用。编译器说函数未在此范围内声明。
将 userdata 指针设置为entry_coord()
允许您访问该函数中所需的一切来解决此问题。您对具有条目指针和缓冲区的结构的想法肯定会起作用,但在我看来,您的 Window 类也应该同样有效......只需使用 this
作为数据指针,并使回调看起来像这样:
static void entry_coord(GtkButton *widget, gpointer data_ptr)
{
auto window = static_cast<Window*> (data_ptr);
window->update_buff_from_entries();
}
然后,您的 Window 类自然需要包含指向条目的(私有)指针,以便新的公共方法update_buff_from_entries()
可以从中获取值并调用 write_buff()
。
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销