Gtk进度条高度

问题描述 投票:0回答:2

我正在用 C++ 开发一个 Gtk 应用程序。 (GTK 3.0)。我的问题是我无法更改 ProgressBar 高度! (https://developer.gnome.org/gtk3/stable/GtkProgressBar.html)

下图将显示我所需要的。一般来说,我希望能够在我的代码中设置进度条的高度。progress bar height

我使用以下命令来编译我的示例:

gcc progress.cpp `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0

这是我的代码示例:

#include <gtk/gtk.h>

static gboolean
fill (gpointer   user_data)
{
  GtkWidget *progress_bar = user_data;

  /*Get the current progress*/
  gdouble fraction;
  fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progress_bar));

  /*Increase the bar by 10% each time this function is called*/
  fraction += 0.1;

  /*Fill in the bar with the new fraction*/
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);

  /*Ensures that the fraction stays below 1.0*/
  if (fraction < 1.0) 
    return TRUE;

  return FALSE;
}


static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *progress_bar;

  gdouble fraction = 0.0;

  /*Create a window with a title, and a default size*/
  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "ProgressBar Example");
  gtk_window_set_default_size (GTK_WINDOW (window), 220, 20);

  /*Create a progressbar and add it to the window*/
  progress_bar = gtk_progress_bar_new ();
  gtk_container_add (GTK_CONTAINER (window), progress_bar);

  /*Fill in the given fraction of the bar. Has to be between 0.0-1.0 inclusive*/
  gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);

  /*Use the created fill function every 500 milliseconds*/
  g_timeout_add (500, fill, GTK_PROGRESS_BAR (progress_bar));

  gtk_widget_show_all (window);
}


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

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

  return status;
}
c++ gtk gtkmm
2个回答
6
投票

对于 GTK 3.20 及更高版本,定义 CSS 时,似乎必须使用

progressbar
progress
类,而不是使用
trough
类。这对我有用:

progress, trough {
  min-height: 30px;
}

0
投票

正如文档所说,进度条有3个子节点:

> progressbar[.osd]
├── [text]
╰── trough[.empty][.full]
    ╰── progress[.pulse]

这样你就可以用这种风格编写CSS:

progressbar {
    min-height: 100px;
    margin-top: 10px;
}

progressbar > trough {
    min-height: 80px;
    background-color: white;
}

progressbar > trough > progress {
    min-height: 80px;
    background-color: greenyellow;
}

progressbar > text {
    font-style: italic;
    font-size: xx-large;
    color: cornflowerblue;
}

如果你通过这个函数应用CSS

gtk_style_context_add_provider();

css中定义的进度条子节点样式(槽、文本、进度)可能不起作用,我改用这个函数:

gtk_style_context_add_provider_for_display();   
© www.soinside.com 2019 - 2024. All rights reserved.