Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: How to correctly re-assign a pointer?

  1. #21
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to correctly re-assign a pointer?

    Quote Originally Posted by homerun4711 View Post
    so the crash should not happen at all?!?
    It happens because DataManager still thinks m_customerTableView is valid. Post full versions of AddressBook and DataManager (both header and cpp). It's diffecult to get the big picture at this point with just code fragments.


    Added after 19 minutes:


    I've got an idea, will take a few to compose my thoughts. Hold off on posting the code if you like.
    Last edited by pkohut; 4th January 2011 at 15:18.

  2. #22
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: How to correctly re-assign a pointer?

    One minute to late
    Here is the complete code:

    AddressBook.h
    Qt Code:
    1. class AddressBook : public QDialog, public Ui::AddressBook
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AddressBook(DataManager* dmgr, QWidget *parent = 0);
    7. ~AddressBook();
    8.  
    9. private:
    10. DataManager* data;
    11.  
    12. };
    13.  
    14. #endif
    To copy to clipboard, switch view to plain text mode 

    AddressBook.cpp
    Qt Code:
    1. AddressBook::AddressBook(DataManager* dmgr, QWidget *parent): QDialog(parent)
    2. {
    3.  
    4. Q_ASSERT(dmgr);
    5. data = dmgr;
    6. Q_ASSERT(data);
    7.  
    8. setupUi(this);
    9. connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    10.  
    11. tableView = dmgr->customerTableView(this);
    12. Q_ASSERT(tableView);
    13.  
    14. }
    15.  
    16. AddressBook::~AddressBook()
    17. {
    18. data->DeleteCustomerTableView();
    19. }
    To copy to clipboard, switch view to plain text mode 

    DataManager.h

    Qt Code:
    1. class DataManager
    2. {
    3. public:
    4. DataManager();
    5.  
    6. QStandardItemModel *customerModel();
    7. QStandardItemModel *articlesModel();
    8. QStandardItemModel *invoicesModel();
    9. QStandardItemModel *invoiceEntriesModel();
    10.  
    11. QTableView *customerTableView(QWidget *parent = 0);
    12. QTableView *articlesTableView(QWidget *parent = 0);
    13. QTableView *invoicesTableView(QWidget* parent = 0);
    14. QTableView *invoiceEntriesTableView(QWidget* parent = 0);
    15.  
    16. QTableView *DeleteCustomerTableView();
    17. QTableView *DeleteArticlesTableView();
    18. QTableView *DeleteInvoicesTableView();
    19. QTableView *DeleteInvoiceEntriesTableView();
    20.  
    21. private:
    22.  
    23. QStandardItemModel *model_customer;
    24. QStandardItemModel *model_articles;
    25. QStandardItemModel *model_invoices;
    26. QStandardItemModel *model_invoiceEntries;
    27.  
    28. QTableView *m_customerTableView;
    29. QTableView *m_articlesTableView;
    30. QTableView *m_invoicesTableView;
    31. QTableView *m_invoiceEntriesTableView;
    32.  
    33. QItemSelectionModel *selectionModelCustomer;
    34. QItemSelectionModel *selectionModelArticles;
    35. QItemSelectionModel *selectionModelInvoices;
    36. QItemSelectionModel *selectionModelInvoiceEntries;
    37.  
    38. QHeaderView *headerViewCustomer;
    39. QHeaderView *headerViewArticles;
    40. QHeaderView *headerViewInvoices;
    41. QHeaderView *headerViewInvoiceEntries;
    42.  
    43. void setupModels();
    44. void setupTableViews();
    45.  
    46. };
    To copy to clipboard, switch view to plain text mode 

    DataManager.cpp


    Qt Code:
    1. DataManager::DataManager()
    2. {
    3. QTextStream out(stdout);
    4. out << "DataManager()\n";
    5.  
    6. setupModels();
    7. setupTableViews(); //set QTableViews to NULL
    8.  
    9. }
    10.  
    11. void DataManager::setupModels()
    12. {
    13.  
    14. QTextStream out(stdout);
    15. out << "setupModels()\n";
    16.  
    17. model_customer = new QStandardItemModel;
    18. model_customer->setObjectName("Customers");
    19.  
    20. model_articles = new QStandardItemModel;
    21. model_articles->setObjectName("Articles");
    22.  
    23. model_invoices = new QStandardItemModel;
    24. model_invoices->setObjectName("Invoices");
    25.  
    26. model_invoiceEntries = new QStandardItemModel;
    27. model_invoiceEntries->setObjectName("InvoiceEntries");
    28.  
    29. }
    30.  
    31.  
    32.  
    33. void DataManager::setupTableViews()
    34. {
    35. QTextStream out(stdout);
    36. out << "setupTableViews()\n";
    37.  
    38. //set to NULL, else the delete later will crash the app
    39. m_customerTableView = NULL;
    40. m_articlesTableView = NULL;
    41. m_invoicesTableView = NULL;
    42. m_invoiceEntriesTableView = NULL;
    43.  
    44. }
    45.  
    46. QStandardItemModel *DataManager::customerModel()
    47. {
    48. Q_ASSERT(model_customer);
    49. return model_customer;
    50. }
    51.  
    52. QStandardItemModel *DataManager::articlesModel()
    53. {
    54. Q_ASSERT(model_articles);
    55. return model_articles;
    56. }
    57.  
    58. QStandardItemModel *DataManager::invoicesModel()
    59. {
    60. Q_ASSERT(model_invoices);
    61. return model_invoices;
    62. }
    63.  
    64. QStandardItemModel *DataManager::invoiceEntriesModel()
    65. {
    66. Q_ASSERT(model_invoiceEntries);
    67. return model_invoiceEntries;
    68. }
    69.  
    70. QTableView *DataManager::customerTableView(QWidget* parent)
    71. {
    72.  
    73. DeleteCustomerTableView();
    74.  
    75. m_customerTableView = new QTableView(parent);
    76. m_customerTableView->setModel(model_customer);
    77. m_customerTableView->setGeometry(QRect(10, 10, 700, 400));
    78. m_customerTableView->setObjectName("CustomerTableView");;
    79. m_customerTableView->setSortingEnabled(true);
    80. m_customerTableView->sortByColumn(0, Qt::DescendingOrder);
    81. m_customerTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    82. m_customerTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    83. selectionModelCustomer = new QItemSelectionModel(model_customer);
    84. m_customerTableView->setSelectionModel(selectionModelCustomer);
    85. headerViewCustomer = m_customerTableView->horizontalHeader();
    86. return m_customerTableView;
    87. }
    88.  
    89.  
    90. QTableView *DataManager::articlesTableView(QWidget* parent)
    91. {
    92.  
    93. DeleteArticlesTableView();
    94.  
    95. m_articlesTableView = new QTableView(parent);
    96. m_articlesTableView->setModel(model_articles);
    97. m_articlesTableView->setGeometry(QRect(10, 10, 700, 400));
    98. m_articlesTableView->setObjectName("ArticlesTableView");;
    99. m_articlesTableView->setSortingEnabled(true);
    100. m_articlesTableView->sortByColumn(0, Qt::DescendingOrder);
    101. m_articlesTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    102. m_articlesTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    103. selectionModelArticles = new QItemSelectionModel(model_articles);
    104. m_articlesTableView->setSelectionModel(selectionModelArticles);
    105. headerViewArticles = m_articlesTableView->horizontalHeader();
    106. return m_articlesTableView;
    107. }
    108.  
    109. QTableView *DataManager::invoicesTableView(QWidget* parent)
    110. {
    111.  
    112. DeleteInvoicesTableView();
    113.  
    114. m_invoicesTableView = new QTableView(parent);
    115. m_invoicesTableView->setModel(model_invoices);
    116. m_invoicesTableView->setGeometry(QRect(10, 10, 700, 400));
    117. m_invoicesTableView->setObjectName("ArticlesTableView");;
    118. m_invoicesTableView->setSortingEnabled(true);
    119. m_invoicesTableView->sortByColumn(0, Qt::DescendingOrder);
    120. m_invoicesTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    121. m_invoicesTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    122. selectionModelInvoices = new QItemSelectionModel(model_invoices);
    123. m_invoicesTableView->setSelectionModel(selectionModelInvoices);
    124. headerViewInvoices = m_invoicesTableView->horizontalHeader();
    125. return m_invoicesTableView;
    126. }
    127.  
    128. QTableView *DataManager::invoiceEntriesTableView(QWidget* parent)
    129. {
    130. DeleteInvoiceEntriesTableView();
    131.  
    132. m_invoiceEntriesTableView = new QTableView(parent);
    133. m_invoiceEntriesTableView->setModel(model_invoiceEntries);
    134. m_invoiceEntriesTableView->setGeometry(QRect(10, 10, 700, 400));
    135. m_invoiceEntriesTableView->setObjectName("ArticlesTableView");;
    136. m_invoiceEntriesTableView->setSortingEnabled(true);
    137. m_invoiceEntriesTableView->sortByColumn(0, Qt::DescendingOrder);
    138. m_invoiceEntriesTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    139. m_invoiceEntriesTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    140. selectionModelInvoiceEntries = new QItemSelectionModel(model_invoiceEntries);
    141. m_invoiceEntriesTableView->setSelectionModel(selectionModelInvoiceEntries);
    142. headerViewInvoices = m_invoiceEntriesTableView->horizontalHeader();
    143. return m_invoiceEntriesTableView;
    144. }
    145.  
    146.  
    147.  
    148. //suggestion from forum, to avoid leaks
    149. QTableView *DataManager::DeleteCustomerTableView()
    150. {
    151. delete m_customerTableView;
    152. m_customerTableView = NULL;
    153. return m_customerTableView;
    154. }
    155.  
    156. QTableView *DataManager::DeleteArticlesTableView()
    157. {
    158. delete m_articlesTableView;
    159. m_articlesTableView = NULL;
    160. return m_articlesTableView;
    161. }
    162.  
    163. QTableView *DataManager::DeleteInvoicesTableView()
    164. {
    165. delete m_invoicesTableView;
    166. m_invoicesTableView = NULL;
    167. return m_invoicesTableView;
    168. }
    169.  
    170. QTableView *DataManager::DeleteInvoiceEntriesTableView()
    171. {
    172. delete m_invoiceEntriesTableView;
    173. m_invoiceEntriesTableView = NULL;
    174. return m_invoiceEntriesTableView;
    175. }
    To copy to clipboard, switch view to plain text mode 

  3. #23
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to correctly re-assign a pointer?

    AddressBook "has-a" QTableView, it should own it. DataManager should only reference it. So remove m_customerTableView from DataManager and move it to AddressBook.
    Create a new member function in DataManager called CustomerModel.
    If DataManager needs to operate on a QTableView then pass a pointer to the function.

    Qt Code:
    1. AddressBook::AddressBook(DataManager* dmgr, QWidget *parent): QDialog(parent)
    2. {
    3. m_customerTableView = new QTableView(this);
    4. m_customerTableView->setModel(dmgr->customerModel());
    5. }
    6.  
    7. DataManager::DataManager()
    8. {
    9. ...
    10. ...
    11. }
    12.  
    13. QAbstractItemMode * DataManager::customerModel(void)
    14. {
    15. return model_customer;
    16. }
    17.  
    18. void DataManager::someFunctionThatNeedsToWorkonQTableView(QTableView * tableView)
    19. {
    20. tableView->doWhatEverNeedsToBeDone();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Of course follow proper Qt naming conventions and code defensively.

  4. #24
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: How to correctly re-assign a pointer?

    Yes, I understand what you mean, but I like to keep the QTableView central in DataManager, because it is used by more components, not only by AddressBook, to provide a uniform interface where ever in the GUI a customer list is shown.
    I would prefer to fix the errors and completly understand why they happen to prevent them in the future.

  5. #25
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to correctly re-assign a pointer?

    Quote Originally Posted by homerun4711 View Post
    One minute to late
    Here is the complete code:
    Move all the table view code to the AddressBook. The views are owned by the dialog and will be automatically deleted when the dialog is dismissed. Headache gone, code simplified.

    Quote Originally Posted by homerun4711 View Post
    Yes, I understand what you mean, but I like to keep the QTableView central in DataManager, because it is used by more components, not only by AddressBook, to provide a uniform interface where ever in the GUI a customer list is shown.
    I would prefer to fix the errors and completly understand why they happen to prevent them in the future.
    Ask yourself these questions -
    Does DataManager have a tableView?
    Does AddressBook have a tableView?
    Who should be the owner?

    If DataManager does most of the work, then pass pointers of the tableView to the worker functions. In the furture, if you derive a class from QTableView, then overload the DataManager worker function with the derived classes.

    Qt Code:
    1. class MyTableView1 : public QTableView;
    2. class MyTableView2 : public QTableView;
    3. AddressBook::DoSomeWork()
    4. {
    5. dmgr->DoSomeWork(pMyTableView1);
    6. dmgr->DoSomeWork(pMyTableView2);
    7. }
    8.  
    9. void DataManager::DoSomeWork(MyTableView1 * pTableView);
    10. void DataManager::DoSomeWork(MyTableView2 * pTableView);
    To copy to clipboard, switch view to plain text mode 

    ps, and I see that address book is just one of many Dialog classes planned. Each of those classes will own their own table view and pass that to the data manager.
    Last edited by pkohut; 4th January 2011 at 16:09.

  6. The following user says thank you to pkohut for this useful post:

    homerun4711 (4th January 2011)

  7. #26
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Thumbs up Re: How to correctly re-assign a pointer?

    Does DataManager have a tableView?
    Does AddressBook have a tableView?
    Who should be the owner?
    If DataManager does most of the work, then pass pointers of the tableView to the worker functions. In the furture, if you derive a class from QTableView, then overload the DataManager worker function with the derived classes.
    Yes, you are right. I will do that in the future, well, maybe I will rewrite the application I am working on right now to work this way.

    Thanks a lot for your help and your suggestions!

  8. #27
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to correctly re-assign a pointer?

    Quote Originally Posted by homerun4711 View Post
    Yes, you are right. I will do that in the future, well, maybe I will rewrite the application I am working on right now to work this way.
    With any code, but Qt for sure, if you feel like you're fighting the code or framework you probably are. Always be open minded about refactoring a chunk of code, even if it's a 1000 lines and took a week to write. Especially when first learning a language or framework, because most of that learning is code thrashing and the what's written is probably fragile.

    Quote Originally Posted by homerun4711 View Post
    Thanks a lot for your help and your suggestions!
    Thank you as well, this provided me with an opportunity to explore the API differently than I might have, definitely sooner.

Similar Threads

  1. Cannot assign to pixmap during painting
    By fanatos in forum Qt Programming
    Replies: 9
    Last Post: 22nd February 2016, 17:56
  2. Replies: 1
    Last Post: 4th December 2010, 17:20
  3. Assign a wchar_t array to QString
    By jacky in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 12:28
  4. boost::assign
    By Sivert in forum General Programming
    Replies: 0
    Last Post: 2nd May 2007, 00:23
  5. I can't assign TableView signals...
    By tomek in forum Newbie
    Replies: 5
    Last Post: 9th January 2006, 21:04

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.