#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>
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 (){
   return (rand() % 49)+ 1;
}
void lotto::displayNumbers(){
    numEdit->setText(generateLotteryNumbers());
}

I can't get the random numbers to be displayed in the text box. why?

You can use stringstream

Also it appears your generateLotteryNumbers () will only produce one number. You might want to recode it with a loop and maybe return a string of the numbers, or maybe an array of int's

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.