因此,我试图制作一个程序,该程序复制Gtk::Entry的输入并将其输出到控制台。但是,目前,当我输入第一个字符时,什么也没有发生。输入第二个字符后,第一个字符被打印,输入第三个字符后,前两个字符被打印。当我输入第四个字符时,将打印前三个字符。我想要这样,以便当我输入第一个字符时,第一个字符被打印,一旦输入第二个字符,则前两个字符被打印,依此类推。
这是我当前的inputbox.cpp
#include "inputbox.h"
#include <iostream>
inputBox::inputBox(){
this->signal_key_press_event().connect(sigc::mem_fun(*this, &inputBox::onKeyPress),false);
}
bool inputBox::onKeyPress(GdkEventKey* event){
huidigeInput = get_text();
std::cout << huidigeInput;
set_text("");
return false;
}
这是我的inputbox.h文件class inputBox : public::Gtk::Entry{
public:
inputBox();
bool onKeyPress(GdkEventKey*);
Glib::ustring huidigeInput;
};
希望有人可以找到错误的问题!
最佳答案
让我们看看触发按键事件时的一系列 Action 。
Entry
中的文本并将其发送到std::cout
。 Entry
中的文本中。 自然地,如果在将字符添加到文本之前抓取文本,则不会抓取该字符。您必须等到添加字符之后。
您的回调在默认处理程序之前运行的原因是您将
false
指定为after
的connect()
参数。如果省略此参数或指定true
,则默认处理程序将在您之前运行。当然,默认处理程序可能认为事件已被完全处理,并且阻止了在该事件之后运行的处理程序。为确保获得所需的行为,覆盖虚拟函数
on_key_press_event()
可能比显式连接到signal_key_press_event()
更好。这使您可以在默认处理返回其值之前插入代码。bool inputBox::on_key_press_event(GdkEventKey * key_event)
{
// Start with the default processing.
bool retval = Gtk::Entry::on_key_press_event(key_event);
// Now your functionality:
huidigeInput = get_text();
std::cout << huidigeInput;
set_text("");
// Preserve what the default processing wanted to return.
return retval;
}
关于c++ - GTKmm输入到控制台输出滞后1个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64472050/