#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 , slots connect(lotusbutton,signal(clicked()),this, slot(generatelotterynumbers())); } int lotto::generatelotterynumbers (){ srand ( time(null) ); int i, j, num, duplicates, numbers[6]; ( = 0; < 6; i++ ) { { num = 1 + rand()%49; duplicates = 0; ( j = 0; j < i; j++ ) { if ( numbers[ j ] == num ) duplicates = 1; } } while (duplicates); numbers[ ] = num; } } void lotto::displaynumbers(){ numedit->settext(qstring::number(generatelotterynumbers())); }
i try display 6 lotto numbers in textedit box. better use lineedit? textbox remains empty when click lotto number button. why? how can textbox (or linebox) display numbers?
i tried this:
void lotto::displaynumbers(){ srand ( time(null) ); int i, j, num, duplicates, numbers[6]; ( = 0; < 6; i++ ) { { num = 1 + rand()%49; duplicates = 0; ( j = 0; j < i; j++ ) { if ( numbers[ j ] == num ) duplicates = 1; } } while (duplicates); numbers[ ] = num; } numedit->settext(qstring::number(num)); }
and each time click button different number (one @ time)
as can see code lotto::displaynumbers
method not being called after button click. button click signal connected lotto::generatelotterynumbers
method instead.
also can not see return statements in lotto::generatelotterynumbers
method.
by way figure out if spend minute in debugger.
Comments
Post a Comment