首页 > 解决方案 > gtk 图像突然不刷新没有任何错误或警告

问题描述

我开发了一个工具来从相机捕获图像并在窗口中显示捕获的图像。GUI 窗口基于 GTK 2.4。开始时,该工具运行正常,并且从相机捕获图像并实时显示在窗口上。过了一会儿,窗口上的图像突然不再刷新,但仍然是从相机中捕获的。没有错误或警告。有人遇到过这样的情况吗?谢谢你。

Ubuntu 18.04,GTK 2.4

  // loop to call the following code to refresh the image on the window
  pixbuf_ = Gdk::Pixbuf::create_from_data(
      final_img_buf_.data, Gdk::COLORSPACE_RGB, false, 8, final_img_buf_.cols,
      final_img_buf_.rows, static_cast<int>(final_img_buf_.step));
  gtk_image_.set(pixbuf_);

编辑于 2019-02-27 感谢您的所有回复。我已将 GTK 升级到 GTK+ 3,但这仍然出现。

  // loop to call the following code to refresh the image on the window
  pixbuf_ = Gdk::Pixbuf::create_from_data(
      final_img_buf_.data, Gdk::COLORSPACE_RGB, false, 8, final_img_buf_.cols,
      final_img_buf_.rows, static_cast<int>(final_img_buf_.step));
  // ensure the image is normally updating
  //std::this_thread::sleep_for (std::chrono::milliseconds(30));
  gtk_image_.set(pixbuf_);
  Glib::RefPtr<Gdk::Pixbuf> pixbuf = gtk_image_.get_pixbuf();
  std::string filename = std::string("./debug/") + std::to_string(CurTimestampMicrosecond()) + std::string(".jpg");
  pixbuf->save(filename, "jpeg");

运行一段时间后,窗口不再刷新图像,但图像仍然正确保存。

编辑于 2019-02-28

// The initialization code
gtk_main_.reset(new Gtk::Main(argc, argv));
ctrl_window_.reset(new CtrlWindow(screen, ctrl_rect)); // inherited from Gtk::Window
thread_ = std::thread([this]() {
  Gtk::Main::run(*ctrl_window_);
}

标签: c++ubuntugtkgtkmm

解决方案


由于处理其他事件源,看起来两者都g_timeout_add可能会延迟,因此在需要精确计时的上下文中不是最佳选择,例如在帧更新之前将图像绘制到屏幕上。g_idle_add我注意到g_timeout_add在 2D 图形上下文中使用会完全冻结应用程序,如果它无法以指定的 fps 加载帧。

gtk_widget_add_callback()可能更适合您的需求,因为它将尽可能快地绘制缓冲区,而不会挂起,基本上为您进行混乱的同步。


推荐阅读