Results 1 to 7 of 7

Thread: Error when trying to update TableView

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

    Default Error when trying to update TableView

    Hi,

    I am new to Qt programming and Im having problems trying to insert new data into a QTableView, via a QStandardItemModel,

    Specifically, I am having trouble trying to update the table from within a function "void Loader::addChannelToTable() "

    Qt Code:
    1. #include <QtGui>
    2. #include "loader.h"
    3. #include <QString>
    4. #include <QTextStream>
    5. #include "qextserialport.h"
    6. #include "addDialog.h"
    7. #include <QtGui/QApplication>
    8. #include <QCoreApplication>
    9. #include <QString>
    10. #include <QTextStream>
    11. #include <QThread>
    12. #include <QTime>
    13. #include <QtGlobal>
    14. #include <qdatetime.h>
    15. #include <QByteArray>
    16. #include <QBitArray> // probably shouldnt use as RS232 works with bytes.
    17. #include <QStandardItem>
    18. #include <QStandardItemModel>
    19. #include <QVariant>
    20. #include <QModelIndex>
    21.  
    22.  
    23. Loader::Loader(QWidget *parent)
    24. : QWidget(parent)
    25. {
    26.  
    27.  
    28. QTableView *channelsView = new QTableView();
    29.  
    30. // for (int row = 0; row < 4; ++row) {
    31. // for (int column = 0; column < 2; ++column) {
    32. // QStandardItem *item = new QStandardItem(QString("%0, %1").arg(row).arg(column));
    33. // //QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    34. // model->setItem(row, column, item); //This item is put in the model by using the setItem(int, int, QStandardItem*) method.
    35. // }
    36. // } //When I uncomment this section the Table is updated
    37.  
    38. channelsView->setModel(model);
    39.  
    40.  
    41. QLabel *titleLabel = new QLabel(tr("COMMUNICATIONS"));
    42. QLabel *descriptionLabel = new QLabel(tr("Determines Distribution"));
    43. QLabel *netNameLabel = new QLabel(tr("Net Name:"));
    44. QLabel *currentChannelsLabel = new QLabel(tr("Descriptionfor Net:"));
    45. QLabel *cloningLabel = new QLabel(tr("Enable Cloning"));
    46. netNameLine = new QLineEdit;
    47. currentChannelsText = new QTextEdit;
    48. QPushButton *saveButton = new QPushButton(tr("Save Configuration Data As..."));
    49. QPushButton *uploadButton = new QPushButton(tr("Upload Configuration "));
    50. QPushButton *quitButton = new QPushButton(tr("Quit"));
    51. QPushButton *addChannelButton = new QPushButton(tr("Add Channel")); // the 'tr' is for translate...
    52. QPushButton *removeChannelButton = new QPushButton(tr("Remove Channel"));
    53. QCheckBox *cloningEnabled = new QCheckBox();
    54. QHBoxLayout *secondLayout = new QHBoxLayout();
    55. secondLayout->addWidget(saveButton);
    56. secondLayout->addWidget(uploadButton);
    57. secondLayout->addWidget(quitButton);
    58.  
    59. QHBoxLayout *thirdLayout = new QHBoxLayout();
    60. thirdLayout->addWidget(cloningLabel);
    61. thirdLayout->addWidget(cloningEnabled);
    62.  
    63. QGridLayout *mainLayout = new QGridLayout;
    64. mainLayout->addWidget(titleLabel, 0, 1); //The title widget (WInDows GadgET) is situated at row 0, column 1, in the grid layout.
    65. mainLayout->addWidget(descriptionLabel, 1, 1, Qt::AlignTop);
    66. mainLayout->addWidget(netNameLabel, 4, 0);
    67. mainLayout->addWidget(netNameLine, 4, 1);
    68. mainLayout->addLayout(thirdLayout,5,1);
    69. mainLayout->addWidget(addChannelButton, 6, 1);
    70. mainLayout->addWidget(removeChannelButton, 7, 1);
    71. mainLayout->addWidget(channelsView, 8, 1);
    72. mainLayout->addWidget(currentChannelsLabel, 8, 0, Qt::AlignTop);
    73. mainLayout->addLayout(secondLayout,9,1);
    74. setLayout(mainLayout);
    75. setWindowTitle(tr("Unit Loader"));
    76.  
    77. QObject::connect(quitButton, SIGNAL(clicked()),
    78. this, SLOT(close()));
    79.  
    80. QObject::connect(uploadButton, SIGNAL(clicked()),
    81. this, SLOT(calculateRandomCode()));
    82.  
    83. QObject::connect(uploadButton, SIGNAL(clicked()),
    84. this, SLOT(send()));
    85.  
    86. QObject::connect(addChannelButton, SIGNAL(clicked()), // So I click the addChannel button and the addChannel Dialog opens
    87. this, SLOT(addChannelDialog())); // So I click the addChannel button and the addChannel Dialog opens
    88. }
    89.  
    90.  
    91. void Loader::addChannelDialog() // the addChannelDialog SLOT or Function..
    92. {
    93. AddDialog *dialog = new AddDialog(this); // Creates a new object; *dialog of class AddDialog
    94. dialog->show(); //Shows the dialog..
    95.  
    96. QObject::connect(dialog, SIGNAL(addThisChannel()),
    97. this, SLOT(addChannelToTable()));
    98. }
    99.  
    100. void Loader::addChannelToTable() // I want this SLOT to be activated when the addButton (from the AddDialog::dialog object) is clicked.
    101.  
    102. {
    103. QMessageBox::information(
    104. this,
    105. tr("Yes,"),
    106. tr("It works.") );
    107.  
    108. for (int row = 0; row < 4; ++row) {
    109. for (int column = 0; column < 2; ++column) {
    110. QStandardItem *item = new QStandardItem(QString("%0, %1").arg(row).arg(column));
    111. model->setItem(row, column, item); //This item is put in the model by using the setItem(int, int, QStandardItem*) method.
    112. }
    113. } // I think my problem is in the line above or below.
    114.  
    115. channelsView->setModel(model);
    116. }
    117.  
    118.  
    119.  
    120. void Loader::send()
    121.  
    122.  
    123. {
    124. }
    125.  
    126.  
    127.  
    128. void Loader::calculateRandomCode()
    129. {
    130.  
    131. }
    To copy to clipboard, switch view to plain text mode 


    My code compiles and runs, but as soon as I get to the part where I can run "void Loader::addChannelToTable() " the information box "yes it works" is displayed, then windows shows and error screen and I am forced to close my program.


    When I update the table from within the

    Qt Code:
    1. Loader::Loader(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. //
    5. }
    To copy to clipboard, switch view to plain text mode 
    section it works fine.



    Thanks for reading, any help would be greatly appreciated
    Last edited by Ferric; 12th January 2010 at 04:55.

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

    Default Re: Error when trying to update TableView

    In addChannelToTable(), you are using variables model and channelsView.
    but in Loader(), you are defined model and channelsView as local pointer.

    I'm sorry in poor English.
    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
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    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?

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

    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

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

    Ferric (12th January 2010)

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

    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

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

    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

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.