返回介绍

Qyoto dialogs

发布于 2025-02-22 22:19:46 字数 10127 浏览 0 评论 0 收藏 0

In this part of the Qyoto C# programming tutorial, we will work with dialogs.

Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.

Message boxes

Message boxes are convenient dialogs that provide messages to the user of the application. The message consists of text and image data.

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();
  }
}

We use the GridLayout manager to set up a grid of five buttons. Each of the buttons shows a different message box.

QPushButton button = (QPushButton) Sender();

Here we determine, which button called the ShowDialog() method.

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

In case we pressed the error button, we show the error dialog. We use static methods of the QMessageBox class to show the message boxes.

Information message dialog Warning message dialog Question message dialog Error message dialog About message dialog

QInputDialog

The QInputDialog class provides a simple convenience dialog to get a single value from the user. The input value can be a string, a number or an from a list. A label must be set to tell the user what they should enter.

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();
  }
}

In the code example, we have a button and a line edit. The button shows an input dialog. We get some text and the text is shown in the line edit widget.

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

The GetText() static method creates the input dialog. The text from the dialog is stored in the text variable.

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

Before we update the line edit, we ensure that the text variable is not null and that it is not empty and does not consists only from spaces.

Input dialog
Figure: Input dialog

QColorDialog

The QColorDialog class provides a dialog widget for specifying colors. The color dialog's function is to allow users to choose colors.

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();
  }
}

We show a some text in the center of the window. By clicking on the area of the window, we show a colour dialog. We change the text foreground color to the selected colour from the dialog.

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

In order to receive mouse press events for our window, we must override the MousePressEvent() method.

QColor col = QColorDialog.GetColor();

The QColorDialog is being created. The selected colour is stored in the col variable.

if (!color.IsValid()) return;

We do nothing, when the cancel button was pressed.

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

Here we update the foreground color of the label's text.

QColorDialog
Figure: QColorDialog

QFontDialog

The QFontDialog class provides a dialog widget for selecting a font.

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();
  }
}

This example is similar to the previous one. This time, we change the font of the text.

QFont font = QFontDialog.GetFont(ref ok);

The QFontDialog is being created. The boolean ok variable is set, when we press the OK button of the dialog.

if (!ok) return;

We do nothing if we didn't press the OK button.

label.Font = font;

The font field stores the selected font. We update the label's font to the newly selected font.

QFontDialog
Figure: QFontDialog

In this part of the Qyoto C# tutorial, we worked with dialog windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文