Search code examples
c++textfieldgtkmm

Creating an entryfield


How can I make an entryfield (textfield) in gtkmm-2.4?

Here's a code snippet that shows a container holding three buttons. I would like to know how to add an entryfield to it. I cannot find any class for this in 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;
}

Solution

  • You need to use a 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

    or 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