#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?

Dani AI

Generated

Short diagnosis: the button is wired to a slot that never updates the UI and that slot is declared to return an int but does not return anything. As hinted, either call a display slot from the button or have the slot you connect actually format and set text in the widget. For a simple one-line result a QLineEdit is easier; for selectable/multi-line output keep QTextEdit (set it read-only).

Fix outline (practical, small changes)

  • Make the button call a void slot that updates the UI (e.g., displayNumbers()).
  • Make generateLotteryNumbers(...) return a container of ints (QList<int> or QVector<int>) or fill a member variable.
  • Format those ints into a QString and call setText() (QLineEdit) or setPlainText()/setText() (QTextEdit).
  • Don’t call srand(time(NULL)) on every click. Seed once or use std::mt19937 / QRandomGenerator.

Example (different approach — produce unique numbers by shuffling the 1..49 pool)

QList<int> lotto::generateLotteryNumbers(int count = 6, int min = 1, int max = 49)
{
    QVector<int> pool;
    for (int v = min; v <= max; ++v) pool.append(v);
    std::shuffle(pool.begin(), pool.end(), std::mt19937{static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count())});
    QList<int> out;
    for (int i = 0; i < count; ++i) out.append(pool[i]);
    std::sort(out.begin(), out.end());
    return out;
}

void lotto::displayNumbers()
{
    auto nums = generateLotteryNumbers();
    QStringList parts;
    for (int n : nums) parts << QString::number(n);
    numEdit->setPlainText(parts.join(" "));
}

Extra tips

  • Use the new signal/slot syntax: connect(lotusButton, &QPushButton::clicked, this, &lotto::displayNumbers); so mismatched signatures are less likely.
  • Turn on compiler warnings; they would flag a non-void function that never returns.
  • If you only want one line, replace QTextEdit with QLineEdit and call setText(...).

This directly addresses why the text area stayed empty and gives a safe, modern way to generate and display unique lotto numbers.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.