283.md 9.4 KB
Newer Older
W
wizardforcel 已提交
1 2
{% raw %}

W
wizardforcel 已提交
3 4 5 6
# Qyoto 对话框

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

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

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

W
wizardforcel 已提交
11
## `MessageDialog`
W
wizardforcel 已提交
12

W
wizardforcel 已提交
13
消息框是方便的对话框,可向应用的用户提供消息。 该消息由文本和图像数据组成。
W
wizardforcel 已提交
14

W
wizardforcel 已提交
15
```cs
W
wizardforcel 已提交
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
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`管理器来设置五个按钮的网格。 每个按钮显示一个不同的消息框。

W
wizardforcel 已提交
104
```cs
W
wizardforcel 已提交
105 106 107 108 109 110
QPushButton button = (QPushButton) Sender();

```

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

W
wizardforcel 已提交
111
```cs
W
wizardforcel 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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)

W
wizardforcel 已提交
131
## `QInputDialog`
W
wizardforcel 已提交
132 133 134

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

W
wizardforcel 已提交
135
```cs
W
wizardforcel 已提交
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
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();
    }
}

```

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

W
wizardforcel 已提交
203
```cs
W
wizardforcel 已提交
204 205 206 207 208
String text = QInputDialog.GetText(
        this, "Input Dialog", "Enter your name");

```

W
wizardforcel 已提交
209
`GetText()`静态方法创建输入对话框。 对话框中的文本存储在`text`变量中。
W
wizardforcel 已提交
210

W
wizardforcel 已提交
211
```cs
W
wizardforcel 已提交
212 213 214 215 216 217 218
if (text!=null && text.Trim() != String.Empty) 
{
    edit.Text = text;
}

```

W
wizardforcel 已提交
219
在更新行编辑之前,请确保`text`变量不为`null`且不为空,并且不仅由空格组成。
W
wizardforcel 已提交
220 221 222

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

W
wizardforcel 已提交
223
图:输入对话框
W
wizardforcel 已提交
224

W
wizardforcel 已提交
225
## `QColorDialog`
W
wizardforcel 已提交
226 227 228

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

W
wizardforcel 已提交
229
```cs
W
wizardforcel 已提交
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
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();
    }
}

```

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

W
wizardforcel 已提交
293
```cs
W
wizardforcel 已提交
294 295 296 297 298 299 300 301 302
protected override void OnMousePressEvent(QMouseEvent arg1) 
{
    ...
}

```

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

W
wizardforcel 已提交
303
```cs
W
wizardforcel 已提交
304 305 306 307 308 309
QColor col = QColorDialog.GetColor();

```

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

W
wizardforcel 已提交
310
```cs
W
wizardforcel 已提交
311 312 313 314 315 316
if (!color.IsValid()) return;

```

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

W
wizardforcel 已提交
317
```cs
W
wizardforcel 已提交
318 319 320 321 322 323 324 325 326
String style = String.Format("QWidget {{ color: {0} }}", color.Name());
label.SetStyleSheet(style);

```

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

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

W
wizardforcel 已提交
327
图:`QColorDialog`
W
wizardforcel 已提交
328

W
wizardforcel 已提交
329
## `QFontDialog`
W
wizardforcel 已提交
330 331 332

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

W
wizardforcel 已提交
333
```cs
W
wizardforcel 已提交
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
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();
    }
}

```

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

W
wizardforcel 已提交
395
```cs
W
wizardforcel 已提交
396 397 398 399
QFont font = QFontDialog.GetFont(ref ok);

```

W
wizardforcel 已提交
400
正在创建`QFontDialog`。 当我们按下对话框的 OK 按钮时,将设置`boolean ok`变量。
W
wizardforcel 已提交
401

W
wizardforcel 已提交
402
```cs
W
wizardforcel 已提交
403 404 405 406 407 408
if (!ok) return;

```

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

W
wizardforcel 已提交
409
```cs
W
wizardforcel 已提交
410 411 412 413 414 415 416 417
label.Font = font;

```

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

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

W
wizardforcel 已提交
418
图:`QFontDialog`
W
wizardforcel 已提交
419

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

{% endraw %}