如何在GtkGrid中对齐widget

时间:2016-05-02 22:52:03

标签: c++ gtk

我是新GTK,并尝试编写一个小型界面。但是,我没有找到一个函数来帮助对齐GtkGrid中的小部件。下图显示(1)按钮尺寸不同(2)标签内的文字不是左对齐的。我该怎么调整呢?

#include <gtk/gtk.h>
//#include <iostream>
//using namespace std;



void load_file(GtkWidget *widget, gpointer data)
{
GtkWidget *dialog;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
gint res;

dialog = gtk_file_chooser_dialog_new ("Open File",
                                      NULL,
                                      action,
                                      "Cancel",
                                      GTK_RESPONSE_CANCEL,
                                      "Open",
                                      GTK_RESPONSE_ACCEPT,
                                      NULL);

res = gtk_dialog_run (GTK_DIALOG (dialog));
if (res == GTK_RESPONSE_ACCEPT)
  {
    char *filename;
    GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
    filename = gtk_file_chooser_get_filename (chooser);
    //open_file (filename);
    //g_free (filename);
  }

gtk_widget_destroy (dialog);

}


int main( int   argc,char *argv[] )
{
    gtk_init (&argc, &argv);

    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window),800,400);

    GtkWidget *lbl_ldmap = gtk_label_new("Load Map");
    GtkWidget *lbl_capacity = gtk_label_new("Capacity");
    GtkWidget *lbl_npassenger = gtk_label_new("Passengers");
    GtkWidget *lbl_nveh = gtk_label_new("Vehicles");
    GtkWidget *lbl_optby = gtk_label_new("Optimize By");

//    gtk_label_set_justify(GTK_LABEL(lbl_ldmap),gtk_label_set_justify);


    GtkWidget *btn_fchoose = gtk_button_new_with_label("...");
    GtkWidget *btn_start = gtk_button_new_with_label("Start");
    GtkWidget *btn_save = gtk_button_new_with_label("Save");
    GtkWidget *btn_about = gtk_button_new_with_label("About");
    GtkWidget *btn_close = gtk_button_new_with_label("Close");


    GtkWidget *mainwindow = gtk_grid_new();
    gtk_grid_set_row_spacing (GTK_GRID (mainwindow), 16);

    gtk_grid_attach(GTK_GRID(mainwindow),lbl_ldmap,0,0,2,2);  
    gtk_grid_attach(GTK_GRID(mainwindow),btn_fchoose,3,0,2,2); 
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_capacity,0,2,2,2);
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_npassenger,0,4,2,2);
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_nveh,0,6,2,2);
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_optby,0,8,2,2);

    gtk_grid_attach(GTK_GRID(mainwindow),btn_start,0,10,5,2);
    gtk_grid_attach(GTK_GRID(mainwindow),btn_save,6,10,5,2);
    gtk_grid_attach(GTK_GRID(mainwindow),btn_about,0,12,5,2);
    gtk_grid_attach(GTK_GRID(mainwindow),btn_close,6,12,5,2);


    GtkWidget *area = gtk_drawing_area_new();
    gtk_grid_attach(GTK_GRID(mainwindow),area,13,0,20,20);

    gtk_container_add(GTK_CONTAINER(window),mainwindow);

    g_signal_connect(G_OBJECT(btn_fchoose),"clicked",
                       G_CALLBACK(load_file),NULL);





    gtk_widget_show_all(window);

    gtk_main ();

    return(0);
}

enter image description here

1 个答案:

答案 0 :(得分:2)

像GtkBox和GtkGrid这样的容器小部件不会填充可用空间,除非他们知道如何填充它们。您需要通过在这些小部件上设置hexpandhalignvexpand和/或valign属性来扩展GtkBox或GtkGrid的一个或多个子级(不在网格本身!)。有关详细信息,请参阅this page