PDA

View Full Version : random numbers and slots



Enoctaffie
12th April 2012, 08:21
Hi all

I want to use the funtion srand(time(0)) to generate random numbers but I am getting an error message that time is not declared in this scope, please help.
I also have two QPushButtons "OK" and "Cancel", there is no differenve between clicking on the OK and Cancel buttons. I used a slot to connect to this the code is as follows: connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));

ChrisW67
12th April 2012, 09:03
I want to use the funtion srand(time(0)) to generate random numbers but I am getting an error message that time is not declared in this scope, please help.

Try:

#include <ctime>


I also have two QPushButtons "OK" and "Cancel", there is no differenve between clicking on the OK and Cancel buttons. I used a slot to connect to this the code is as follows: connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
On a generated QDialog I assume... the buttons are connected to the QDialog::accept() or QDialog::reject() slots, which close the dialog and return a different value to the QDialog::exec() call.

Enoctaffie
12th April 2012, 09:36
Hi ChrisW67

thanks the first one worked but I do not understand the second solution. This is how I created the push button and added it to the layout.
QGridLayout* layout = new QGridlayout;
QPushButton* cancel = new QPushButton("Cancel);
QHBoxLayout* buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(cancel);
layout->addWidget(buttonLayout);

nish
12th April 2012, 10:37
what error are you getting? btw above code you presented wont compile.

Enoctaffie
12th April 2012, 11:09
Hi

I am not getting an error, the code is running only that when I click the cancel button the application is not exiting as I expect.

myta212
13th April 2012, 08:52
Hi,
Please check your above code :
This is code for your button signal&slot :


connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));


And this is code in your button design :


QPushButton* cancel = new QPushButton("Cancel");


You have different object name for your "CANCEL BUTTON" :)

Best regards,

myta212

ChrisW67
14th April 2012, 07:54
I also have two QPushButtons "OK" and "Cancel", there is no differenve between clicking on the OK and Cancel buttons. I used a slot to connect to this the code is as follows: connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
Two buttons. If you built a dialog with the wizards in Qt Creator then the default OK and Cancel buttons are already connected for you, which is what my earlier comment was about. It appears that you have built the widget yourself so that does not apply.

I am not getting an error,
There is only one button in your pasted code and the code you posted will not compile. Therefore the code you are actually running is different to what you have described and shown us.

the code is running only that when I click the cancel button the application is not exiting as I expect.
There is nothing in the code that would guarantee the application will quit. QWidget::close() hides the widget if the event is accepted (the default). That will only quit the application if the widget concerned is the last top level widget in the program and you have not disabled this by fiddling with the Qt::WA_QuitOnClose attribute of the widget or the QApplication quitOnLastWindowClosed property. We don't know if it is the the last window or not; but the behaviour you describe would imply that it is not.