283.md 9.4 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4
# Qyoto 对话框

> 原文: [http://zetcode.com/gui/csharpqyoto/dialogs/](http://zetcode.com/gui/csharpqyoto/dialogs/)

W
wizardforcel 已提交
5
在 Qyoto C# 编程教程的这一部分中,我们将使用对话框。
W
wizardforcel 已提交
6 7 8

对话框窗口或对话框是大多数现代 GUI 应用程序必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用程序中,对话框是一个窗口,用于与应用程序“对话”。 对话框用于输入数据,修改数据,更改应用程序设置等。对话框是用户与计算机程序之间进行通信的重要手段。

W
wizardforcel 已提交
9
## `MessageDialog`
W
wizardforcel 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

消息框是方便的对话框,可向应用程序的用户提供消息。 该消息由文本和图像数据组成。

```
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * This program shows
 * QMessageBox dialogs
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */

public class QyotoApp : QWidget 
{
    public QyotoApp() 
    {
        WindowTitle = "Message boxes";

        InitUI();

        Resize(220, 90);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {        
        QGridLayout grid = new QGridLayout(this);
        grid.Spacing = 2;

        QPushButton error = new QPushButton("Error", this);
        QPushButton warni = new QPushButton("Warning", this);
        QPushButton quest = new QPushButton("Question", this);
        QPushButton infor = new QPushButton("Information", this);
        QPushButton about = new QPushButton("About", this);

        grid.AddWidget(error, 0, 0);
        grid.AddWidget(warni, 0, 1);
        grid.AddWidget(quest, 1, 0);
        grid.AddWidget(infor, 1, 1);
        grid.AddWidget(about, 2, 0);

        error.Clicked += ShowDialog;
        warni.Clicked += ShowDialog;
        quest.Clicked += ShowDialog;
        infor.Clicked += ShowDialog;
        about.Clicked += ShowDialog;                
    }

    [Q_SLOT]
    void ShowDialog() 
    {
        QPushButton btn = (QPushButton) Sender();

        if ("Error".Equals(btn.Text)) 
        {
            QMessageBox.Critical(this, "Error", "Error loading file!");
        } else if ("Warning".Equals(btn.Text)) 
        {
            QMessageBox.Warning(this, "Warning", "Operation not permitted!");
        } else if ("Question".Equals(btn.Text)) 
        {
            QMessageBox.Question(this, "Question", "Are you sure to quit?");
        } else if ("Information".Equals(btn.Text)) 
        {
            QMessageBox.Information(this, "Information", "Download completed.");
        } else if ("About".Equals(btn.Text)) 
        {
            QMessageBox.About(this, "About", "ZetCode Qyoto C# tutorial.");
        } 
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}

```

我们使用`GridLayout`管理器来设置五个按钮的网格。 每个按钮显示一个不同的消息框。

```
QPushButton button = (QPushButton) Sender();

```

在这里,我们确定哪个按钮称为`ShowDialog()`方法。

```
if ("Error".Equals(btn.Text)) 
{
    QMessageBox.Critical(this, "Error", "Error loading file!");
} 

```

如果按下错误按钮,则会显示错误对话框。 我们使用`QMessageBox`类的静态方法来显示消息框。

![Information message dialog](img/7e9491bb982c31a8a7b2c33c1d05f83a.jpg)

![Warning message dialog](img/ccb4b30650b9dccf0c883d302476257d.jpg)

![Question message dialog](img/cf195247005cfea116353efce3982e72.jpg)

![Error message dialog](img/db277080820e7311823765ff3a13d8aa.jpg)

![About message dialog](img/284be2326d27e911ef1bb51aae022f4a.jpg)

## QInputDialog

`QInputDialog`类提供了一个简单的便捷对话框,可从用户那里获取单个值。 输入值可以是字符串,数字或列表中的。 必须设置标签以告知用户他们应该输入什么。

```
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * This program shows an input
 * dialog.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */

public class QyotoApp : QWidget 
{
    QLineEdit edit;

    public QyotoApp() 
    {
        WindowTitle = "QInputDialog";

        InitUI();

        Resize(300, 150);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {        
        QPushButton show = new QPushButton("Dialog", this);
        show.Clicked += ShowDialog;

        show.FocusPolicy = Qt.FocusPolicy.NoFocus;
        show.Move(20, 20);

        edit = new QLineEdit(this);
        edit.Move(130, 22);
    }

    [Q_SLOT]
    void ShowDialog() 
    {        
        String text = QInputDialog.GetText(
                this, "Input Dialog", "Enter your name");

        if (text!=null && text.Trim() != String.Empty) 
        {
            edit.Text = text;
        }
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}

```

在代码示例中,我们有一个按钮和一行编辑。 该按钮显示一个输入对话框。 我们得到一些文本,文本显示在行编辑小部件中。

```
String text = QInputDialog.GetText(
        this, "Input Dialog", "Enter your name");

```

`GetText()`静态方法创建输入对话框。 对话框中的文本存储在 text 变量中。

```
if (text!=null && text.Trim() != String.Empty) 
{
    edit.Text = text;
}

```

在更新行编辑之前,请确保 text 变量不为 null 且不为空,并且不仅由空格组成。

![Input dialog](img/3c2d028a67f6f1e4054b0bb015ba37c1.jpg)

Figure: Input dialog

## QColorDialog

`QColorDialog`类提供用于指定颜色的对话框小部件。 颜色对话框的功能是允许用户选择颜色。

```
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, we use the
 * QColorDialog to change the colour
 * of a label text.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */

public class QyotoApp : QWidget 
{
    QLabel label;

    public QyotoApp() : base() 
    {
        WindowTitle = "QColorDialog";

        InitUI();

        Resize(250, 200);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {
        label = new QLabel("ZetCode Qyoto C# tutorial", this);

        QVBoxLayout vbox = new QVBoxLayout(this);
        label.Alignment = AlignmentFlag.AlignCenter;
        vbox.AddWidget(label);
    }    

    protected override void OnMousePressEvent(QMouseEvent arg1) 
    {
        QColor col = QColorDialog.GetColor();

        if (!col.IsValid()) return;

        String style = String.Format("QWidget {{ color: {0} }}", col.Name());
        label.StyleSheet = style;
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}

```

我们在窗口中心显示一些文本。 通过单击窗口区域,我们显示一个颜色对话框。 我们将文本前景色更改为从对话框中选择的颜色。

```
protected override void OnMousePressEvent(QMouseEvent arg1) 
{
    ...
}

```

为了接收我们窗口的鼠标按下事件,我们必须重写`MousePressEvent()`方法。

```
QColor col = QColorDialog.GetColor();

```

正在创建`QColorDialog`。 所选颜色存储在`col`变量中。

```
if (!color.IsValid()) return;

```

当按下取消按钮时,我们什么也不做。

```
String style = String.Format("QWidget {{ color: {0} }}", color.Name());
label.SetStyleSheet(style);

```

在这里,我们更新标签文本的前景色。

![QColorDialog](img/5b700ef91851420252629806f0c2cc71.jpg)

Figure: QColorDialog

## QFont 对话框

`QFontDialog`类提供用于选择字体的对话框小部件。

```
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, we use the
 * QFontDialog to change the font
 * of a label text.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */

public class QyotoApp : QWidget 
{    
    QLabel label;

    public QyotoApp() : base() 
    {
        WindowTitle = "QFontDialog";

        InitUI();

        Resize(250, 200);
        Move(300, 300);
        Show();
    }

    void InitUI() 
    {
        label = new QLabel("ZetCode Qyoto C# tutorial", this);

        QVBoxLayout vbox = new QVBoxLayout(this);
        label.Alignment = AlignmentFlag.AlignCenter;
        vbox.AddWidget(label);
    }

    protected override void OnMousePressEvent(QMouseEvent arg1) 
    {
        bool ok = true;
        QFont font = QFontDialog.GetFont(ref ok);
        if (!ok) return;
        label.Font = font;
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();
        return QApplication.Exec();
    }
}

```

此示例与上一个示例相似。 这次,我们更改文本的字体。

```
QFont font = QFontDialog.GetFont(ref ok);

```

正在创建`QFontDialog`。 当我们按下对话框的 OK 按钮时,将设置 boolean ok 变量。

```
if (!ok) return;

```

如果不按“确定”按钮,我们什么也不做。

```
label.Font = font;

```

`font`字段存储所选字体。 我们将标签的字体更新为新选择的字体。

![QFontDialog](img/12be835e6e49bad17e8a4438b28e7847.jpg)

Figure: QFontDialog

W
wizardforcel 已提交
418
在 Qyoto C# 教程的这一部分中,我们使用了对话框窗口。