PDA

View Full Version : Unable to use Ok and cancel button as they are supposed to be used



newtoQ_s
29th September 2015, 11:32
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?

anda_skoa
29th September 2015, 12:09
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,
_

newtoQ_s
29th September 2015, 12:24
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.

anda_skoa
29th September 2015, 13:59
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,
_

newtoQ_s
5th October 2015, 06:49
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

ChrisW67
5th October 2015, 21:18
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.

newtoQ_s
8th October 2015, 14:02
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.

d_stranz
9th October 2015, 16:18
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.