问题描述
我正在使用Ubuntu 20.04。我需要将GTK::Entry
小部件的框架配置为不超过11个字符。我以为我可以使用此代码set_max_width_chars
Gtk::Entry nomUsb;
nomUsb.set_can_focus(true);
nomUsb.set_max_length(11);
nomUsb.set_max_width_chars(11);
boiteBoutonsH1.pack_start(nomUsb,Gtk::PACK_SHRINK);
但是结果给出了这个(20个字符):
有什么想法吗?
解决方法
我怀疑这是一些代码,可以执行您想要的操作:
#include <gtkmm.h>
class MainWindow : public Gtk::Window
{
public:
MainWindow()
{
// Can't type in more than 11 chars (copy/paste will truncate):
m_11CharMax.set_max_length(11);
// Sets the width of the entry to about 11 characters wide (depends on the character):
m_11CharMax.set_width_chars(11);
add(m_11CharMax);
show_all();
}
private:
Gtk::Entry m_11CharMax;
};
int main(int argc,char **argv)
{
auto app = Gtk::Application::create(argc,argv,"so.question.q63568716");
MainWindow w;
return app->run(w);
}
以下Makefile将为您构建该文件(假设代码位于名为main.cpp
的文件中:
all: main.cpp
g++ main.cpp -o example.out `pkg-config gtkmm-3.0 --cflags --libs`
程序example.out
将创建一个以Gtk::Entry
作为其唯一子控件的窗口。该条目的宽度为 11个字符(请注意,这取决于字符。例如,m
大于UI字体中的l
)。另外,它接受的字符数不能超过11个:
如果您尝试将11个以上的字符复制并粘贴到其上,则该条目将自动截断以适合它。即使您调整它的大小,也不能将其输入超过11个字符: