猜数字游戏
main.cpp
/*
*猜数字游戏
*author: http://blog.csdn.net/myhaspl
*date:2015.02.25
*/
#include<QApplication>
#include"guessdialog.h"
intmain(intargc,char*argv[])
{
QApplicationapp(argc,argv);
GuessDialog*myDialog=newGuessDialog;
myDialog->show();
returnapp.exec();
}
guessnumber.cpp
/*
*猜数字类
*author: http://blog.csdn.net/myhaspl
*date:2015.02.25
*/
#include<QTime>
#include"guessnumber.h"
GuessNumber::GuessNumber(){
Init();
}
voidGuessNumber::GenerateNumber(){
m_guessnumber=qrand()%500;//产生500以内的随机数
}
intGuessNumber::GuessCompare(intguessnumber){
//返回0表示正确,正数表示大了,负数表示小了
returnguessnumber-m_guessnumber;
}
voidGuessNumber::Init(){
QTimet;
t=QTime::currentTime();
qsrand(t.msec()+t.second()*1000);//每次启动程序产生不同的初始值
GenerateNumber();
}
guessnumber.h
/*
*猜数字类
*author:http://blog.csdn.net/myhaspl
*date:2015.02.25
*/
#ifndefGUESSNUMBER_H
#defineGUESSNUMBER_H
classGuessNumber{
private:
intm_guessnumber;//电脑实际数字
voidGenerateNumber();
public:
GuessNumber();
voidInit();
int GuessCompare(intguessnumber);
};
#endif//GUESSNUMBER_H
guessdialog.h
/*
*猜数字对话框
*author:http://blog.csdn.net/myhaspl
*date:2015.02.25
*/
#ifndefGUESSDIALOG_H
#defineGUESSDIALOG_H
#include<QDialog>
#include<QLabel>
#include<QPushButton>
#include<QLineEdit>
#include<QVBoxLayout>
#include<QHBoxLayout>
classGuessNumber;
classGuessDialog:publicQDialog
{
Q_OBJECT
public:
GuessDialog(QWidget*parent=0);
signals:
voidTryGuess(intmyNumber);
private:
QLabel*resultLb;
QLabel*messageLb;
QLineEdit*numberLe;
QPushButton*guessBt;
QPushButton*quitBt;
GuessNumber*myGuessNumber;
voidGuessInit();
privateslots:
voidGuessMyNumber();//猜数字
voidGuessIsOk(constQString&myNumber);//输入数字,可以开始猜数
voidGuess(intguessResult);
};
#endif//GUESSDIALOG_H
guessdialog.cpp
/*
*猜数字对话框
*author:http://blog.csdn.net/myhaspl
*date:2015.02.25
*/
#include"guessdialog.h"
#include"guessnumber.h"
GuessDialog::GuessDialog(QWidget*parent):QDialog(parent){
messageLb=newQLabel(tr("请输入一个1-500以内的数字\n系统将提示猜数结果"));
numberLe=newQLineEdit;
resultLb=newQLabel(tr("猜数结果\n"));
guessBt=newQPushButton(tr("猜一下&G"));
quitBt=newQPushButton(tr("退出游戏&Q"));
numberLe->setFocus();
guessBt->setEnabled(false);
QObject::connect(numberLe,SIGNAL(textChanged(constQString&)),this,SLOT(GuessIsOk(constQString&)));
QObject::connect(guessBt,SIGNAL(clicked()),this,SLOT(GuessMyNumber()));
QObject::connect(quitBt,SIGNAL(clicked()),this,SLOT(close()));
QObject::connect(this,SIGNAL(TryGuess(int)),this,SLOT(Guess(int)));
QVBoxLayout*leftLayout=newQVBoxLayout;
leftLayout->addWidget(messageLb);
leftLayout->addWidget(numberLe);
QVBoxLayout*rightLayout=newQVBoxLayout;
rightLayout->addWidget(resultLb);
QHBoxLayout*rightdownLayout=newQHBoxLayout;
rightdownLayout->addWidget(guessBt);
rightdownLayout->addWidget(quitBt);
rightLayout->addLayout(rightdownLayout);
QHBoxLayout*mainLayout=newQHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("猜数字"));
setFixedHeight(sizeHint().height());
myGuessNumber=newGuessNumber();//生成随机猜数类实例,构造函数中已经对随机数进行生成和种子重新赋值操作。
}
voidGuessDialog::GuessInit(){
myGuessNumber->Init();;
}
//槽函数
voidGuessDialog::GuessIsOk(constQString&myNumber){
boolisOk;
myNumber.toInt(&isOk,10);
if(!myNumber.isEmpty()&&isOk){
guessBt->setEnabled(true);
}
else{
guessBt->setEnabled(false);
}
}
voidGuessDialog::GuessMyNumber(){
QStringmyGuess=numberLe->text();
boolok;
intgsNumber=myGuess.toInt(&ok,10);
intguessResult=myGuessNumber->GuessCompare(gsNumber);
emitTryGuess(guessResult);
}
voidGuessDialog::Guess(intguessResult){
if (guessResult==0){
resultLb->setText(tr("猜数结果:\n猜对了!游戏重新开始~"));
GuessInit();
}
elseif(guessResult>0){
resultLb->setText(tr("猜数结果:\n猜大了!"));
}
else{
resultLb->setText(tr("猜数结果:\n猜小了!"));
}
}
1、signals前面不可加public、private和protected进行修饰;slots前面可以加,因为Qt说槽函数可以当普通函数使用。
2、signals区域的函数必须是void类型,而且这些信号函数没有函数体,也就是说不可以自己定义这些信号函数,你只要声明它就够了,其它不用管,Qt内部自己弄。
3、宏定义和函数指针不能用于信号和槽的参数,信号和槽也不能有缺省参数。
原文地址:http://blog.csdn.net/myhaspl/article/details/43939805