Results 1 to 7 of 7

Thread: random numbers and slots

  1. #1
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default random numbers and slots

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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: random numbers and slots

    Quote Originally Posted by Enoctaffie View Post
    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:
    Qt Code:
    1. #include <ctime>
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by ChrisW67; 12th April 2012 at 09:06. Reason: Fixed slot names

  3. #3
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: random numbers and slots

    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);

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: random numbers and slots

    what error are you getting? btw above code you presented wont compile.

  5. #5
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slots

    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.

  6. #6
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: slots

    Hi,
    Please check your above code :
    This is code for your button signal&slot :
    Qt Code:
    1. connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    To copy to clipboard, switch view to plain text mode 

    And this is code in your button design :
    Qt Code:
    1. QPushButton* cancel = new QPushButton("Cancel");
    To copy to clipboard, switch view to plain text mode 

    You have different object name for your "CANCEL BUTTON"

    Best regards,

    myta212

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: slots

    Quote Originally Posted by Enoctaffie
    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.

  8. The following user says thank you to ChrisW67 for this useful post:

    Enoctaffie (19th April 2012)

Similar Threads

  1. Random markers from nowhere.
    By Spitfire in forum Qwt
    Replies: 10
    Last Post: 3rd November 2011, 14:43
  2. Replies: 7
    Last Post: 22nd November 2010, 18:35
  3. Replies: 1
    Last Post: 7th April 2010, 16:26
  4. Qt large random numbers
    By timmu in forum Qt Programming
    Replies: 11
    Last Post: 31st August 2009, 08:22
  5. random
    By raphaelf in forum General Programming
    Replies: 9
    Last Post: 6th June 2007, 12:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.