Force numeric input in a Gtk::Entry widget

回眸只為那壹抹淺笑 提交于 2021-01-27 11:33:10

问题


I'm searching for a possible method to only allow numeric input in a Gtk::Entry widget, without relying on SpinButtons. The matter is, I found a template for this (link), but it just won't work. I can compile it along my other code, but if I want to declare an instance with

NumericEntry<int> int_entry(1,0,10);

it tells me

expected ‘,’ or ‘...’ before numeric constant

The second part is, that I have no clear idea how to pack this entry, because I get a

can't convert to widget

error when using

functionname.pack_start(int_entry())

I guess there is a stupid error an my part (bad combination of C++ and Gtkmm newbie), so any help is appreciated.


回答1:


One way to only allow numbers is to subclass Gtk::Entry and override the on_insert_text() virtual function. In that virtual function, you can validate the text entered and only call the base class's on_insert_text() when the text validates.

void NumberEntry::on_insert_text(const Glib::ustring& text, int* position)
{
    // allow only numbers to be entered
    if (contains_only_numbers(text))
        Gtk::Entry::on_insert_text(text, position);
}


来源:https://stackoverflow.com/questions/10279579/force-numeric-input-in-a-gtkentry-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!