C/C++中如何将元素与GTK库对齐

ubby3x7f  于 2023-01-03  发布在  C/C++
关注(0)|答案(2)|浏览(192)

我在C/C++中使用GTK+库时遇到了一些问题。我有一个简单的GTK表,其中第一列和第三列包含带有描述的标签。特别是第一列是正在执行的测试的名称。第三列是测试运行状态的描述。
第二列包含进度条,每行一个,从图像中可以看到,它们没有与同一行上的标签对齐。
我该如何解决这个问题?下面是一个简短且可重复的示例,它显示了我的问题:

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

gtk_init (&argc, &argv);
GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_fullscreen(GTK_WINDOW(win)); 
g_signal_connect (win, "delete_event", G_CALLBACK (gtk_main_quit),NULL);
GtkWidget *names[7], *states[7], *bars[7];
GtkWidget *table = gtk_table_new (7, 3, TRUE);
for ( int i = 0 ; i < 7 ; i++ ){

    names[i] = gtk_label_new( std::string("Name" + std::to_string(i)).c_str() );
    states[i] = gtk_label_new("Waiting Mode");
    bars[i] = gtk_progress_bar_new();
    gtk_table_attach_defaults (GTK_TABLE (table), names[i] , 0, 1, i, i+1);     
    gtk_table_attach_defaults (GTK_TABLE (table), bars[i] , 1, 2, i, i+1);      
    gtk_table_attach_defaults (GTK_TABLE (table), states[i] , 2, 3, i, i+1);        
}

gtk_container_add (GTK_CONTAINER (win), table);
gtk_widget_show_all (win);
gtk_main ();
return 0;
}

您将获得的GUI如下所示:img

u3r8eeie

u3r8eeie1#

您需要使用gtk_widget_valign_set设置进度条小部件的垂直对齐方式。

// ...

    for ( int i = 0 ; i < 7 ; i++ ){
    
        names[i] = gtk_label_new(std::string("Name" + std::to_string(i)).c_str());
        states[i] = gtk_label_new("Waiting Mode");
        bars[i] = gtk_progress_bar_new();

        // Align vertically in the table:
        gtk_widget_set_valign(bars[i], GTK_ALIGN_CENTER);

        gtk_table_attach_defaults(GTK_TABLE(table), names[i]  , 0, 1, i, i+1);     
        gtk_table_attach_defaults(GTK_TABLE(table), bars[i]   , 1, 2, i, i+1);      
        gtk_table_attach_defaults(GTK_TABLE(table), states[i] , 2, 3, i, i+1);        
    }
    
    // ...

使用Gtkmm 3.24,可以得到:

由于您似乎已经在使用C++,我建议您检查Gtkmm,它是Gtk+的C++ Package 器。

70gysomp

70gysomp2#

您可能需要GtkAlign。一个C示例:

#include <gtk/gtk.h>

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *win = gtk_application_window_new (app);
  gtk_window_fullscreen (GTK_WINDOW (win));
  g_autofree gchar *label_text = NULL;
  GtkWidget *names[7], *states[7], *bars[7];
  GtkWidget *grid = gtk_grid_new ();
  for (gint32 i = 0; i < 7; i++) {
    label_text = g_strdup_printf ("Name %d", i);
    names[i] = gtk_label_new (label_text);
    states[i] = gtk_label_new ("Waiting Mode");
    bars[i] = gtk_progress_bar_new();
    gtk_widget_set_valign(bars[i], GTK_ALIGN_CENTER);
    gtk_grid_attach (GTK_GRID (grid), names[i] , 0, i, 1, 1);
    gtk_grid_attach (GTK_GRID (grid), bars[i] , 1, i, 1, 1);
    gtk_grid_attach (GTK_GRID (grid), states[i] , 2, i, 1, 1);
  }

  gtk_window_set_child (GTK_WINDOW (win), grid);
  gtk_widget_show (win);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

关于GtkBoxGtkGrid对齐的良好文档仍然是GTK 3的文档,但它也适用于GTK 4:是的。

相关问题