ComboBox med ett Entry

En ComboBox kan innehålla en Entry-komponent för inmatning av godtycklig text genom att ange true för konstruktorns has_entry-parameter.

Textkolumnen

So that the Entry can interact with the drop-down list of choices, you must specify which of your model columns is the text column, with set_entry_text_column(). For instance:

m_combo.set_entry_text_column(m_columns.m_col_name);

When you select a choice from the drop-down menu, the value from this column will be placed in the Entry.

The entry

Eftersom användaren kan mata in godtycklig text är en aktiv modellrad inte tillräckligt för att säga till oss vilken text som användaren har matat in. Du bör därför erhålla Entry-komponenten med metoden ComboBox::get_entry() och anropa get_text() på den.

Svara på ändringar

When the user enters arbitrary text, it may not be enough to connect to the changed signal, which is emitted for every typed character. It is not emitted when the user presses the Enter key. Pressing the Enter key or moving the keyboard focus to another widget may signal that the user has finished entering text. To be notified of these events, connect to the Entry's activate signal (available since gtkmm 4.8), and add a Gtk::EventControllerFocus and connect to its leave signal, like so

auto entry = m_Combo.get_entry();
if (entry)
{
  // Alternatively you can connect to m_Combo.signal_changed().
  entry->signal_changed().connect(sigc::mem_fun(*this,
    &ExampleWindow::on_entry_changed));
  entry->signal_activate().connect(sigc::mem_fun(*this,
    &ExampleWindow::on_entry_activate));
  // The Entry shall receive focus-leave events.
  auto controller = Gtk::EventControllerFocus::create();
  m_ConnectionFocusLeave = controller->signal_leave().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_entry_focus_leave));
  entry->add_controller(controller);
}

The changed signals of ComboBox and Entry are both emitted for every change. It doesn't matter which one you connect to. But the EventControllerFocus must be added to the Entry.

Fullständigt exempel

ComboBox with Entry

Källkod

Enkelt textexempel

ComboBoxText with Entry

Källkod