如何使用c ++在gtkmm:gtk :: Listbox中添加文本框

时间:2015-12-13 16:20:39

标签: c++ listbox gtk gtkmm

我想在cbox中使用Gtkmm在Listbox中添加这种类型的文本。

list box with text

你能告诉我怎么办吗?

2 个答案:

答案 0 :(得分:2)

这是我创建的用于制作ListBox的示例。

//=======================================================
// GTKMM3/C++11 ListBox Example / W.Moore
//=======================================================
#include <gtkmm.h>
#include <iostream>
#include <string>
#include <vector>
#include <initializer_list>

using namespace std;

//======================================================
// My List Row
//======================================================
class MyListRow : public Gtk::ListBoxRow
{
public:
    MyListRow(const string text)
      : label{text}
    {
       add(label);
       set_halign(Gtk::Align::ALIGN_START);
       label.set_size_request(150); 
       show_all_children();
    }
protected:
    Gtk::Label label;
};

//======================================================
// My List Box
//======================================================
class MyListBox : public Gtk::ListBox
{
public:
    MyListBox();
    MyListBox(initializer_list<string> list);
    void api_AddRow(string line);
    void api_Clear();
};

inline MyListBox::MyListBox()
{
}

inline MyListBox::MyListBox(initializer_list<string> list) {
  for (auto s : list) {
    api_AddRow(s);
  }
}

inline void MyListBox::api_AddRow(string text)
{
    auto row = Gtk::manage(new MyListRow{text});
    append(*row);
    row->show();
}

inline void MyListBox::api_Clear() {
    vector<Gtk::Widget* w> children = get_children();
    foreach (widget* w : children) {
        remove(*w);
        delete w;
    }
}

//======================================================
// My Window
//======================================================
class MyWindow : public Gtk::Window {
  public:
    MyWindow();

  protected:
    MyListBox listbox1 {
        "List Item 1",
        "List Item 2",
        "List Item 3",
        "List Item 4",
        "List Item 5",
        "List Item 6",
        "List Item 7"
    };

};

inline MyWindow::MyWindow() {
  add(listbox1);
  set_title("ListBox Example");
  set_border_width(6);
  set_default_size(300, 100);

  show_all_children();
}

//======================================================
// Main
//======================================================
int main(int argc, char** argv) {
    auto appMain = Gtk::Application::create(argc, argv, "org.gtkmm.example");
    MyWindow MyWindow1;
    appMain->run(MyWindow1);
    return 0;
}

上面的内容仍然不是很有用,因为它缺少像标准Windows文本框这样的滚动条,如果你过度填充它,那么它会使窗口长于屏幕。这是给它滚动条的代码:

//=======================================================
// GTKMM3/C++11 ListBox Example / W.Moore
//=======================================================
#include <gtkmm.h>
#include <iostream>
#include <string>
#include <sstream>
#include <initializer_list>

using namespace std;

//======================================================
// My List Row
//======================================================
class MyListRow : public Gtk::ListBoxRow
{
public:
    MyListRow(const string text)
      : c1{text}
    {
       add(c1);
       set_halign(Gtk::Align::ALIGN_START);
       show_all_children();
    }
    Gtk::Label c1;
};

//======================================================
// List Box with scrollbars
//======================================================
class ListBoxScroll : public Gtk::ScrolledWindow {
  public:
    ListBoxScroll();
    ListBoxScroll(initializer_list<string> list);
    void api_AddRow(string line);
    void api_AddRows(vector<string>& lines);
    void api_Clear();

