Results 1 to 4 of 4

Thread: QStandardItemModel Transfer over SIGNAL/SLOT

  1. #1
    Join Date
    Jun 2017
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QStandardItemModel Transfer over SIGNAL/SLOT

    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:
    Qt Code:
    1. Pattern_creator pattern_creator(colorNames);
    2. QObject::connect(&pattern_creator, SIGNAL(valueChanged(int,int,QStandardItemModel*)), this, SLOT(PatternCreator(int,int,QStandardItemModel*)));
    To copy to clipboard, switch view to plain text mode 


    The function in the main window:

    Qt Code:
    1. void Pattern::PatternCreator(int row, int column, QStandardItemModel *model_test)
    2. {
    3. ui->tableView_test->setModel(model_test);
    4. .
    5. .
    6. .
    7. }
    To copy to clipboard, switch view to plain text mode 

    The call from the Dialog Window:
    Qt Code:
    1. emit valueChanged(row_pattern, column_pattern, model_pattern_creator);
    To copy to clipboard, switch view to plain text mode 

    The Dialog Header :
    Qt Code:
    1. public:
    2. QStandardItemModel *model_pattern_creator;
    3. signals:
    4. void valueChanged(int row_pattern,int column_pattern, QStandardItemModel *model_pattern_creator );
    To copy to clipboard, switch view to plain text mode 

    Thanks for help!

  2. #2
    Join Date
    Jun 2017
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStandardItemModel Transfer over SIGNAL/SLOT

    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?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStandardItemModel Transfer over SIGNAL/SLOT

    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:

    Qt Code:
    1. // MainWindow.h:
    2.  
    3. class MainWindow : public QMainWindow //...
    4. {
    5.  
    6. // ...
    7.  
    8. private:
    9. QStandardItemModel * myModel;
    10. };
    11.  
    12.  
    13. // MainWindow.cpp
    14.  
    15. MainWindow::MainWindow( QWidget * parent )
    16. : QMainWindow( parent )
    17. {
    18. ui->setupUi();
    19.  
    20. myModel = new QStandardItemModel( this );
    21. }
    22.  
    23. void MainWIndow::doDialog()
    24. {
    25. MyDialog dlg;
    26. dlg.setModel( myModel ); // you need to write this method for your dialog class
    27. connect( &dlg, SIGNAL( valueChanged( int, int ) ), this, SLOT( onValueChanged( int, int ) );
    28. dlg.exec();
    29. }
    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    Pschnot1000 (27th June 2017)

  5. #4
    Join Date
    Jun 2017
    Posts
    7
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStandardItemModel Transfer over SIGNAL/SLOT

    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!
    Last edited by Pschnot1000; 27th June 2017 at 07:56.

Similar Threads

  1. Replies: 1
    Last Post: 14th August 2014, 17:08
  2. Replies: 6
    Last Post: 4th March 2014, 15:09
  3. Replies: 2
    Last Post: 22nd August 2013, 10:50
  4. Replies: 8
    Last Post: 7th November 2012, 14:10
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.