GTK图像突然不会刷新任何错误或警告

gtk image suddenly do not refresh without any errors or warnings

本文关键字:任何 错误 警告 刷新 图像 突然 GTK      更新时间:2023-10-16

我开发了一种工具来从相机捕获图像,并在窗口中显示捕获的图像。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");

afther运行一段时间,窗口不再刷新图像,但是该图像仍然可以纠正。

编辑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_);
}

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

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