    function<void(string label)> fun_selected;

protected:
    Gtk::Box               hbox {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::ListBox           listbox;

    void sig_row_selected(Gtk::ListBoxRow* listboxrow);
};

//======================================================
// ListBoxScroll
//======================================================
inline ListBoxScroll::ListBoxScroll()
{
    add(hbox);
    hbox.pack_start(listbox, Gtk::PackOptions::PACK_EXPAND_WIDGET);
    listbox.signal_row_selected().connect(sigc::mem_fun(*this, ListBoxScroll::sig_row_selected));
    show_all_children();
}

inline ListBoxScroll::ListBoxScroll(initializer_list<string> list) {
  for (auto s : list) {
    api_AddRow(s);
  }
}

inline void ListBoxScroll::api_Clear() {
  listbox.unselect_all();
  vector<Gtk::Widget*> children = listbox.get_children();
  for (Widget* w : children) {
    listbox.remove(*w);
    delete w;
  }
}

inline void ListBoxScroll::api_AddRow(string text)
{
    auto row = Gtk::manage(new MyListRow{text});
    listbox.append(*row);
    row->show();
}

inline void ListBoxScroll::api_AddRows(vector<string>& lines) {
  for (string& L : lines) {
    api_AddRow(L);
  }
}

inline void ListBoxScroll::sig_row_selected(Gtk::ListBoxRow* listboxrow) {
  if (fun_selected && (listboxrow != nullptr)) {
    MyListRow* listrow = (MyListRow*)listboxrow;
    Glib::ustring us = listrow->c1.get_text();
    fun_selected(us.c_str());
  }
}


class WnViewer : public Gtk::Window {
  public:
    WnViewer();
    ~WnViewer();

