异步操作

在默认情况下,PrintOperation::run()在打印操作完成时返回。如果你需要运行非阻塞的打印操作,请调用PrintOperation::set_allow_async()。请注意,并非所有平台都支持set_allow_async(),但done信号仍然会被发出。

run()可能会返回PrintOperation::Result::IN_PROGRESS。要跟踪状态并处理结果或错误,你需要实现donestatus_changed信号的信号处理函数。

例如:

// in class ExampleWindow's method...
auto op = PrintOperation::create();
// ...set up op...
op->signal_done().connect(sigc::bind(sigc::mem_fun(
  *this, &ExampleWindow::on_printoperation_done), op));
// run the op

然后,检查错误并连接到status_changed信号,例如:

void ExampleWindow::on_printoperation_done(Gtk::PrintOperationResult result,
  const Glib::RefPtr<PrintOperation>& op)
{
  if (result == Gtk::PrintOperation::Result::ERROR)
    //notify user
  else if (result == Gtk::PrintOperation::Result::APPLY)
    //Update PrintSettings with the ones used in this PrintOperation

  if (! op->is_finished())
    op->signal_status_changed().connect(sigc::bind(sigc::mem_fun(
      *this, &ExampleWindow::on_printoperation_status_changed), op));
}

最后检查状态。例如:

void ExampleWindow::on_printoperation_status_changed(const Glib::RefPtr<PrintOperation>& op)
{
  if (op->is_finished())
    //the print job is finished
  else
    //get the status with get_status() or get_status_string()

  //update UI
}