Home  Contents

Qyoto dialogs

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 Qyoto;

/**
 * ZetCode Qyoto C# tutorial
 *
 * This program shows
 * QMessageBox dialogs
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified April 2009
 */


public class QyotoApp : QWidget {

    
    public QyotoApp() {

        SetWindowTitle("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 warning = new QPushButton("Warning", this);
        QPushButton question = new QPushButton("Question", this);
        QPushButton information = new QPushButton("Information", this);
        QPushButton about = new QPushButton("About", this);

        grid.AddWidget(error, 0, 0);
        grid.AddWidget(warning, 0, 1);
        grid.AddWidget(question, 1, 0);
        grid.AddWidget(information, 1, 1);
        grid.AddWidget(about, 2, 0);

        Connect(error, SIGNAL("clicked()"), this, SLOT("ShowDialog()"));
        Connect(warning, SIGNAL("clicked()"), this, SLOT("ShowDialog()"));
        Connect(question, SIGNAL("clicked()"), this, SLOT("ShowDialog()"));
        Connect(information, SIGNAL("clicked()"), this, SLOT("ShowDialog()"));
        Connect(about, SIGNAL("clicked()"), this, SLOT("ShowDialog()"));
        
    }

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

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


    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(button.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 item from a list. A label must be set to tell the user what they should enter.


using System;
using Qyoto;

/**
 * ZetCode Qyoto C# tutorial
 *
 * This program shows an input
 * dialog
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified April 2009
 */


public class QyotoApp : QWidget {

    QLineEdit edit;
    
    public QyotoApp() {

        SetWindowTitle("QInputDialog");

        InitUI();

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

    void InitUI() {
        
        QPushButton show = new QPushButton("Dialog", this);
        
        Connect(show, SIGNAL("clicked()"), this, SLOT("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.SetText(text);
        }
    }


    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.SetText(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 Qyoto;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, we use the
 * QColorDialog to change the color
 * of a label text
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified April 2009
 */


public class QyotoApp : QWidget {

    QLabel label;
    
    public QyotoApp() : base() {

        SetWindowTitle("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 = (int) Qt.AlignmentFlag.AlignCenter;
        vbox.AddWidget(label);
    }
    

    protected override void MousePressEvent(QMouseEvent arg1) {

        QColor color = QColorDialog.GetColor();

        if (!color.IsValid()) return;

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



    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 color dialog. We change the text foreground color to the selected color from the dialog.

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

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

QColor color = QColorDialog.GetColor();

The QColorDialog is being created. The selected color is stored in the color 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 Qyoto;

/**
 * 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 April 2009
 */


public class QyotoApp : QWidget {

    QLabel label;
    
    public QyotoApp() : base() {

        SetWindowTitle("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 = (int) Qt.AlignmentFlag.AlignCenter;
        vbox.AddWidget(label);
    }
    

    protected override void MousePressEvent(QMouseEvent arg1) {

        bool ok = true;
        QFont font = QFontDialog.GetFont(ref ok);
        if (!ok) return;
        label.Font = font;
    }



    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.

HomeContentsTop of Page