- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 C++ 中使用 Gtkmm 在列表框中添加这种类型的文本。
你能建议我怎么做吗?
最佳答案
这是我为制作上述列表框而创建的示例。
//=======================================================
// 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;
}
上面的内容仍然不是很有用,因为它缺少像标准窗口文本框那样的滚动条,如果你过度填充它,那么它会使窗口比屏幕长。这是为其提供滚动条的代码:
//=======================================================
// 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 小部件更小的构建 block (无论如何在我看来)。
一些 Unresolved 问题:
(真的希望 Gnome/Gtk 的人们可以将如下所示的复合小部件添加到 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
关于c++ - 如何使用 C++ 在 gtkmm:gtk::Listbox 中添加文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253297/
我创建了一个用户可以添加测试的字段。这一切运行顺利我只希望当用户点击(添加另一个测试)然后上一个(添加另一个测试)删除并且这个显示在新字段中。 所有运行良好的唯一问题是点击(添加另一个字段)之前添加另
String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在努力将 jQuery 滚动功能添加到 nav-tab (Bootstrap 3)。我希望用户能够选择他们想要的选项卡,并在选项卡内容中有一个可以平滑滚动到 anchor 的链接。这是我的代码,可
我正在尝试在用户登录后再添加 2 个 ui 选项卡。首先,我尝试做一个之后。 $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
我有一个包含选择元素的表单,我想通过选择添加和删除其中一些元素。这是html代码(这里也有jsfiddle http://jsfiddle.net/txhajy2w/):
正在写这个: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) 等同于: view.backgroundColor = UICo
好的,如果其中有任何信息,我想将这些列添加到一起。所以说我有 账户 1 2 3 . 有 4 个帐户空间,但只有 3 个帐户。我如何创建 java 脚本来添加它。 最佳答案 Live Example H
我想知道是否有一种有效的预制算法来确定一组数字的和/差是否可以等于不同的数字。示例: 5、8、10、2,使用 + 或 - 等于 9。5 - 8 = -3 + 10 = 7 + 2 = 9 如果有一个预
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我的 Oracle SQL 查询如下- Q1- select hca.account_number, hca.attribute3, SUM(rcl.extended_amou
我正在阅读 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
我正在尝试添加一个“加载更多”按钮并限制下面的结果,这样投资组合页面中就不会同时加载 1000 个内容,如下所示:http://typesetdesign.com/portfolio/ 我对 PHP
我遇到这个问题,我添加了 8 个文本框,它工作正常,但是当我添加更多文本框(如 16 个文本框)时,它不会添加最后一个文本框。有人遇到过这个问题吗?提前致谢。 Live Link: JAVASCRIP
add/remove clone first row default not delete 添加/删除克隆第一行默认不删除&并获取正确的SrNo(例如:添加3行并在看到问题后删除SrNo.2)
我编码this ,但删除按钮不起作用。我在控制台中没有任何错误.. var counter = 0; var dataList = document.getElementById('materi
我有一个类似数组的对象: [1:数组[10]、2:数组[2]、3:数组[2]、4:数组[2]、5:数组[3]、6:数组[1]] 我正在尝试删除前两个元素,执行一些操作,然后将它们再次插入到同一位置。
使用的 Delphi 版本:2007 你好, 我有一个 Tecord 数组 TInfo = Record Name : String; Price : Integer; end; var Info
我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件) 我的js代
title 323 323 323 title 323 323 323 title 323 323 323 JS $(document).keydown(function(e){
我是一名优秀的程序员,十分优秀!