C ++ memo-QT (3), Memo-qt

Source: Internet
Author: User

C ++ memo-QT (3), Memo-qt

Guess digital games

All content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

Main. cpp

/*

* Guess digital games

* 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.exe c ();

}

 

Guessnumber. cpp

/*

* Number Prediction

* Author: http://blog.csdn.net/myhaspl

* Date: 2015.02.25

*/

# Include <QTime>

# Include "guessnumber. h"

 

GuessNumber: GuessNumber (){

Init ();

}

VoidGuessNumber: GenerateNumber (){

M_guessnumber = qrand () % 500; // generate a random number less than 500

}

IntGuessNumber: GuessCompare (intguessnumber ){

// If the return value is 0, the value is correct. If the positive value is greater, the negative value indicates smaller.

Returnguessnumber-m_guessnumber;

}

VoidGuessNumber: Init (){

QTimet;

T = QTime: currentTime ();

Qsrand (t. msec () + t. second () * 1000); // each time the program is started, different initial values are generated.

GenerateNumber ();

}

Guessnumber. h

/*

* Number Prediction

* Author: http://blog.csdn.net/myhaspl

* Date: 2015.02.25

*/

# IfndefGUESSNUMBER_H

# DefineGUESSNUMBER_H

ClassGuessNumber {

Private:

Intm_guessnumber; // actual computer number

VoidGenerateNumber ();

Public:

GuessNumber ();

VoidInit ();

Int GuessCompare (intguessnumber );

};

# Endif // GUESSNUMBER_H

Guessdialog. h

/*

* "Guess number" dialog box

* 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 * gustmt;

QPushButton * quitBt;

GuessNumber * myGuessNumber;

VoidGuessInit ();

Privateslots:

VoidGuessMyNumber (); // guess the number

VoidGuessIsOk (constQString & myNumber); // enter a number to start guessing.

VoidGuess (intguessResult );

};

# Endif // GUESSDIALOG_H

All content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

Guessdialog. cpp

/*

* "Guess number" dialog box

* 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 ("enter a number within 1-\ n and the system will prompt the result "));

NumberLe = newQLineEdit;

ResultLb = newQLabel (tr ("Result \ n "));

Gustmt = newQPushButton (tr ("Guess & G "));

QuitBt = newQPushButton (tr ("Quit game & Q "));

NumberLe-> setFocus ();

Gustmt-> setEnabled (false );

QObject: connect (numberLe, SIGNAL (textChanged (constQString &), this, SLOT (GuessIsOk (constQString &)));

QObject: connect (gustmt, 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 (gustmt );

RightdownLayout-> addWidget (quitBt );

RightLayout-> addLayout (rightdownLayout );

 

QHBoxLayout * mainLayout = newQHBoxLayout;

MainLayout-> addLayout (leftLayout );

MainLayout-> addLayout (rightLayout );

SetLayout (mainLayout );

SetWindowTitle (tr ("guess number "));

SetFixedHeight (SizeHint(). Height ());

MyGuessNumber = newGuessNumber (); // generate a random number instance. The constructor has generated the random number and re-assigned the value to the seed.

}

 

VoidGuessDialog: GuessInit (){

MyGuessNumber-> Init ();;

}

 

// Slot function

VoidGuessDialog: GuessIsOk (constQString & myNumber ){

BoolisOk;

MyNumber. toInt (& isOk, 10 );

If (! MyNumber. isEmpty () & isOk ){

Gustmt-> setEnabled (true );

}

Else {

Gustmt-> 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 ("Guess result: \ n guessed it! The game starts again ~ "));

GuessInit ();

}

Elseif (guessResult> 0 ){

ResultLb-> setText (tr ("Guess result: \ n is too big to guess! "));

}

Else {

ResultLb-> setText (tr ("the result is \ n! "));

}

}





1. public, private, and protected cannot be added before signals; slots can be added before, because Qt can be used as a common function.

2. Functions in the signals region must be void type, and these signal functions do not have function bodies. That is to say, these signal functions cannot be defined by yourself. You only need to declare them, qt internally.

3. macro definition and function pointer cannot be used for signal and slot parameters, and the signal and slot cannot have default parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.