问题
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