You should not need to create new instances of QMenu every time you use it. For the main menu it is put into the constructor of the parent. If you use QAction in the menus you really don't need to deal with it too much (depends on your goal of course). If you have a context menu you can create it on the stack and not on the heap. The Qt documentation has some good examples: http://doc.qt.io/qt-5/qtwidgets-main...s-example.html
For dialogs you can use a combination of methods. They can be on the stack if appropriate (popup message-box type of dialog) or you can create an instance of a dialog and attach it to the parent class (use 'new' in the constructor). If you are constantly creating a new instanace of a dialog then you need to find a way to delete it or you will exhaust memory sooner or later. I have not seen a need to do this yet.
My personal preference for dialogs is to create them on the heap (using operator new) but only on first use. The parent widget will be contructed faster and the resources required by the dialog will only be used if the dialog is actually created. You only create one instance of the dialog so some sort of 'Reset' function is required to ensure it is at a state suitable for use (does not display previous data). This applies to both modal and modeless dialogs.
// Constructor
d_my_dialog = 0; // pointer to instance of my dialog
// inside some function that uses dialog
if(!d_my_dialog)
{
d_my_dialog = new TMyDialog(this); // this open happens on first use
}
d_my_dialog->Reset(); // reset the dialog to a state that is suitable (repeated use)
if(QDialog::Accepted == d_my_dialog
->exec
()) {
// do something with dialog data or whatever
}
// Constructor
d_my_dialog = 0; // pointer to instance of my dialog
// inside some function that uses dialog
if(!d_my_dialog)
{
d_my_dialog = new TMyDialog(this); // this open happens on first use
}
d_my_dialog->Reset(); // reset the dialog to a state that is suitable (repeated use)
if(QDialog::Accepted == d_my_dialog->exec())
{
// do something with dialog data or whatever
}
To copy to clipboard, switch view to plain text mode
Bookmarks