1. 程式人生 > >qt實現掃雷三:對話方塊設計

qt實現掃雷三:對話方塊設計

qt實現掃雷三:對話方塊設計

對話方塊實現掃雷雷區大小和雷數的設定

問題總結

1、提供給上層物件介面,設定三個引數或獲取三個引數
2、繼承自QDialog類
3、定義槽函式時,需要有巨集,類繼承自QObject時需要有這個巨集定義

Q_OBJECT

4、以下語句可以設定LineEdit的輸入範圍,限制輸入整數型別需要引入標頭檔案

   #include <QIntValidator>
   column_edit->setValidator(new QIntValidator(0, COLUMN, this));

5、使用除錯列印命令

#include <QDebug>
qdebug()<<" "; 

6、使用訊息對話方塊

#include <QMessageBox>
QMessageBox::information(NULL, "hello", "illegal input",
                                         QMessageBox::Ok, QMessageBox::Ok);

7、程式執行效果如下:
在這裡插入圖片描述

標頭檔案

1,定義設定雷區引數對話方塊類

class thunder_dialog : public QDialog
{
    Q_OBJECT

public:
    thunder_dialog(int x = 9, int y = 9, int num = 10, QWidget *parent = 0);

    //獲取行數
    int getRow();
    //獲取列數
    int getColumn();
    //獲取雷數
    int getNumber();
    //設定行數,列數,雷數引數
    void setParameters(int x, int y, int num);
private slots:
    //確定按鈕點選後響應事件的槽函式
    void confirmButtonClicked();

private:
    QPushButton *confirm;
    QPushButton *concel;
    QLabel *row_label;
    QLabel *column_label;
    QLabel *number_label;
    QLineEdit *row_edit;
    QLineEdit *column_edit;
    QLineEdit *number_edit;

private:
    int row;
    int column;
    int number;
};

原始檔

thunder_dialog::thunder_dialog(int x, int y, int num, QWidget *parent)
    :row(x), column(y), number(num), QDialog(parent)
{
    setFixedSize(200, 150);
    confirm = new QPushButton(this);
    confirm->setGeometry(50, 120, 50, 20);
    confirm->setText("confirm");
    connect(confirm, SIGNAL(clicked()), this, SLOT(confirmButtonClicked()));

    concel = new QPushButton(this);
    concel->setGeometry(100,120,50,20);
    concel->setText("cancel");
    connect(concel, SIGNAL(clicked()), this, SLOT(close()));

    row_label = new QLabel(this);
    row_label->setText("行數:");
    row_label->setGeometry(50,30,50,20);

    column_label = new QLabel(this);
    column_label->setText("列數:");
    column_label->setGeometry(50,60,50,20);

    number_label = new QLabel(this);
    number_label->setText("雷數:");
    number_label->setGeometry(50,90,50,20);

    row_edit = new QLineEdit(this);
    row_edit->setGeometry(100,30,50,20);
    row_edit->setText(QString::number(row));
    row_edit->setValidator(new QIntValidator(0, ROW, this));

    column_edit = new QLineEdit(this);
    column_edit->setGeometry(100,60,50,20);
    column_edit->setText(QString::number(column));
    column_edit->setValidator(new QIntValidator(0, COLUMN, this));

    number_edit = new QLineEdit(this);
    number_edit->setGeometry(100,90,50,20);
    number_edit->setText(QString::number(number));
    number_edit->setValidator(new QIntValidator(0, ROW * COLUMN, this));
}

int thunder_dialog::getRow()
{
    return row;
}

int thunder_dialog::getColumn()
{
    return column;
}

int thunder_dialog::getNumber()
{
    return number;
}

void thunder_dialog::setParameters(int x, int y, int num)
{
    row = x;
    column = y;
    number = num;
}

void thunder_dialog::confirmButtonClicked()
{
    int x = row_edit->text().toInt();
    int y = column_edit->text().toInt();
    int z = number_edit->text().toInt();

    if(x <= 0 || x >= ROW || y <= 0 || y >= COLUMN || z < 0 || z > x * y)
    {
        QMessageBox::StandardButton rb = QMessageBox::information(NULL, "hello", "illegal input",
                                     QMessageBox::Ok, QMessageBox::Ok);
        if(rb == QMessageBox::Ok)
        {
            return;
        }else
        {

        }
        return;
    }
    qDebug()<<x<<" "<<y<<" "<<z;
    row = x;
    column = y;
    number = z;
    close();
}