c++ - 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_);
}
解决方案
由于处理其他事件源,看起来两者都g_timeout_add
可能会延迟,因此在需要精确计时的上下文中不是最佳选择,例如在帧更新之前将图像绘制到屏幕上。g_idle_add
我注意到g_timeout_add
在 2D 图形上下文中使用会完全冻结应用程序,如果它无法以指定的 fps 加载帧。
gtk_widget_add_callback()
可能更适合您的需求,因为它将尽可能快地绘制缓冲区,而不会挂起,基本上为您进行混乱的同步。
推荐阅读
- typescript - 如何防止我的 Vue 构建由于 Typescript 错误而失败
- php - PHP Move_uploaded_file 函数不起作用
- python - 正在发生缩进错误,我似乎看不到是什么导致了错误
- android - 如何在 React Native 应用程序上强制 RTL 布局?(不适用于第一次加载)
- html - 具有变换和显示属性的动画子菜单
- admob - 移动应用上的奖励广告数量有任何限制
- excel - 如何使用 talend 计算 Excel 文件中的总页数
- flutter - 在颤振中使用mysql搜索字段
- php - 我需要在 Intranet 站点上使用他们的 Windows 凭据对我的用户进行身份验证
- css - Jest 在打字稿中遇到了意外的标记