<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Ruby Qt 中的對話框 > 原文: [http://zetcode.com/gui/rubyqt/dialogs/](http://zetcode.com/gui/rubyqt/dialogs/) 在 Ruby Qt 編程教程的這一部分中,我們將使用對話框。 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 ## `MessageDialog` 消息框是方便的對話框,可向應用的用戶提供消息。 該消息由文本和圖像數據組成。 ```rb #!/usr/bin/ruby # ZetCode Ruby Qt tutorial # # This program demonstrates # MessageBox dialogs # # author: jan bodnar # website: www.zetcode.com # last modified: July 2009 require 'Qt' class QtApp < Qt::Widget slots 'showDialog()' def initialize super setWindowTitle "Message dialogs" init_ui resize 250, 150 move 300, 300 show end def init_ui grid = Qt::GridLayout.new self grid.setSpacing 2 error = Qt::PushButton.new "Error", self warning = Qt::PushButton.new "Warning", self question = Qt::PushButton.new "Question", self information = Qt::PushButton.new "Information", self about = Qt::PushButton.new "About", self grid.addWidget error, 0, 0 grid.addWidget warning, 0, 1 grid.addWidget question, 1, 0 grid.addWidget information, 1, 1 grid.addWidget about, 2, 0 connect(error, SIGNAL("clicked()"), self, SLOT("showDialog()")) connect(warning, SIGNAL("clicked()"), self, SLOT("showDialog()")) connect(question, SIGNAL("clicked()"), self, SLOT("showDialog()")) connect(information, SIGNAL("clicked()"), self, SLOT("showDialog()")) connect(about, SIGNAL("clicked()"), self, SLOT("showDialog()")) end def showDialog button = sender if "Error" == button.text Qt::MessageBox.critical self, "Error", "Error loading file!" elsif "Warning" == button.text Qt::MessageBox.warning self, "Warning", "Operation not permitted!" elsif "Question" == button.text Qt::MessageBox.question self, "Question", "Are you sure to quit?" elsif "Information" == button.text Qt::MessageBox.information self, "Information", "Download completed." elsif "About" == button.text Qt::MessageBox.about self, "About", "ZetCode Ruby Qt tutorial." end end end app = Qt::Application.new ARGV QtApp.new app.exec ``` 我們使用`GridLayout`管理器來設置五個按鈕的網格。 每個按鈕顯示一個不同的消息框。 ```rb if "Error" == button.text Qt::MessageBox.critical self, "Error", "Error loading file!" ``` 如果按下錯誤按鈕,則會顯示錯誤對話框。 我們使用`MessageBox`類的靜態方法來顯示消息框。 ![Information message dialog](https://img.kancloud.cn/d4/ae/d4ae11818b9f2af162efe0a4cc74ea87_208x117.jpg) 圖:信息對話框 ## `Qt::InputDialog` `Qt::InputDialog`類提供了一個簡單的便捷對話框,可從用戶那里獲取單個值。 輸入值可以是字符串,數字或列表中的項目。 必須設置標簽以告知用戶他們應該輸入什么。 ```rb #!/usr/bin/ruby # ZetCode Ruby Qt tutorial # # This program shows an input # dialog # # author: jan bodnar # website: www.zetcode.com # last modified: July 2009 require 'Qt' class QtApp < Qt::Widget slots 'showDialog()' def initialize super setWindowTitle "Input dialog" init_ui resize 400, 80 move 300, 300 show end def init_ui show = Qt::PushButton.new "Dialog", self connect(show, SIGNAL("clicked()"), self, SLOT("showDialog()")) show.move 20, 20 @edit = Qt::LineEdit.new self @edit.move 130, 22 end def showDialog text = Qt::InputDialog.getText self, "Input Dialog", "Enter your name" if text != nil name = text.strip if not name.empty? @edit.setText name end end end end app = Qt::Application.new ARGV QtApp.new app.exec ``` 在代碼示例中,我們有一個按鈕和一行編輯。 該按鈕顯示一個輸入對話框。 我們得到一些文本,文本顯示在行編輯小部件中。 ```rb text = Qt::InputDialog.getText self, "Input Dialog", "Enter your name" ``` `getText`靜態方法創建輸入對話框。 對話框中的文本存儲在`text`變量中。 ```rb if text != nil name = text.strip if not name.empty? @edit.setText name end end ``` 在更新行編輯之前,請確保`text`變量不為`null`且不為空,并且不僅由空格組成。 ![Input dialog](https://img.kancloud.cn/e2/ad/e2adf580f28f8532fe72af6305cc3d67_206x135.jpg) 圖:輸入對話框 ## `Qt::ColorDialog` `Qt::ColorDialog`類提供一個用于指定顏色的對話框小部件。 顏色對話框的功能是允許用戶選擇顏色。 ```rb #!/usr/bin/ruby # ZetCode Ruby Qt tutorial # # In this program, we use the # ColorDialog to change the color # of a label text # # author: jan bodnar # website: www.zetcode.com # last modified: July 2009 require 'Qt' class QtApp < Qt::Widget slots 'showDialog()' def initialize super setWindowTitle "Color dialog" init_ui resize 400, 300 move 300, 300 show end def init_ui @label = Qt::Label.new "ZetCode Ruby Qt tutorial", self vbox = Qt::VBoxLayout.new self @label.setAlignment Qt::AlignCenter vbox.addWidget @label end def mousePressEvent event color = Qt::ColorDialog.getColor if not color.isValid return end @label.setStyleSheet "QWidget { color: %s }" % color.name end end app = Qt::Application.new ARGV QtApp.new app.exec ``` 我們在窗口中心顯示一些文本。 通過單擊窗口區域,我們顯示一個顏色對話框。 我們將文本前景色更改為從對話框中選擇的顏色。 ```rb def mousePressEvent event ... end ``` 為了接收我們窗口的鼠標按下事件,我們必須重新實現`mousePressEvent`方法。 ```rb color = Qt::ColorDialog.getColor ``` 正在創建`ColorDialog`。 所選顏色存儲在`color`變量中。 ```rb @label.setStyleSheet "QWidget { color: %s }" % color.name ``` 在這里,我們更新標簽文本的前景色。 ![ColorDialog](https://img.kancloud.cn/ca/7b/ca7bc14ae45e67d8358252d27471dafa_350x270.jpg) 圖:`Qt::ColorDialog` ## `Qt::FontDialog` `Qt::FontDialog`類提供用于選擇字體的對話框小部件。 ```rb #!/usr/bin/ruby # ZetCode Ruby Qt tutorial # # In this program, we use the # FontDialog to change the font # of a label text # # author: jan bodnar # website: www.zetcode.com # last modified: July 2009 require 'Qt' class QtApp < Qt::Widget slots 'showDialog()' def initialize super setWindowTitle "Font dialog" init_ui resize 400, 300 move 300, 300 show end def init_ui @label = Qt::Label.new "ZetCode Ruby Qt tutorial", self vbox = Qt::VBoxLayout.new self @label.setAlignment Qt::AlignCenter vbox.addWidget @label end def mousePressEvent event ok = Qt::Boolean.new font = Qt::FontDialog.getFont ok if not ok return end @label.setFont font end end app = Qt::Application.new ARGV QtApp.new app.exec ``` 此示例與上一個示例相似。 這次,我們更改文本的字體。 ```rb font = Qt::FontDialog.getFont ok ``` 正在創建`Qt::FontDialog`。 ```rb if not ok return end ``` 如果單擊對話框的“確定”按鈕,則布爾值`ok`變量為`true`。 如果按下了取消按鈕,我們將從方法中返回。 ```rb @label.setFont font ``` 我們將標簽更新為新選擇的字體。 ![FontDialog](https://img.kancloud.cn/40/3e/403ef54cd8fe7d45bf6baaea8d6ecc72_350x266.jpg) 圖:`Qt::FontDialog` 在 Ruby Qt 教程的這一部分中,我們使用了對話框窗口。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看