如何配置小部件Entry,以使框架的长度不超过11​​个字符

问题描述

我正在使用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个字符):

enter image description here

有什么想法吗?

解决方法

我怀疑这是一些代码,可以执行您想要的操作:

#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个:

enter image description here

如果您尝试将11个以上的字符复制并粘贴到其上,则该条目将自动截断以适合它。即使您调整它的大小,也不能将其输入超过11个字符:

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...