Results 1 to 3 of 3

Thread: QTableView QTablemodel & insertRow

  1. #1
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView QTablemodel & insertRow

    Hi,

    I want to show Data from a Relational DB in a QTableView.
    This work fine, also the editing about OnManualSubmit works. Now I want to add and delete Rows in the QTableView. When I tray to add a row an error occurs:

    Segmentation fault (core dumped)
    *** Exited with status: 139 ***

    What does this error means?

    I think its a memory lack but I don't know what to do?
    I work on Ubuntu Feisty, QT 4.2.3, KDevelop 3.4

    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QDebug>
    4. #include "tablemodel.h"
    5.  
    6. tableModel::tableModel()
    7. {
    8.  
    9. QPushButton *submitPb = new QPushButton(QObject::trUtf8("Änderungen speichern"));
    10. QPushButton *revertPb = new QPushButton(QObject::trUtf8("Änderungen verwerfen"));
    11. QPushButton *addPb = new QPushButton(QObject::trUtf8("Neue Person"));
    12. QPushButton *deletePb = new QPushButton(QObject::trUtf8("Lösche Person"));
    13.  
    14. model->setTable("tx_submission_data");
    15. model->setRelation(28,QSqlRelation("tx_submission_epterm","uid", "epterm"));
    16. model->setRelation(29,QSqlRelation("tx_submission_status","uid", "status"));
    17. model->setRelation(30,QSqlRelation("tx_submission_status","uid", "status"));
    18. model->setRelation(31,QSqlRelation("tx_submission_status","uid", "status"));
    19. model->setRelation(32,QSqlRelation("tx_submission_status","uid", "status"));
    20. model->setRelation(33,QSqlRelation("tx_submission_status","uid", "status"));
    21. model->setRelation(34,QSqlRelation("tx_submission_status","uid", "status"));
    22. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    23.  
    24.  
    25. model->removeColumns(1,7);
    26. model->removeColumn(5);
    27. model->removeColumn(6);
    28. model->removeColumn(6);
    29. model->removeColumn(6);
    30. model->removeColumn(7);
    31. model->removeColumn(7);
    32. model->removeColumn(7);
    33. model->removeColumn(7);
    34. model->removeColumn(7);
    35. model->removeColumn(7);
    36. model->removeColumn(7);
    37. model->removeColumn(8);
    38. model->removeColumn(11);
    39.  
    40. model->select();
    41.  
    42. QTableView *viewTable = new QTableView;
    43. viewTable->setModel(model);
    44. viewTable->setItemDelegate(new QSqlRelationalDelegate(this));
    45. viewTable->setSortingEnabled(TRUE);
    46. viewTable->setAlternatingRowColors(TRUE);
    47. //viewTable->hideColumn(0);
    48. viewTable->setSelectionMode(QAbstractItemView::SingleSelection);
    49. viewTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    50. viewTable->resizeColumnsToContents();
    51.  
    52. connect(submitPb, SIGNAL(clicked(bool)), model, SLOT(submitAll()) );
    53. connect(revertPb, SIGNAL(clicked(bool)), model, SLOT(revertAll() ) );
    54.  
    55. connect(addPb, SIGNAL(clicked(bool)), this, SLOT(addPerson() ) );
    56. connect(deletePb, SIGNAL(clicked(bool)), this, SLOT(deletePerson() ) );
    57.  
    58. QGridLayout *lay = new QGridLayout(this);
    59. lay->addWidget(viewTable, 0, 0, 1, 4);
    60. lay->addWidget(submitPb, 1, 0);
    61. lay->addWidget(revertPb, 1, 1);
    62. lay->addWidget(addPb, 1, 2);
    63. lay->addWidget(deletePb, 1, 3);
    64.  
    65. // CSS
    66. qApp->setStyleSheet("QTableView {background-color: #eeeeee; background-image: url(:/bg.png); color: blue;}");
    67. }
    68.  
    69. void tableModel::addPerson()
    70. {
    71. int row = model->rowCount();
    72. model->insertRow(row);
    73. QModelIndex index = model->index(row, Person_Name);
    74. viewTable->setCurrentIndex(index);
    75. viewTable->edit(index);
    76.  
    77. QMessageBox::warning(this, trUtf8("Person hinzufügen"), trUtf8("Sie müssen alle Felder ausfüllen!") );
    78. }
    79.  
    80. void tableModel::deletePerson()
    81. {
    82.  
    83. }
    84.  
    85.  
    86.  
    87. tableModel::~tableModel()
    88. {
    89.  
    90. }
    To copy to clipboard, switch view to plain text mode 

    Person_Name = 1 (Column with persons name)

  2. #2
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView QTablemodel & insertRow

    Could you not just do :

    Qt Code:
    1. int row = model->rowCount();
    2. QModelIndex index = model->index(row, Person_Name);
    3. beginInsertRows(index, row, row );
    4.  
    5. // insert your data
    6.  
    7. endInsertRows();
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Steve

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

    pfusterschmied (5th June 2007)

  4. #3
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView QTablemodel & insertRow

    Found solution:

    Qt Code:
    1. QTableView *viewTable = new QTableView;
    To copy to clipboard, switch view to plain text mode 
    doesn't work but:
    Qt Code:
    1. viewTable = new QTableView;
    To copy to clipboard, switch view to plain text mode 
    because I defined this in header...

    Thanks for the Code steg90 this is a good way too.

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.