3

I've made my first qt window. Now I'd like to make my first dialog, using qt. I have just finished creating the dialog, which is basically made of a QDialogButtonBox, and now I'd like to connect it to the window. I have two beginner's questions:

  • How can I retrieve how the dialog was closed (ok pressed or cancel pressed) from the window.cpp, which creates a new dialog, and then calls dialog->show() ?
  • Where and how to destroy the dialog pointer ?
2
  • Search Standard Dialogs Example in Qt help documentation. This is a very good example. Commented Jan 4, 2013 at 4:48
  • I had read some of it but I didn't understand it all, and I don't think the answer to my question is there. Commented Jan 4, 2013 at 5:06

3 Answers 3

2

If you use dialog->show() then I assume it's non-modal dialog.

If you have created QDialogButtonBox and connected its signals with accept() and reject() slots of your dialog as documentation shows, then your dialog will emit finished(int) and additionally accepted() or rejected() signals by which you can determine how it was closed.

If you need more customized behavior, then you can reimplement closeEvent(QCloseEvent *event) or create your own singnals.

If you need to delete your dialog you can use setAttribute(Qt::WA_DeleteOnClose, true);, which will delete instance on close.

Sign up to request clarification or add additional context in comments.

Comments

1

you can use one flag, and signal and slot. when put OK flag=1 , and when put cancel then flag=-1; and then use signal. in in the window.h write code how to handle that flags with 1 simple slot.

for destroying the pointer you can use signal and slot in your Dialog and tell when user push Ok, or Cancel , or exit (up- right (red cross)) go to slot in call the Destructer of dialog

and also you that you better set parent of dialog to window.

Comments

1
  • First Question:

    When you want to show the dialog,just construct it,using myDialog *d = new myDialog(this)(the this pointer will make sure that you havn't to delete the pointer you created 'cause Qt will handle this if you specified the dialog's parent). And use d->exec() if you need a modal dialog, or d->show() to make it non-modal;

  • Second Question:

    Once your specified the dialog's parent object, all u need is just use it and leave alone the memory managent,Qt will do this for you. Also you can use d->setAttribute(Qt::WA_DeleteOnClose,true) to make it destroy itself when it is closed.

Remember to link the QDialogButtonBox to your dialog's actions.

3 Comments

Your first answer doesn't refer to question. Question was - how to determine if dialog was closed (and how). In second answer you say that dialog can be left alone if parent was set. It is very likely that main window will be a parent widget for a dialog, so memory won't be freed until application exit. Also what is the point to repeat my tips 4 hours later?
Sorry,man. I answered this question right after i got into my office, open my PC and started the day, it seems that we are not in the same timezone. And you think i'm repeating your tips. When i saw the Question, i gave the answer immediately, and sorry for not notice your tips. I just tring to Help. My English is not good enough, so may my reply help. :)
I thought that you were a kid and I apologize Sir for my didactic style. Now I see that because of timezone issue you see things 4 hours later. Fortunately I adjusted my desktop clock to match your timezone and you have a chance to see this comment immediately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.