Results 1 to 7 of 7

Thread: Error when trying to update TableView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23

    Default Re: Error when trying to update TableView

    Ah, I think I understand what you mean, I need to define model and channelsView as a global pointer maybe? or something else?

  2. #2
    Join Date
    Nov 2007
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Error when trying to update TableView

    Quote Originally Posted by Ferric View Post
    Ah, I think I understand what you mean, I need to define model and channelsView as a global pointer maybe? or something else?
    Yes. I think you should declare model and channelsView as class member.

    Qt Code:
    1. // Loader.h
    2. class Loader {
    3. Q_OBJECT
    4. public:
    5. Loader::Loader(QWidget *parent = 0);
    6.  
    7. // ...
    8.  
    9. private:
    10. QTableView *channelsView; // pointer data member
    11. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // Loader.cpp
    2. Loader::Loader(QWidget *parent)
    3. : QWidget(parent), channelsView(0), model(0) // initialization.
    4. {
    5.  
    6.  
    7. channelsView = new QTableView(); // constraction
    8. model = new QStandardItemModel(4,2);
    9.  
    10. // ....
    11.  
    12. };
    13.  
    14. void Loader::addChannelToTable() // I want this SLOT to be activated when the addButton (from the AddDialog::dialog object) is clicked.
    15. {
    16. QMessageBox::information(
    17. this,
    18. tr("Yes,"),
    19. tr("It works.") );
    20.  
    21. if (!channelsView || !model) { // check whether pointers are not 0.
    22. return;
    23. }
    24.  
    25. for (int row = 0; row < 4; ++row) {
    26. for (int column = 0; column < 2; ++column) {
    27. QStandardItem *item = new QStandardItem(QString("%0, %1").arg(row).arg(column));
    28. model->setItem(row, column, item); //This item is put in the model by using the setItem(int, int, QStandardItem*) method.
    29. }
    30. } // I think my problem is in the line above or below.
    31.  
    32. //channelsView->setModel(model); // This is not necessary, because setModel() has been called in Loader().
    33. // channelsView has already referred to the model.
    34. }
    To copy to clipboard, switch view to plain text mode 
    kichi

  3. The following user says thank you to kichi for this useful post:

    Ferric (12th January 2010)

  4. #3
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23

    Default Re: Error when trying to update TableView

    Thanks a lot, it works now, if you have time could you please tell me the reasoning behind the following lines of code:

    [code]
    if (!channelsView || !model) { // check whether pointers are not 0.
    return;
    }

    Thanks

  5. #4
    Join Date
    Nov 2007
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Error when trying to update TableView

    Quote Originally Posted by Ferric View Post
    Thanks a lot, it works now, if you have time could you please tell me the reasoning behind the following lines of code:

    [code]
    if (!channelsView || !model) { // check whether pointers are not 0.
    return;
    }

    I apologize you annoying.
    In this case, the code is not necessary.

    This check is necessary, in the following case.
    • Always initialize the pointer with 0, before using the pointer.
    • Always assign 0 to the pointer which is referring to nothing.
    • Make the pointer refer to object with "new", when it becomes necessary.
    • Delete the pointer to object, when it becomes unnecessary, and assign 0 to the pointer.
    • "new" may throw std::bad_alloc when the memory becomes insufficient. so always use "try" keyword, and "catch" the exception. In particular, never throw any exceptions out of constractor.
    • Always check the pointer whether it is not 0, before which is used.


    If you don't need std::bad_exception, you can write the following code.

    Qt Code:
    1. QTableView *channelsView = new(nothrow) QTableView();
    To copy to clipboard, switch view to plain text mode 

    "new(nothrow)" does not throw std::bad_exec, and if there is not enough memory, it returns 0.
    but I have rarely seen this using up to now, on the Web.

    I'm sorry in poor English.
    kichi

  6. #5
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    23

    Default Re: Error when trying to update TableView

    Ah I think I understand what you are saying, Thanks once again : ]

Similar Threads

  1. tableview
    By GuL in forum Newbie
    Replies: 1
    Last Post: 26th August 2008, 17:18
  2. TableView and QDataWidgetMapper
    By kramed in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2008, 04:41
  3. tableView Problem
    By hrcariaga in forum Newbie
    Replies: 5
    Last Post: 7th February 2008, 08:29
  4. help in tableview
    By bala in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 15:46
  5. tableview and layouts
    By zorro68 in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2007, 17:21

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
  •  
Qt is a trademark of The Qt Company.