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