<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國際加速解決方案。 廣告
                # Qyoto 中的菜單和工具欄 > 原文: [http://zetcode.com/gui/csharpqyoto/menustoolbars/](http://zetcode.com/gui/csharpqyoto/menustoolbars/) 在 Qyoto C# 編程教程的這一部分中,我們將使用菜單和工具欄。 菜單欄是 GUI 應用中最可見的部分之一。 它是位于各個菜單中的一組命令。 菜單將我們可以在應用中使用的命令分組。 使用工具欄可以快速訪問最常用的命令。 ## 簡單菜單 第一個示例將顯示一個簡單的菜單。 ```cs using System; using QtCore; using QtGui; /** * ZetCode Qyoto C# tutorial * * This program shows a simple * menu. It has one action, which * will terminate the program, when * selected. * * @author Jan Bodnar * website zetcode.com * last modified October 2012 */ public class QyotoApp : QMainWindow { public QyotoApp() { WindowTitle = "Simple menu"; InitUI(); Resize(300, 200); Move(300, 300); Show(); } private void InitUI() { QAction quit = new QAction("&Quit", this); QMenu file = MenuBar.AddMenu("&File"); file.AddAction(quit); Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")); } [STAThread] public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } } ``` 我們有一個菜單欄,一個菜單和一個動作。 為了使用菜單,我們必須繼承`QMainWindow`小部件。 ```cs QAction quit = new QAction("&Quit", this); ``` 此代碼行創建一個`QAction`。 每個`QMenu`具有一個或多個動作對象。 注意 AND 字符(`&`)。 它為以下項目創建快捷方式: `Alt + Q` 。 它還強調了`Q`字符。 下拉文件菜單時,該快捷方式處于活動狀態。 ```cs QMenu file = MenuBar.AddMenu("&File"); file.AddAction(quit); ``` 我們創建一個`QMenu`對象。 &字符創建快捷方式: `Alt + F` 。 連續的快捷方式 `Alt + F` , `Alt + Q` 終止應用。 ```cs Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")); ``` 當我們從菜單中選擇此選項時,應用終止。 ![Simple menu](https://img.kancloud.cn/65/2d/652dbfcce06136d3d2b269afff905dfa_302x226.jpg) 圖:簡單菜單 ## 創建一個子菜單 子菜單是插入另一個菜單對象的菜單。 下一個示例對此進行了演示。 ```cs using System; using QtCore; using QtGui; /** * ZetCode Qyoto C# tutorial * * This program creates a * submenu. * * @author Jan Bodnar * website zetcode.com * last modified October 2012 */ public class QyotoApp : QMainWindow { public QyotoApp() { WindowTitle = "Submenu"; InitUI(); Resize(300, 200); Move(300, 300); Show(); } private void InitUI() { QAction quit = new QAction("&Quit", this); QMenu file = MenuBar.AddMenu("&File"); QMenu impm = new QMenu("Import"); QAction seeds = new QAction("Import news feed...", this); QAction marks = new QAction("Import bookmarks...", this); QAction mail = new QAction("Import mail...", this); impm.AddAction(seeds); impm.AddAction(marks); impm.AddAction(mail); file.AddMenu(impm); file.AddAction(quit); Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")); } [STAThread] public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } } ``` 在示例中,文件菜單的子菜單中有三個選項。 ```cs QMenu file = MenuBar.AddMenu("&File"); QMenu impm = new QMenu("Import"); ``` 我們有兩個`QMenu`對象。 文件菜單和導入菜單。 ```cs QAction seeds = new QAction("Import news feed...", this); QAction marks = new QAction("Import bookmarks...", this); QAction mail = new QAction("Import mail...", this); ``` 我們創建三個動作對象。 ```cs impm.AddAction(seeds); impm.AddAction(marks); impm.AddAction(mail); ``` 我們將動作對象添加到導入菜單中。 ```cs file.AddMenu(impm); ``` 最后,我們將導入菜單添加到文件菜單中。 ![Submenu](https://img.kancloud.cn/8e/9a/8e9af2696f2b6324fbc4ac7cf8c590ae_302x226.jpg) 圖:子菜單 ## 圖像,菜單,分隔符 在以下示例中,我們將進一步增強以前的應用。 我們將在菜單中添加圖標,使用快捷方式和分隔符。 ```cs using System; using QtCore; using QtGui; /** * ZetCode Qyoto C# tutorial * * This program shows image menu items, a shorcut * and a separator. * * @author Jan Bodnar * website zetcode.com * last modified October 2012 */ public class QyotoApp : QMainWindow { public QyotoApp() { WindowTitle = "Image menu"; InitUI(); Resize(300, 200); Move(300, 300); Show(); } private void InitUI() { QIcon newpix = new QIcon("new.png"); QIcon openpix = new QIcon("open.png"); QIcon quitpix = new QIcon("quit.png"); QAction newa = new QAction(newpix, "&New", this); QAction open = new QAction(openpix, "&Open", this); QAction quit = new QAction(quitpix, "&Quit", this); quit.Shortcut = "CTRL+Q"; QMenu file; file = MenuBar.AddMenu("&File"); file.AddAction(newa); file.AddAction(open); file.AddSeparator(); file.AddAction(quit); Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")); } [STAThread] public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } } ``` 在我們的示例中,我們有一個包含三個動作的菜單。 如果我們選擇退出操作,則實際上只有退出操作才可以執行某些操作。 我們還創建了一個分隔符和`Ctrl + Q`快捷鍵,它們將終止應用。 ```cs QIcon newpix = new QIcon("new.png"); QIcon openpix = new QIcon("open.png"); QIcon quitpix = new QIcon("quit.png"); ``` 這些是我們將在應用中使用的 PNG 圖像。 ```cs QAction newa = new QAction(newpix, "&New", this); QAction open = new QAction(openpix, "&Open", this); QAction quit = new QAction(quitpix, "&Quit", this); ``` 在這里,我們創建三個動作對象。 第一個參數是`QIcon`。 ```cs quit.Shortcut = "CTRL+Q"; ``` 這行創建一個快捷方式。 通過按下此快捷方式,我們將運行退出操作,這將退出應用。 ```cs file.AddSeparator(); ``` 我們創建一個分隔符。 分隔符是一條水平線,它使我們能夠將菜單操作分組為一些邏輯部分。 ![Images, shortcut and a separator](https://img.kancloud.cn/71/1a/711a3ba20fcb4deebff97a3d24d07fc2_256x225.jpg) 圖:圖像 s, shortcut and a separator ## 工具欄 `QToolBar`類提供了一個可移動面板,其中包含一組控件,這些控件提供對應用操作的快速訪問。 ```cs using System; using QtCore; using QtGui; /** * ZetCode Qyoto C# tutorial * * This program creates a * toolbar. * * @author Jan Bodnar * website zetcode.com * last modified October 2012 */ public class QyotoApp : QMainWindow { public QyotoApp() { WindowTitle = "Toolbar"; InitUI(); Resize(300, 200); Move(300, 300); Show(); } private void InitUI() { QIcon newpi = new QIcon("new.png"); QIcon openpi = new QIcon("open.png"); QIcon quitpi = new QIcon("quit.png"); QToolBar toolbar = AddToolBar("main toolbar"); toolbar.AddAction(newpi, "New File"); toolbar.AddAction(openpi, "Open File"); toolbar.AddSeparator(); QAction quit = toolbar.AddAction(quitpi, "Quit Application"); Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")); } [STAThread] public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } } ``` 我們創建一個帶有三個動作對象和一個分隔符的工具欄。 ```cs QIcon newpi = new QIcon("new.png"); QIcon openpi = new QIcon("open.png"); QIcon quitpi = new QIcon("quit.png"); ``` 工具欄動作對象將顯示這些圖標。 ```cs QToolBar toolbar = AddToolBar("main toolbar"); ``` `QMainWindow`類的`AddToolBar()`方法為應用創建一個工具欄。 文本字符串為工具欄命名。 此名稱用于引用此工具欄,因為一個應用中可以有多個工具欄。 如果右鍵單擊窗口區域,我們將看到一個可檢查的選項,該選項顯示/隱藏工具欄。 ```cs toolbar.AddSeparator(); ``` 我們創建一個垂直分隔符。 ```cs Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")); ``` 當我們單擊退出操作對象時,應用終止。 ![Toolbar](https://img.kancloud.cn/fc/2d/fc2df2023f844dc7396d9117e4d27040_256x225.jpg) 圖:工具欄 ## 撤銷重做 以下示例演示了如何停用工具欄上的工具欄按鈕。 這是 GUI 編程中的常見做法。 例如,保存按鈕。 如果我們將文檔的所有更改都保存到磁盤上,則在大多數文本編輯器中,“保存”按鈕將被停用。 這樣,應用會向用戶指示所有更改都已保存。 ```cs using System; using QtCore; using QtGui; /** * ZetCode Qyoto C# tutorial * * This program disables/enables * toolbuttons on a toolbar. * * @author Jan Bodnar * website zetcode.com * last modified October 2012 */ public class QyotoApp : QMainWindow { int count = 0; QToolButton undoButton; QToolButton redoButton; public QyotoApp() { WindowTitle = "Undo redo"; InitUI(); Resize(300, 200); Move(300, 300); Show(); } private void InitUI() { QIcon undoi = new QIcon("undo.png"); QIcon redoi = new QIcon("redo.png"); QIcon quitpi = new QIcon("quit.png"); QToolBar toolbar = new QToolBar(); undoButton = new QToolButton(); redoButton = new QToolButton(); QAction undoAction = new QAction(undoi, "Undo", undoButton); QAction redoAction = new QAction(redoi, "Redo", redoButton); undoButton.DefaultAction = undoAction; redoButton.DefaultAction = redoAction; toolbar.AddWidget(undoButton); toolbar.AddWidget(redoButton); toolbar.AddSeparator(); QAction quit = toolbar.AddAction(quitpi, "Quit Application"); undoButton.Triggered += Count; redoButton.Triggered += Count; Connect(quit, SIGNAL("triggered()"), qApp, SLOT("quit()")); AddToolBar(toolbar); } [Q_SLOT] void Count(QAction action) { if ("Undo".Equals(action.Text)) { count += -1; } else { count += 1; } if (count <= 0) { undoButton.SetDisabled(true); redoButton.SetDisabled(false); } if (count >= 5) { undoButton.SetDisabled(false); redoButton.SetDisabled(true); } } [STAThread] public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } } ``` 在我們的示例中,我們有三個`QAction`對象和一個分隔符。 在撤消或重做按鈕上單擊幾下后,它們將被停用。 外觀上,按鈕顯示為灰色。 ```cs int count = 0; ``` 計數變量確定哪個按鈕被激活和停用。 ```cs undoButton.Triggered += Count; redoButton.Triggered += Count; ``` 單擊工具欄按鈕,將發射`Triggered`信號。 我們將此信號連接到`Count()`方法。 它接收觸發它的`QAction`對象。 ```cs if ("Undo".Equals(action.Text)) { count += -1; } else { count += 1; } ``` 撤消工具欄按鈕從計數變量中減去 1。 重做添加 1.根據計數變量的值,我們啟用/禁用工具欄按鈕。 ```cs if (count <= 0) { undoButton.SetDisabled(true); redoButton.SetDisabled(false); } ``` `SetDisabled()`方法激活或停用工具欄按鈕。 ![Undo redo](https://img.kancloud.cn/e4/a1/e4a1ee56d6db1dfe5c4ea942ef3b9fd1_302x226.jpg) 圖:撤銷和重做 在 Qyoto C# 教程的這一部分中,我們提到了菜單和工具欄。
                  <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>

                              哎呀哎呀视频在线观看