【问题标题】:FLTK Closing windowFLTK 关闭窗口
【发布时间】:2011-12-29 05:05:09
【问题描述】:

我正在使用 FLTK。我有一个带有各种按钮的窗口,用户可以单击以执行某些操作。在我的 int main() 中,我有一个 switch 语句来处理所有这些。当用户单击退出时,switch 语句的设置如下:

case Exit_program:
    cout << "save files and exit\n";
    do_save_exit(sw);

这转到 do_save_exit 函数,该函数创建一个退出确认窗口,其中包含两个按钮是(退出)和否(不退出)。我让“是”按钮工作,退出程序,但“否”按钮意味着我应该隐藏确认窗口。这是以下功能:

void yes(Address addr, Address)
{
    exit(0);
}
void no(Address addr, Address)
{

}
void do_save_exit(Window& w)
{
    Window quit(Point(w.x()+100, w.y()+100), 250, 55, "Exit confirmation");
    Text conf(Point(15,15),"Do you really want to save and exit?");
    Button yes(Point(60, 20),35,30,"Yes",yes);
    Button no(Point(140, 20),35,30,"No",no);
    quit.attach(conf);
    quit.attach(yes);
    quit.attach(no);
    wait_for_main_window_click();
}

问题是,当我单击否按钮时,它会变为无效,但我无法从那里去任何地方。我只想做 quit.hide() 但 no 函数看不到退出窗口(超出范围)。我应该如何进行?谢谢你

P.S:我曾考虑过使用指向退出窗口的指针,然后使用指针在 no 函数中退出窗口,但不知道该怎么做。

【问题讨论】:

    标签: c++ user-interface window fltk


    【解决方案1】:

    无需使用 hide()。 您可以简单地在回调中使用exit(0);

    【讨论】:

      【解决方案2】:

      当试图关闭窗口时调用 Fl_Window 回调。默认回调隐藏窗口(如果所有窗口都隐藏,您的应用程序将结束)。如果您设置自己的窗口回调,则可以覆盖此行为,以免隐藏窗口:

      // This window callback allows the user to save & exit, don't save, or cancel.
      static void window_cb (Fl_Widget *widget, void *) 
      {
          Fl_Window *window = (Fl_Window *)widget;
      
          // fl_choice presents a modal dialog window with up to three choices.
          int result = fl_choice("Do you want to save before quitting?", 
                                 "Don't Save",  // 0
                                 "Save",        // 1
                                 "Cancel"       // 2
                                 );
          if (result == 0) {  // Close without saving
              window->hide();
          } else if (result == 1) {  // Save and close
              save();
              window->hide();
          } else if (result == 2) {  // Cancel / don't close
              // don't do anything
          }
      }
      

      在您设置 Fl_Window 的任何位置设置窗口的回调,例如在你的 ma​​in 函数中:

      window->callback( win_cb );
      

      【讨论】:

        【解决方案3】:

        构建时没有收到错误或警告?问题可能是您有两个全局函数名称 yesno 以及调用相同的局部变量。重命名变量的函数。

        【讨论】:

          【解决方案4】:

          您可能需要考虑使用模式(即对话框)窗口。看&lt;FL/fl_ask.h&gt;

          if (fl_ask("Do you really want to save and exit?"))
              save_and_exit();
          

          header还具有popup的字体、标题等功能。

          【讨论】:

          • 太棒了。非常感谢。正在扯掉我的头发来解决这个问题。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-15
          • 1970-01-01
          • 2019-05-28
          相关资源
          最近更新 更多