创建输入字段

Creating an entryfield

本文关键字:字段 输入 创建      更新时间:2023-10-16

如何在gtkmm-2.4中创建一个输入字段(textfield) ?

下面的代码片段显示了一个包含三个按钮的容器。我想知道如何添加一个输入字段到它。我在gtkmm中找不到任何该类。

headerfile:

#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H
#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/window.h>
class HelloWorld : public Gtk::Window
{
public:
   HelloWorld();
   virtual ~HelloWorld();
protected:
   // Signal handlers:
   // Our new improved on_button_clicked(). (see below)
   void on_button_clicked(Glib::ustring data);
  // Child widgets:
   Gtk::HBox m_box1;
   Gtk::Button m_button1, m_button2, m_button3;
};
#endif // GTKMM_EXAMPLE_HELLOWORLD_H 

cpp-file:

#include "helloworld.h"
#include <iostream>
HelloWorld::HelloWorld()
: m_button1("Button 1"),
m_button2("Button 2"),
m_button3("Button 3")
{
   set_title("Hello Buttons!");
   set_border_width(10);
   add(m_box1);
   m_button1.signal_clicked().connect(sigc::bind<Glib::ustring>(
    sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 1"));
   m_box1.pack_start(m_button1);
   m_button1.show();
   m_button2.signal_clicked().connect(sigc::bind<-1, Glib::ustring>(
    sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 2"));
   m_box1.pack_start(m_button2);
   m_button2.show();
   m_button3.signal_clicked().connect(sigc::bind<-1, Glib::ustring>(
    sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 3"));
   m_box1.pack_start(m_button3);
   m_button3.show();
   m_box1.show();
   }
   HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked(Glib::ustring data)
{
   std::cout << "Hello World - " << data << " was pressed" << std::endl;
}

您需要使用Gtk::Entry:

  1. https://developer.gnome.org/gtkmm/unstable/classGtk_1_1Entry.html
  2. https://developer.gnome.org/gtkmm-tutorial/unstable/sec-text-entry.html.en

或Gtk:: TextView:

  1. https://developer.gnome.org/gtkmm/stable/classGtk_1_1TextView.html
  2. https://developer.gnome.org/gtkmm-tutorial/stable/chapter-textview.html.en