Results 1 to 8 of 8

Thread: Unable to use Ok and cancel button as they are supposed to be used

  1. #1
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Unable to use Ok and cancel button as they are supposed to be used

    Hi,

    I have been trying to use the OK and Cancel buttons to perform their default behavior. I believe that my previous attempts to close the dialog box or click on Cancel did not modify the file and only upon clicking OK the file is updated.
    However, recently I have observed that upon clicking on cancel an empty line gets added to the file and even on clicking close.

    The ok and cancel buttons are in a buttonbox. They have signals accepted() and rejected() respectively and slots accept() and reject() in them; this is mentioned in the designer. But their implementation is missing in the file.

    My attempts to connect them using the connect function is not working and I get a message saying the functions are protected.

    Any suggestions?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unable to use Ok and cancel button as they are supposed to be used

    accept() and reject() have default implementations in QDialog.
    You can overwrite them (they are virtual) if you need to change the behavior, e.g. not close the dialog under certain circumstances.

    But your problem description sounds more like as if the dialog is being used incorrectly.
    Do you run the dialog with exec()?
    Do you check the return value against QDialog::Accepted or QDialog::Rejected?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: Unable to use Ok and cancel button as they are supposed to be used

    I am executing it using exec().
    However I am not checking for Qdialog::Accepted and QDialog::Rejected. I believe it was behaving as expected earlier. Only recently I am noticing this change. I do not think that I changed the code before I noticed the problem.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unable to use Ok and cancel button as they are supposed to be used

    Well, apparently your code that decides whether to append to the file or not is broken.
    So you either do not check the return value of exec() correctly or your dialog uses signals to communicate values and emits them wrongly.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: Unable to use Ok and cancel button as they are supposed to be used

    Hi,

    The accepted and accept methods are in the designer under the signals ans slots section. Earlier without explicitly specifying them to connect using signal and slot they were behaving as expected. However, I am unaware as to what changes I need to make in order to make them behave as expected.
    Any help please

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

    Default Re: Unable to use Ok and cancel button as they are supposed to be used

    If you have not reimplemented QDialog::accept() or QDialog::reject() and the modal dialog closes when you click either Ok or Cancel then QDialog is behaving exactly designed and as it always has.

    QDialog has no code that will write to a file: that is happening somewhere in your code. Put some break points before each place your code writes to the file and see which one is called when you close the dialog.

  7. #7
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Accepted() accept(),rejected() reject() not working as expected

    I am facing an issue in that I have declared the Standard Buttons Ok and Cancel in a buttonbox but am unable to use their signals accepted() and accept() and rejected() and Reject(). I am unable to figure out why and its a bit mind irritating because they have been declared in the Qt Designer.

    However even in spite to attempts to connect them externally I am still unable to get the desired result.
    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    I need the dialog to close without adding any blank spaces to the file, unfortunately it still does that.
    Looking forward to any advice.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,120
    Thanks
    235
    Thanked 656 Times in 646 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Accepted() accept(),rejected() reject() not working as expected

    I need the dialog to close without adding any blank spaces to the file, unfortunately it still does that.
    You aren't listening to us. QDialog::accepted() and QDialog::rejected() do not do anything with files. They are just signals, and their default action is to close the dialog they are associated with.

    Somewhere in your code you are opening a file and writing something to it. You need to find where that is, analyze the logic behind what your code is doing, and change it so it doesn't do anything with your file when you don't want it to.

    In this entire thread, you've showed us exactly two lines of code and absolutely nothing about where or how you use those lines of code, what happens before you exec() your dialog, what happens after you exec() your dialog, or what happens in either of your two slots. You haven't presented any evidence that you have even declared and implemented the slots correctly or that Qt is actually establishing the connection at run time. Have you looked at the Output window to see if there are error messages about these connect statements?

    The bottom line is your code has a logic error in it which is causing the effects you see. Until you accept that fact and investigate it, you're just wasting everyone's time with your posts.

Similar Threads

  1. QFileDilaog:: cancel button
    By kulsekarr in forum Newbie
    Replies: 2
    Last Post: 20th March 2015, 12:32
  2. QFileDialog::getSaveFileName Cancel Button
    By penny in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2011, 16:46
  3. Replies: 1
    Last Post: 17th February 2010, 23:17
  4. QDialog, editingFinished, and Cancel Button.
    By jeffpogo in forum Qt Programming
    Replies: 8
    Last Post: 3rd June 2009, 03:22
  5. How to show QProgressDialog without cancel button
    By rajesh in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2007, 09:53

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.