Hello!
I have problem with QTableView and QStandardItemModel
My header file:

Qt Code:
  1. #ifndef TABLEVIEW_H
  2. #define TABLEVIEW_H
  3.  
  4. #include <QtGui>
  5. #include <QWidget>
  6. #include <QTableView>
  7. #include <QStandardItemModel>
  8. #include <QStandardItem>
  9. #include <QString>
  10. #include <QVector>
  11. #include <QtSql/QSqlDatabase>
  12. #include <QtSql/QSqlError>
  13. #include <QtSql/QSqlQuery>
  14. #include <QtSql/QSQLiteDriver>
  15. #include <QMessageBox>
  16. #include <QDialog>
  17. #include "dialog.h"
  18.  
  19. class tableView : public QWidget
  20. {
  21. Q_OBJECT
  22. public:
  23. tableView(QWidget *parent = 0);
  24. void db_add(QString atr1, QString atr2);
  25.  
  26. private:
  27. QString sstr;
  28. QTableView *tblv;
  29. QLabel *label1, *label2;
  30. QLineEdit *lineeRow, *lineeCol;
  31. QPushButton *btnApply;
  32. int nrow, ncol;
  33. QVector<QString> table;
  34. int db_connect();
  35. void btnApply_clicked();
  36. QString asda;
  37. int a;
  38. private slots:
  39. void dial();
  40. };
  41.  
  42. #endif // TABLEVIEW_H
To copy to clipboard, switch view to plain text mode 

source file:

Qt Code:
  1. #include "tableview.h"
  2. #include <QDebug>
  3.  
  4. tableView::tableView(QWidget *parent)
  5. : QWidget(parent)
  6. {
  7.  
  8. //create layout
  9. QVBoxLayout *mainLayout = new QVBoxLayout();
  10. QHBoxLayout *horLayout1 = new QHBoxLayout;
  11. QHBoxLayout *horLayout2 = new QHBoxLayout;
  12.  
  13. btnApply = new QPushButton(tr("Apply"));
  14.  
  15.  
  16. //create QTableView
  17. tblv = new QTableView();
  18. tblv->setSelectionBehavior(QAbstractItemView::SelectItems );
  19. tblv->setSelectionMode( QAbstractItemView::ExtendedSelection );
  20. //setting layout
  21. mainLayout->addLayout(horLayout1);
  22. mainLayout->addLayout(horLayout2);
  23. mainLayout->addWidget(btnApply);
  24. mainLayout->addWidget(tblv);
  25. setLayout(mainLayout);
  26. connect(btnApply, SIGNAL(clicked()), this, SLOT(dial()));
  27. btnApply_clicked();
  28.  
  29. }
  30. void tableView::dial()
  31. {
  32. Dialog dial;
  33. dial.show();
  34. dial.exec();
  35. }
  36.  
  37. int tableView::db_connect()
  38. {
  39. if(!db.isOpen())
  40. {
  41. db = QSqlDatabase::addDatabase("QSQLITE");
  42. db.setDatabaseName("./baza.db");
  43. if (!db.open())
  44. {
  45. QMessageBox::critical(0, qApp->tr("Nie udało się nawiązać połączenia z bazą danych!"),
  46. qApp->tr("Naciśnij Cancel aby zakończyć."), QMessageBox::Cancel);
  47. }
  48. return 1;
  49. }
  50. return 0;
  51. }
  52.  
  53. void tableView::db_add(QString atr1, QString atr2)
  54. {
  55. QSqlQuery query;
  56. QString asda;
  57. asda = QString("insert into person values(null, '");
  58. asda += atr1;
  59. asda += "', '";
  60. asda += atr2;
  61. asda += "')";
  62. qDebug() << asda;
  63. query.exec(asda);
  64. if(query.lastError().isValid())
  65. qDebug() << query.lastError();
  66. qDebug() << "1";
  67. item = new QStandardItem(query.lastInsertId().toString());
  68. qDebug() << "2";
  69. model->setItem(a, 0, item);
  70. qDebug() << "3";
  71. item = new QStandardItem(atr1);
  72. qDebug() << "4";
  73. model->setItem(a, 1, item);
  74. qDebug() << "5";
  75. item = new QStandardItem(atr2);
  76. qDebug() << "6";
  77. model->setItem(a, 2, item);
  78. }
  79.  
  80. void tableView::btnApply_clicked()
  81. {
  82. if(db_connect()==1)
  83. {
  84. //get number of input row and column
  85. nrow = 2;
  86. ncol = 3;
  87.  
  88. //create model
  89. model = new QStandardItemModel( nrow, ncol, this );
  90.  
  91. //create QTableview Horizontal Header
  92. // model->setHorizontalHeaderItem( r, new QStandardItem( QString("Column_ %0" ).arg(r+1)) );
  93. model->setHorizontalHeaderItem(0, new QStandardItem(QString("Lp.")));
  94. model->setHorizontalHeaderItem(1, new QStandardItem(QString("Nazwa")));
  95. model->setHorizontalHeaderItem(2, new QStandardItem(QString("Haslo")));
  96. QSqlQuery query;
  97.  
  98. a = 0;
  99. query.exec("SELECT * from person");
  100. while(query.next())
  101. {
  102. for(int x=0; x<3; x++)
  103. {
  104. sstr = query.value(x).toString();
  105.  
  106. item = new QStandardItem(sstr);
  107. model->setItem(a, x, item);
  108. }
  109. a++;
  110. }
  111. //set model
  112. tblv->setModel(model);
  113.  
  114. tblv->resizeRowsToContents();
  115. tblv->setFixedSize(800,600);
  116. tblv->setColumnWidth(2,350);
  117. tblv->setColumnWidth(1,350);
  118. tblv->setColumnWidth(0,50);
  119. tblv->scrollToBottom();
  120. }
  121. }
To copy to clipboard, switch view to plain text mode 

in btnApply_Clicked() I am generating a table on base sqlite data.
in Dialog window I am giving arguments and calling up db_add function, then the arguments get into sqlite database and then I want to add this arguments to next rows in the QTableView, but when I run the program I get error of memory, and information in qt creator "exited with code -1073741819" when it get to item = new QStandardItem(query.lastInsertId().toString()). I tried give other argument (item = new QStandardItem(QString("test")) but it didn't help.
I would be grateful for your help, and sory for my bad english.
Janusz