【发布时间】:2017-04-24 07:07:03
【问题描述】:
我现在正在做 Gtkmm GUI 编程,我是这种编程语言的新手。 如何让用户只在 Gtkmm Entry 文本框中输入数值。
【问题讨论】:
-
关键事件必须自己处理。
我现在正在做 Gtkmm GUI 编程,我是这种编程语言的新手。 如何让用户只在 Gtkmm Entry 文本框中输入数值。
【问题讨论】:
您需要为您的条目添加一个按键释放信号,例如这是我的条目“m_EntryAmount”
m_EntryAmount.signal_key_release_event().connect(sigc::mem_fun(*this, &Vente::entryKeyReleaseAmount));
并添加信号功能
bool Sales::entryKeyReleaseAmount(GdkEventKey* /* event */ )
{
if(m_EntryAmount.get_text()!="" && is_number(m_EntryAmount.get_text()) ){
//now the Entry is not empty and its a number
//do what you want here
}else{
//here the Entry is not a number you can just delete it
m_EntryAmount.set_text("");
}
return true;
}
并添加 is_number 函数
bool Vente::is_number(const string& s)
{
return !s.empty() && std::find_if(s.begin(),
s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
}
这只是一个简单的例子,你可以用自己的方式自己做
【讨论】:
p 而不是 0 或其他)并兴高采烈地运行应用程序清除整个条目。不太好!另一种方法当然是过滤 presses,并且只屏蔽不被接受的字符。
使用 Gtk::SpinButton 而不是 Gtk::Entry 只允许数字输入。
【讨论】: