{% raw %}

Qyoto 中的小部件

原文: http://zetcode.com/gui/csharpqyoto/widgets/

在 Qyoto C# 编程教程的这一部分中,我们将介绍 Qyoto 小部件。

小部件是 GUI 应用的基本构建块。 多年来,几个小部件已成为所有 OS 平台上所有工具包中的标准。 例如,按钮,复选框或滚动条。 Qyoto 有一组丰富的小部件,可以满足大多数编程需求。 可以将更多专门的窗口小部件创建为自定义窗口小部件。

QCheckBox

QCheckBox是具有两种状态的窗口小部件:开和关。 接通状态通过复选标记显示。 它用来表示一些布尔属性。 QCheckBox小部件提供一个带有文本标签的复选框。

  1. using System;
  2. using QtCore;
  3. using QtGui;
  4. /**
  5. * ZetCode Qyoto C# tutorial
  6. *
  7. * This program uses QCheckBox
  8. * widget to show/hide the title
  9. * of the window.
  10. *
  11. * @author Jan Bodnar
  12. * website zetcode.com
  13. * last modified October 2012
  14. */
  15. public class QyotoApp : QWidget
  16. {
  17. public QyotoApp()
  18. {
  19. WindowTitle = "QCheckBox";
  20. SetupUI();
  21. Resize(250, 150);
  22. Move(300, 300);
  23. Show();
  24. }
  25. public void SetupUI()
  26. {
  27. QCheckBox cb = new QCheckBox("Show Title", this);
  28. cb.Checked = true;
  29. cb.Move(50, 50);
  30. cb.StateChanged += ShowTitle;
  31. }
  32. [Q_SLOT]
  33. public void ShowTitle(int state)
  34. {
  35. if (state == (int) CheckState.Checked)
  36. {
  37. WindowTitle = "QCheckBox";
  38. } else {
  39. WindowTitle = "";
  40. }
  41. }
  42. [STAThread]
  43. public static int Main(String[] args)
  44. {
  45. new QApplication(args);
  46. new QyotoApp();
  47. return QApplication.Exec();
  48. }
  49. }

在我们的示例中,我们在窗口上放置了一个复选框。 复选框显示/隐藏窗口的标题。

  1. WindowTitle = "QCheckBox";

在构建窗口期间,我们为窗口设置标题。

  1. QCheckBox cb = new QCheckBox("Show Title", this);

QCheckBox小部件已创建。 构造器的第一个参数是其文本标签。 第二个参数是父窗口小部件。

  1. cb.Checked = true;

标题在应用的开始处可见。 因此,也必须选中该复选框。 我们通过Checked属性选中该复选框。

  1. cb.StateChanged += ShowTitle;

我们将ShowTitle()方法插入StateChange事件。 复选框的状态更改时,将发出事件。

  1. [Q_SLOT]
  2. public void ShowTitle(int state)
  3. {
  4. ...
  5. }

方法定义之前带有Q_SLOT属性。 此属性通知编译器有关自定义槽的信息。

  1. if (state == (int) CheckState.Checked)
  2. {
  3. WindowTitle = "QCheckBox";
  4. } else {
  5. WindowTitle = "";
  6. }

根据复选框的状态,我们显示或隐藏窗口的标题。

281.md - 图1

图:QCheckBox

QLabel

QLabel小部件用于显示文本或图像。 没有用户交互。

  1. using System;
  2. using QtCore;
  3. using QtGui;
  4. /**
  5. * ZetCode Qyoto C# tutorial
  6. *
  7. * This program uses QLabel to
  8. * show lyrics of a song.
  9. *
  10. * @author Jan Bodnar
  11. * website zetcode.com
  12. * last modified October 2012
  13. */
  14. public class QyotoApp : QWidget
  15. {
  16. public QyotoApp()
  17. {
  18. WindowTitle = "You know I'm no Good";
  19. InitUI();
  20. Resize(250, 150);
  21. Move(300, 300);
  22. Show();
  23. }
  24. public void InitUI()
  25. {
  26. string text = @"Meet you downstairs in the bar and heard
  27. your rolled up sleeves and your skull t-shirt
  28. You say why did you do it with him today?
  29. and sniff me out like I was Tanqueray
  30. cause you're my fella, my guy
  31. hand me your stella and fly
  32. by the time I'm out the door
  33. you tear men down like Roger Moore
  34. I cheated myself
  35. like I knew I would
  36. I told ya, I was trouble
  37. you know that I'm no good";
  38. QLabel label = new QLabel(text, this);
  39. label.Font = new QFont("Purisa", 9);
  40. QVBoxLayout vbox = new QVBoxLayout();
  41. vbox.AddWidget(label);
  42. Layout = vbox;
  43. }
  44. [STAThread]
  45. public static int Main(String[] args)
  46. {
  47. new QApplication(args);
  48. new QyotoApp();
  49. return QApplication.Exec();
  50. }
  51. }

我们的示例在窗口中显示了歌曲的歌词。

  1. string text = @"Meet you downstairs in the bar and heard
  2. ...

我们定义了多行文字。 多行文本在 C# 语言中以@字符开头。

  1. QLabel label = new QLabel(text, this);
  2. label.Font = new QFont("Purisa", 9);

我们创建标签小部件并更改其字体。

  1. QVBoxLayout vbox = new QVBoxLayout();
  2. vbox.AddWidget(label);
  3. Layout = vbox;

代替手动编码标签的位置和大小,我们将标签放入盒子布局中。

281.md - 图2

图:QLabel

QLineEdit

QLineEdit是一个小部件,允许输入和编辑单行纯文本。 QLineEdit小部件具有撤消/重做,剪切/粘贴和拖放功能。

  1. using System;
  2. using QtCore;
  3. using QtGui;
  4. /**
  5. * ZetCode Qyoto C# tutorial
  6. *
  7. * This program shows text
  8. * which is entered in a QLineEdit
  9. * widget in a QLabel widget.
  10. *
  11. * @author Jan Bodnar
  12. * website zetcode.com
  13. * last modified October 2012
  14. */
  15. public class QyotoApp : QWidget
  16. {
  17. QLabel label;
  18. public QyotoApp()
  19. {
  20. WindowTitle = "QLineEdit";
  21. InitUI();
  22. Resize(250, 150);
  23. Move(400, 300);
  24. Show();
  25. }
  26. public void InitUI()
  27. {
  28. label = new QLabel(this);
  29. QLineEdit edit = new QLineEdit(this);
  30. edit.TextChanged += OnChanged;
  31. edit.Move(60, 100);
  32. label.Move(60, 40);
  33. }
  34. [Q_SLOT]
  35. public void OnChanged(string text)
  36. {
  37. label.Text = text;
  38. label.AdjustSize();
  39. }
  40. [STAThread]
  41. public static int Main(string[] args)
  42. {
  43. new QApplication(args);
  44. new QyotoApp();
  45. return QApplication.Exec();
  46. }
  47. }

在我们的示例中,我们显示了两个小部件。 行编辑和标签小部件。 输入到行编辑中的文本显示在标签窗口小部件中。

  1. QLineEdit edit = new QLineEdit(this);

QLineEdit小部件已创建。

  1. edit.TextChanged += OnChanged;

当我们在行编辑中键入或删除某些文本时,将触发OnChanged()方法。 该方法采用字符串参数。

  1. [Q_SLOT]
  2. public void OnChanged(string text)
  3. {
  4. label.Text = text;
  5. label.AdjustSize();
  6. }

OnChanged()方法中,我们将行编辑的内容设置为标签窗口小部件。 AdjustSize()方法确保所有文本都是可见的。

281.md - 图3

图:QLineEdit小部件

ToggleButton

切换按钮是设置了可检查标志的按钮。 切换按钮是具有两种状态的按钮。 已按下但未按下。 通过单击可以在这两种状态之间切换。 在某些情况下此功能非常合适。

  1. using System;
  2. using QtCore;
  3. using QtGui;
  4. /**
  5. * ZetCode Qyoto C# tutorial
  6. *
  7. * This program uses toggle buttons to
  8. * change the background colour of
  9. * a widget.
  10. *
  11. * @author Jan Bodnar
  12. * website zetcode.com
  13. * last modified October 2012
  14. */
  15. public class QyotoApp : QWidget
  16. {
  17. QWidget square;
  18. QColor col;
  19. QPushButton redb;
  20. QPushButton greenb;
  21. QPushButton blueb;
  22. public QyotoApp()
  23. {
  24. WindowTitle = "Toggle buttons";
  25. InitUI();
  26. Resize(350, 240);
  27. Move(400, 300);
  28. Show();
  29. }
  30. private void InitUI()
  31. {
  32. col = new QColor();
  33. redb = new QPushButton("Red", this);
  34. redb.Checkable = true;
  35. greenb = new QPushButton("Green", this);
  36. greenb.Checkable = true;
  37. blueb = new QPushButton("Blue", this);
  38. blueb.Checkable = true;
  39. redb.Toggled += OnToggled;
  40. greenb.Toggled += OnToggled;
  41. blueb.Toggled += OnToggled;
  42. square = new QWidget(this);
  43. square.StyleSheet = "QWidget { background-color: black }";
  44. redb.Move(30, 30);
  45. greenb.Move(30, 80);
  46. blueb.Move(30, 130);
  47. square.SetGeometry(150, 25, 150, 150);
  48. }
  49. [Q_SLOT]
  50. public void OnToggled(bool @checked)
  51. {
  52. int red = col.Red;
  53. int green = col.Green;
  54. int blue = col.Blue;
  55. if (redb.Checked)
  56. {
  57. red = 255;
  58. } else {
  59. red = 0;
  60. }
  61. if (greenb.Checked)
  62. {
  63. green = 255;
  64. } else {
  65. green = 0;
  66. }
  67. if (blueb.Checked)
  68. {
  69. blue = 255;
  70. } else {
  71. blue = 0;
  72. }
  73. col = new QColor(red, green, blue);
  74. string sheet = System.String.Format("QWidget {{ background-color: {0} }}",
  75. col.Name());
  76. square.StyleSheet = sheet;
  77. }
  78. [STAThread]
  79. public static int Main(string[] args)
  80. {
  81. new QApplication(args);
  82. new QyotoApp();
  83. return QApplication.Exec();
  84. }
  85. }

在代码示例中,我们使用三个切换按钮来更改矩形小部件的颜色。

  1. QWidget square;
  2. QColor color;
  3. QPushButton redb;
  4. QPushButton greenb;
  5. QPushButton blueb;

我们定义了五个对象。 方形小部件是QWidget,它显示颜色。 color变量用于保存颜色值。 这三个按钮是切换按钮,用于混合颜色值。

  1. redb = new QPushButton("Red", this);
  2. redb.Checkable = true;

我们创建一个QPushButton小部件。 Checkable属性将按钮更改为切换按钮。

  1. redb.Toggled += OnToggled;
  2. greenb.Toggled += OnToggled;
  3. blueb.Toggled += OnToggled;

所有三个按钮都插入到一个方法调用中,即OnToggled()方法。

  1. square = new QWidget(this);
  2. square.StyleSheet = "QWidget { background-color: black }";

我们创建方形小部件。 一开始是黑色的。 在 Qyoto 中,我们使用样式表来自定义小部件的外观。

OnToggled()方法内部,我们确定颜色值并将正方形小部件更新为新颜色。

  1. int red = col.Red;
  2. int green = col.Green;
  3. int blue = col.Blue;

在这里,我们确定方形小部件的当前颜色。

  1. if (redb.Checked)
  2. {
  3. red = 255;
  4. } else {
  5. red = 0;
  6. }

根据红色切换按钮的状态,更改颜色的红色部分。

  1. col = new QColor(red, green, blue);

我们创建一个新的颜色值。

  1. string sheet = System.String.Format("QWidget {{ background-color: {0} }}",
  2. col.Name());

我们使用 C# Format方法创建适当的样式表。

  1. square.StyleSheet = sheet;

正方形的颜色已更新。

281.md - 图4

图:开关按钮

QComboBox

QComboBox是一个小部件,允许用户从选项列表中进行选择。 这是一个显示当前项目的选择小部件,可以弹出可选择项目的列表。 组合框可能是可编辑的。 它以占用最少屏幕空间的方式向用户显示选项列表。

  1. using System;
  2. using QtCore;
  3. using QtGui;
  4. /**
  5. * ZetCode Qyoto C# tutorial
  6. *
  7. * This program uses the QComboBox widget.
  8. * The option selected from the combo box is
  9. * displayed in the label widget.
  10. *
  11. * @author Jan Bodnar
  12. * website zetcode.com
  13. * last modified October 2012
  14. */
  15. public class QyotoApp : QWidget
  16. {
  17. QLabel label;
  18. public QyotoApp()
  19. {
  20. WindowTitle = "QComboBox";
  21. InitUI();
  22. Resize(250, 150);
  23. Move(300, 300);
  24. Show();
  25. }
  26. public void InitUI()
  27. {
  28. label = new QLabel("Ubuntu", this);
  29. QComboBox combo = new QComboBox(this);
  30. combo.AddItem("Ubuntu");
  31. combo.AddItem("Arch");
  32. combo.AddItem("Fedora");
  33. combo.AddItem("Red Hat");
  34. combo.AddItem("Gentoo");
  35. combo.Move(50, 30);
  36. label.Move(50, 100);
  37. combo.ActivatedString += OnActivated;
  38. }
  39. [Q_SLOT]
  40. public void OnActivated(string text)
  41. {
  42. label.Text = text;
  43. label.AdjustSize();
  44. }
  45. [STAThread]
  46. public static int Main(String[] args)
  47. {
  48. new QApplication(args);
  49. new QyotoApp();
  50. return QApplication.Exec();
  51. }
  52. }

在我们的代码示例中,我们有两个小部件。 组合框和标签小部件。 从组合框中选择的选项显示在标签中。

  1. label = new QLabel("Ubuntu", this);

这是一个标签,它将显示组合框中当前选择的选项。

  1. QComboBox combo = new QComboBox(this);

我们创建QComboBox小部件的实例。

  1. combo.AddItem("Ubuntu");
  2. combo.AddItem("Arch");
  3. combo.AddItem("Fedora");
  4. combo.AddItem("Red Hat");
  5. combo.AddItem("Gentoo");

组合框将填充值。

  1. combo.ActivatedString += OnActivated;

当我们从组合框中选择一个选项时,将触发OnActivated()方法。

  1. [Q_SLOT]
  2. public void OnActivated(string text)
  3. {
  4. label.Text = text;
  5. label.AdjustSize();
  6. }

OnActivated()方法中,我们将标签小部件更新为从组合框中选择的当前字符串。

281.md - 图5

图:QComboBox小部件

在 Qyoto C# 教程的这一部分中,我们介绍了几个 Qyoto 小部件。

{% endraw %}