PDA

View Full Version : QStandardItemModel Transfer over SIGNAL/SLOT



Pschnot1000
26th June 2017, 09:50
Hey,

I want to send my TableView QstandardItemModel from my DialogWindow to my Main Window.
My problem is that the Table View in the mainWindow show me an empty table. I probally do something wrong with the pointers, but I am not sure.
Need Help to send the model or maybe there is another method to send the content of the Dialog Table?
Here is my Code:

Signal - Slot connection in the main window:


Pattern_creator pattern_creator(colorNames);
QObject::connect(&pattern_creator, SIGNAL(valueChanged(int,int,QStandardItemModel*)), this, SLOT(PatternCreator(int,int,QStandardItemModel*))) ;



The function in the main window:



void Pattern::PatternCreator(int row, int column, QStandardItemModel *model_test)
{
ui->tableView_test->setModel(model_test);
.
.
.
}


The call from the Dialog Window:


emit valueChanged(row_pattern, column_pattern, model_pattern_creator);



The Dialog Header :


public:
QStandardItemModel *model_pattern_creator;
signals:
void valueChanged(int row_pattern,int column_pattern, QStandardItemModel *model_pattern_creator );



Thanks for help!

Pschnot1000
26th June 2017, 15:32
I think the connection with the pointer is correct. The problem is, that when I close the Dialog Window it destroy the pointer on the model. But how to store the model before it get destroyed?

d_stranz
26th June 2017, 16:55
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:

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();
}


Note that your signal / slot don't need the QStandardItemModel pointer, because your MainWindow and Dialog already know what it is.

Pschnot1000
27th June 2017, 08:56
Thanks for your help d_stranz.

Sounds for me like a good way to do it.
But when I try it like in your example, I get the report that setModel is not a member of myDialog.
What do I have to change in the myDialog form?

Added after 41 minutes:

Ok I found out what you meant. Thanks a lot, for your help!