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




#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QTimer>
#include <QLineEdit>
#include <QPushButton>
#include <QStringList>

class MainWindow : public QWidget {
    Q_OBJECT
public:
    MainWindow();
    void setTimer();
    void DisplayMessage();

 public slots:

    void changeTimerValue();
    void toggleTimer();
    void TimeOut();

private:
    static QStringList m_messages;
    static int m_current_message;
    QTimer* m_timer;
    QLineEdit* m_timer_entry;
    QPushButton* m_start_timer;
    //QPushButton* m_set_timer;
    QLabel* lblRandomMsg;
    QLabel* lblMsg;
    bool m_started;

};
#endif // MAINWINDOW_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()));
    }

I try to display 6 lotto numbers in a textedit box. Is it better to use a lineedit? The textbox remains empty when I click the lotto number button. Why? How can I get the textbox (or the linebox) to display the numbers?

Edited 3 Years Ago by annitaz

Taking a quick glance at it says that you've never added anything to actually call the function to display the numbers in the control. You connected the button to the generateLotteryNumbers function, but nothing ever calls displayNumbers.

In regards to the functions, in general, take a look at your logic. The generate function is supposed to return an integer, but doesn't return anything. As for displayNumbers, why have it call generateLotteryNumbers when that happens when the button is clicked anyway? Shouldn't it simply display the numbers as the name suggests?

To break things up functionally, possibly change your signals/slots so that displayNumbers is used instead of generateNumbers. Modify generateNumbers to have an integer as a parameter so that display can pass it over and then use it for display.

Just a few suggestions from a quick look.

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