限制GtkComboBox弹出高度
我正在寻找限制 GtkComboBox 弹出窗口高度的方法,当达到该高度时,控件将获得滚动条。我找不到办法做到这一点。列表从窗口的顶部流到底部(尝试附加的代码)。我检查了 API,但找不到有用的方法。我是 GTK+ 的新手,我已经在 google 上搜索了好几天了。
#include <gtk/gtk.h>
int main(int argc, char** argv) {
GtkWidget* frame;//main frame
GtkWidget* combobox;
GtkWidget* listbox;
GtkWidget* okbutton;
GtkWidget* cancelbutton;
GtkWidget* hbox_buttons;
GtkWidget* vbox;
gtk_init(&argc, &argv);
//create widgets
frame = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/*Combobox issues*/
GtkListStore* list_store;
list_store = NULL;
GtkTreeIter iter;
list_store=gtk_list_store_new(1,G_TYPE_STRING);
int i;
for(i=0; i<100; i++) {
gtk_list_store_append(list_store, &iter);
gtk_list_store_set(list_store, &iter,0,"Residental", -1);
}
combobox = gtk_combo_box_new_with_model((GtkTreeModel *)list_store);
g_object_unref(G_OBJECT(list_store));
GtkCellRenderer* cell = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), cell, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), cell, "text", 0, NULL);
listbox = gtk_list_new();
okbutton = gtk_button_new_with_label("Ok");
cancelbutton = gtk_button_new_with_label("Cancel");
//create containers
hbox_buttons = gtk_hbox_new(FALSE, 5);
vbox = gtk_vbox_new(FALSE, 5);
//Pack things
gtk_container_add(GTK_CONTAINER(frame),vbox);
gtk_box_pack_start(GTK_BOX(vbox), combobox, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(vbox), listbox, TRUE, TRUE, 5);
//pack buttons
gtk_box_pack_start(GTK_BOX(hbox_buttons), okbutton, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(hbox_buttons), cancelbutton, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(vbox), hbox_buttons, FALSE, TRUE, 5);
//gtk_box_pack_start(vbox, listbox, TRUE, TRUE, 5);
g_signal_connect_swapped(G_OBJECT(frame), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(frame));
gtk_widget_show_all(frame);
gtk_main();
return 0;
}
I was finding the way to limit of height of popup window for GtkComboBox and when that height is reached the control will get scrollbars. I cannot find wa way to do that. the list flows from top to bottom of window (try the attached code). I have checked API and I cannot find useful method. I'm new to GTK+ and I have searched google for days.
#include <gtk/gtk.h>
int main(int argc, char** argv) {
GtkWidget* frame;//main frame
GtkWidget* combobox;
GtkWidget* listbox;
GtkWidget* okbutton;
GtkWidget* cancelbutton;
GtkWidget* hbox_buttons;
GtkWidget* vbox;
gtk_init(&argc, &argv);
//create widgets
frame = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/*Combobox issues*/
GtkListStore* list_store;
list_store = NULL;
GtkTreeIter iter;
list_store=gtk_list_store_new(1,G_TYPE_STRING);
int i;
for(i=0; i<100; i++) {
gtk_list_store_append(list_store, &iter);
gtk_list_store_set(list_store, &iter,0,"Residental", -1);
}
combobox = gtk_combo_box_new_with_model((GtkTreeModel *)list_store);
g_object_unref(G_OBJECT(list_store));
GtkCellRenderer* cell = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), cell, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), cell, "text", 0, NULL);
listbox = gtk_list_new();
okbutton = gtk_button_new_with_label("Ok");
cancelbutton = gtk_button_new_with_label("Cancel");
//create containers
hbox_buttons = gtk_hbox_new(FALSE, 5);
vbox = gtk_vbox_new(FALSE, 5);
//Pack things
gtk_container_add(GTK_CONTAINER(frame),vbox);
gtk_box_pack_start(GTK_BOX(vbox), combobox, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(vbox), listbox, TRUE, TRUE, 5);
//pack buttons
gtk_box_pack_start(GTK_BOX(hbox_buttons), okbutton, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(hbox_buttons), cancelbutton, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(vbox), hbox_buttons, FALSE, TRUE, 5);
//gtk_box_pack_start(vbox, listbox, TRUE, TRUE, 5);
g_signal_connect_swapped(G_OBJECT(frame), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(frame));
gtk_widget_show_all(frame);
gtk_main();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的,所以我最终使用了 wxComboCtrl 这是非常可定制的!
It is not possible so I ended up using wxComboCtrl which is very customizable!