  private:
    Gtk::Box          m_vbox       {Gtk::ORIENTATION_VERTICAL};
    Gtk::Box          m_box        {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::Box          m_boxleft    {Gtk::ORIENTATION_VERTICAL};
    Gtk::Box          m_boxbtn     {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::Button       m_btnClear   {"Clear"};
    Gtk::Button       m_btnRefresh {"Refresh"};
    ListBoxScroll     m_listbox;
    Gtk::TextView     m_textview;
};

inline WnViewer::WnViewer() {
  set_title("Viewer");
  set_border_width(6);
  set_default_size(600, 600);

  add(m_vbox);
  m_vbox.pack_start(m_boxbtn, Gtk::PackOptions::PACK_SHRINK);
  m_vbox.pack_start(m_box);
  m_boxbtn.pack_start(m_btnClear,   Gtk::PackOptions::PACK_SHRINK);
  m_boxbtn.pack_start(m_btnRefresh, Gtk::PackOptions::PACK_SHRINK);

  m_box     .pack_start(m_boxleft,  Gtk::PackOptions::PACK_SHRINK);
  m_box     .pack_start(m_textview, Gtk::PackOptions::PACK_EXPAND_WIDGET);
  m_boxleft .pack_start(m_listbox,  Gtk::PackOptions::PACK_EXPAND_WIDGET);
  m_listbox.set_size_request(200);


  for(int i=0; i < 30; i++) {
    stringstream x;
    x << "Testing " << i << "\n";
    m_listbox.api_AddRow(x.str().c_str());
  }

  auto lamba_clear = [&]() {
      m_listbox.api_Clear();
  };

  auto lamba_refresh = [&]() {
    for(int i=0; i < 30; i++) {
      stringstream x;
      x << "Testing " << i << "\n";
      m_listbox.api_AddRow(x.str().c_str());
    }
  };

  m_btnClear   .signal_clicked().connect(lamba_clear);
  m_btnRefresh .signal_clicked().connect(lamba_refresh);

  m_listbox.fun_selected = [](string label) {
      cout << "selected:" << label << "\n";
  };

  show_all_children();
}

inline WnViewer::~WnViewer() {

}

//======================================================
// Main
//======================================================
int main(int argc, char** argv) {
    auto appMain = Gtk::Application::create(argc, argv, 
                       "org.gtkmm.example");
    WnViewer WnViewer1;
    appMain->run(WnViewer1);
    return 0;
}

我认为gtkmm3的效果非常好。但是你需要围绕所有组件创建包装器以使它们像普通的c#小部件一样工作,因为它们比c#.net小部件更小的构建块(在我看来无论如何)。

一些松散的结局:

  • 如何在此示例中将标签水平宽度缩小到最小标签高度?
  • 如何让veritical滚动条总是更像标准的Windows滚动条?

(真的希望Gnome / Gtk的人们可以在下面添加如下所示的复合小部件,而不需要每次有人需要像c#/ .net这样简单的列表框时重新发明轮子。)

以上是一个更详细的例子:

#ifndef WIDGET_LISTBOX_H
#define WIDGET_LISTBOX_H

//=======================================================
// GTKMM3 ListBox Example: C++11 / W.Moore
//=======================================================
#include <gtkmm.h>
#include <iostream>
#include <string>
#include <initializer_list>

using namespace std;

//======================================================
// List Row
//======================================================
class ListRow : public Gtk::ListBoxRow
{
public:
    ListRow(const string text) : c1{text} {
       add(c1);
       set_halign(Gtk::Align::ALIGN_START);
       show_all_children();
    }
    Gtk::Label c1;
};

//======================================================
// List Box with scrollbars
//======================================================
class ListBoxScroll : public Gtk::ScrolledWindow {
  public:
    ListBoxScroll();
    ListBoxScroll(initializer_list<string> list);

    void           api_AddRow(string line);
    void           api_AddRows(vector<string>& lines);
    void           api_Clear();
    void           api_Focus();
    vector<string> api_GetAll();
    int            api_GetCount();
    string         api_GetAt(int i);
    string         api_GetSelected();
    void           api_DelSelected();
    void           api_DelAll();

    function<void(string label)> fun_selected;   //single-click or cursor selection
    function<void(string label)> fun_activated;  //double-click

protected:
    Gtk::Box               hbox {Gtk::ORIENTATION_HORIZONTAL};
    Gtk::ListBox           listbox;
    void sig_row_selected  (Gtk::ListBoxRow* listboxrow);
    void sig_row_activated (Gtk::ListBoxRow* listboxrow);

};

inline void ListBoxScroll::api_Focus() {
  grab_focus();
  listbox.grab_focus();
}

//======================================================
// ListBoxScroll: Constructor 1
//======================================================
inline ListBoxScroll::ListBoxScroll()
{
    add(hbox);
    hbox.pack_start(listbox, Gtk::PackOptions::PACK_EXPAND_WIDGET);

    //make double click active row
    listbox.set_activate_on_single_click(false);

    listbox.signal_row_activated().connect(sigc::mem_fun(
                    *this,
                    ListBoxScroll::sig_row_activated)
    );
    listbox.signal_row_selected().connect(sigc::mem_fun(
                    *this,
                    ListBoxScroll::sig_row_selected)
    );
    listbox.set_can_focus();
    show_all_children();

}

//======================================================
// ListBoxScroll: Constructor 2
//======================================================
inline ListBoxScroll::ListBoxScroll(initializer_list<string> list) {
  for (auto s : list) {
    api_AddRow(s);
  }
}

//======================================================
// ListBoxScroll: Delete All
//======================================================
inline void ListBoxScroll::api_DelAll() {
  api_Clear();
}

//======================================================
// ListBoxScroll: Get All
//======================================================
inline vector<string> ListBoxScroll::api_GetAll() {
  vector<Gtk::Widget*> children = listbox.get_children();
  vector<string>       strlist;

  for (Widget* row : children) {
    ListRow* rowi = (ListRow*)row;
    strlist.push_back(rowi->c1.get_text().c_str());
  }

  return strlist;
}

//======================================================
// ListBoxScroll: Get At
//======================================================
inline string ListBoxScroll::api_GetAt(int i) {
  vector<Gtk::Widget*> children = listbox.get_children();
  Widget*  row  = children.at(i);
  ListRow* rowi = (ListRow*)row;
  return rowi->c1.get_text().c_str();
}

//======================================================
// ListBoxScroll: Get Count
//======================================================
inline int ListBoxScroll::api_GetCount() {
  vector<Gtk::Widget*> children = listbox.get_children();
  return (int) children.size();
}

//======================================================
// ListBoxScroll: Delete Selected
//======================================================
inline void ListBoxScroll::api_DelSelected() {
  ListRow* row_selected = (ListRow*) listbox.get_selected_row();

  //Hackishly find selected position since GTK lacks a better way...
  vector<Gtk::Widget*> wrow = listbox.get_children();
  int row_posi              = -1;
  int row_count             = (int) wrow.size();
  for(int i = 0; i <row_count; i++) {
    ListRow* row_i = (ListRow*)wrow[i];
    //cout << "row_i:" << i << " (" << row_i->c1.get_text() << ")\n";
    if (row_i == row_selected) {
        row_posi = i;
        break;
    }
  }
  //cout << "row_pos:" << row_posi << "\n";

  if (row_posi != -1) {

    // Unselect Current
    listbox.unselect_row();

    Gtk::ListBoxRow* select_next = nullptr;

    // Move Selection Line by One
    if ((row_posi + 1) < row_count) {
      select_next = (Gtk::ListBoxRow*)wrow[row_posi+1];
    }
    else if ((row_posi-1) >= 0) {
      select_next = (Gtk::ListBoxRow*)wrow[row_posi-1];
    }

    // Remove Initially Selected
    //const Glib::RefPtr<Gtk::Adjustment> hadj = get_hadjustment(); //save hpos before remove
    const Glib::RefPtr<Gtk::Adjustment> vadj = get_vadjustment(); //save hpos before remove
    double vvalue = vadj->get_value();

    listbox.remove(*row_selected);
    delete row_selected;

    // On Remove, for some reason...
    // the widget loses focus and vadjustment gets reset
    vadj->set_value(vvalue);
    listbox.grab_focus(); //need this?
    listbox.select_row(*select_next);
    select_next->grab_focus();
  }
}

//======================================================
// ListBoxScroll: Get Selected
//======================================================
inline string ListBoxScroll::api_GetSelected() {
  Gtk::ListBoxRow* row = listbox.get_selected_row();
  ListRow* rowi = (ListRow*) row;
  return rowi->c1.get_text().c_str();
}

//======================================================
// ListBoxScroll: Clear
//======================================================
inline void ListBoxScroll::api_Clear() {
  vector<Gtk::Widget*> children = listbox.get_children();
  listbox.unselect_all();
  for (Widget* w1 : children) {
    listbox.remove(*w1);
  }

  //Allow GUI to update
  while (gtk_events_pending()) {
    gtk_main_iteration_do(false);
  }

  for (Widget* w2 : children) {
    delete w2;
  }

}

//======================================================
// ListBoxScroll: AddRow
//======================================================
inline void ListBoxScroll::api_AddRow(string text)
{
    auto row = Gtk::manage(new ListRow{text});
    listbox.append(*row);
    row->show();
}

//======================================================
// ListBoxScroll: AddRows
//======================================================
inline void ListBoxScroll::api_AddRows(vector<string>& lines) {
  for (string& L : lines) {
    api_AddRow(L);
  }
}

//======================================================
// ListBoxScroll: sig_row_selected
//======================================================
inline void ListBoxScroll::sig_row_selected(Gtk::ListBoxRow* listboxrow) {
  if (fun_selected && (listboxrow != nullptr)) {
    //cout << "<selected>\n";
    ListRow* listrow = (ListRow*)listboxrow;
    Glib::ustring us = listrow->c1.get_text();
    fun_selected(us.c_str());
  }
}

//======================================================
// ListBoxScroll: sig_row_activated
//======================================================
inline void ListBoxScroll::sig_row_activated(Gtk::ListBoxRow* listboxrow) {
  if (fun_activated && (listboxrow != nullptr)) {
    //cout << "<activated>\n";
    ListRow* listrow = (ListRow*)listboxrow;
    Glib::ustring us = listrow->c1.get_text();
    fun_activated(us.c_str());
  }
}


#endif // WIDGET_LIST_BOX_H

答案 1 :(得分:1)

对于这个非常简单的情况,您可能想要使用gtkmm的ListViewText小部件: https://developer.gnome.org/gtkmm/stable/classGtk_1_1ListViewText.html

以下是一些示例代码: https://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/treeview/listviewtext/examplewindow.cc

但是,如果你想做更复杂的事情,你需要使用完整的Gtk :: TreeView类。

我不认为Gtk :: ListBox适合提供这样的简单值列表。实际上,您可能想要使用Gtk :: ComboBox - 甚至可能是简单的Gtk :: ComboBoxText小部件: https://developer.gnome.org/gtkmm/stable/classGtk_1_1ComboBoxText.html

这是一些Gtk :: ComboBoxText示例代码: https://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/combobox/text/examplewindow.cc