#ifndef LOTTO_H
#define LOTTO_H
#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>
class lotto : public QWidget
{
Q_OBJECT
private:
//widget data members
QLabel* numbersLabel;
QTextEdit* numEdit;
QPushButton* lotusButton;
public slots:
int generateLotteryNumbers ();
void displayNumbers();
public:
lotto();
};
#endif // LOTTO_H
#include "lotto.h"
#include <stdlib.h>
#include <time.h>
#include <QWidget>
#include <QGridLayout>
#include <iostream>
#include <sstream>
#include <QString>
#include "lotto.h"
using namespace std;
lotto::lotto(){
setWindowTitle("Lotto Numbers");
QGridLayout* layout = new QGridLayout(this);
lotusButton = new QPushButton ("Lotto Numbers");
numbersLabel = new QLabel ("Your lucky numbers are:");
numEdit = new QTextEdit();
layout->addWidget(lotusButton, 0,0);
layout->addWidget(numbersLabel, 1,0);
layout->addWidget(numEdit, 1,1);
setLayout(layout);
//connect signals and slots
connect(lotusButton,SIGNAL(clicked()),this, SLOT(generateLotteryNumbers()));
}
int lotto::generateLotteryNumbers (){
srand ( time(NULL) );
int i, j, num, duplicates, numbers[6];
for ( i = 0; i < 6; i++ ) {
do {
num = 1 + rand()%49;
duplicates = 0;
for ( j = 0; j < i; j++ ) {
if ( numbers[ j ] == num ) duplicates = 1;
}
}
while (duplicates);
numbers[ i ] = num;
}
}
void lotto::displayNumbers(){
numEdit->setText(QString::number(generateLotteryNumbers()));
}
我尝试在textedit框中显示6个乐透号码。使用lineedit更好吗?单击乐透编号按钮时,文本框保持为空。为什么?如何让文本框(或信箱)显示数字?
我试过了:
void lotto::displayNumbers(){
srand ( time(NULL) );
int i, j, num, duplicates, numbers[6];
for ( i = 0; i < 6; i++ ) {
do {
num = 1 + rand()%49;
duplicates = 0;
for ( j = 0; j < i; j++ ) {
if ( numbers[ j ] == num ) duplicates = 1;
}
}
while (duplicates);
numbers[ i ] = num;
}
numEdit->setText(QString::number(num));
}
每次点击按钮我都会得到一个不同的号码(一次一个)
答案 0 :(得分:0)
正如我从代码中看到的那样,点击按钮后甚至没有调用lotto::displayNumbers
方法。按钮单击信号改为连接到lotto::generateLotteryNumbers
方法。
此外,我在lotto::generateLotteryNumbers
方法中看不到任何返回语句。
顺便说一句,如果你在调试器中花一分钟时间,你可以自己搞清楚。