If you are creating your QStandardItemModel on the stack in your QDialog or if you are creating it on the heap with the QDialog as its parent, in either case it will be destroyed when the QDialog is destroyed.
In the first case (stack), you cannot pass the pointer to the stack instance (&model) in the signal, because the model will be destroyed no matter what happens.
In the second case, you can create the model -without a parent-. In this case the model will outlive the dialog. However, it will also outlive anything else and become a memory leak.
The solution I would use is to make the model a member variable of the MainWindow class, pass it to the dialog when you open it, and let the dialog update it:
// MainWindow.h:
{
// ...
private:
};
// MainWindow.cpp
MainWindow
::MainWindow( QWidget * parent
){
ui->setupUi();
}
void MainWIndow::doDialog()
{
MyDialog dlg;
dlg.setModel( myModel ); // you need to write this method for your dialog class
connect( &dlg, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged( int, int ) );
dlg.exec();
}
// MainWindow.h:
class MainWindow : public QMainWindow //...
{
// ...
private:
QStandardItemModel * myModel;
};
// MainWindow.cpp
MainWindow::MainWindow( QWidget * parent )
: QMainWindow( parent )
{
ui->setupUi();
myModel = new QStandardItemModel( this );
}
void MainWIndow::doDialog()
{
MyDialog dlg;
dlg.setModel( myModel ); // you need to write this method for your dialog class
connect( &dlg, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged( int, int ) );
dlg.exec();
}
To copy to clipboard, switch view to plain text mode
Note that your signal / slot don't need the QStandardItemModel pointer, because your MainWindow and Dialog already know what it is.
Bookmarks