Results 1 to 9 of 9

Thread: Problem with adding rows into QTableView

  1. #1
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with adding rows into QTableView

    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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem with adding rows into QTableView

    You are missing this statment in the tableView ctor
    Qt Code:
    1. model = new QStandardItemModel(this);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with adding rows into QTableView

    but
    Qt Code:
    1. model = new QStandardItemModel(this);
    To copy to clipboard, switch view to plain text mode 
    is initiated in function btnApply_clicked. If it is about changing (nrow, ncol, this) into (this) then i tried to do that and it didn't help. program is crashing when is in function db_add, and get to any of function in model.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem with adding rows into QTableView

    you might be calling db_add before btnApply_clicked

  5. #5
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with adding rows into QTableView

    btnApply_clicked is calling when program is starting, and db_add when i press the button, another window show then i press the button on that window:
    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. {
    3. text1 = ui->lineEdit->text();
    4. text2 = ui->lineEdit_2->text();
    5. if(!text1.isEmpty() && !text2.isEmpty())
    6. {
    7. tableView *table;
    8. table->db_add(text1,text2);
    9. }
    10. this->close();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I just check, and if i am calling function db_add in btnApply_clicked() then its work ok, so maybe i got error in dialog class?
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QString>
    6.  
    7. namespace Ui {
    8. class Dialog;
    9. }
    10.  
    11. class Dialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Dialog(QWidget *parent = 0);
    17. ~Dialog();
    18.  
    19. private slots:
    20.  
    21. void on_pushButton_2_clicked();
    22.  
    23. void on_pushButton_clicked();
    24.  
    25. private:
    26. Ui::Dialog *ui;
    27. QString text1;
    28. QString text2;
    29. };
    30.  
    31. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "tableview.h"
    4.  
    5. Dialog::Dialog(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::Dialog)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. Dialog::~Dialog()
    13. {
    14. delete ui;
    15. }
    16.  
    17.  
    18. void Dialog::on_pushButton_2_clicked()
    19. {
    20. this->close();
    21. }
    22.  
    23. void Dialog::on_pushButton_clicked()
    24. {
    25. text1 = ui->lineEdit->text();
    26. text2 = ui->lineEdit_2->text();
    27. if(!text1.isEmpty() && !text2.isEmpty())
    28. {
    29. tableView *table;
    30. table->db_add(text1,text2);
    31. }
    32. this->close();
    33. }
    To copy to clipboard, switch view to plain text mode 
    i get in "Build Issues" "table may be used uninitialized in this function
    Last edited by januszmk; 13th July 2011 at 20:24.

  6. #6
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with adding rows into QTableView

    I maybe found solution, but don't know how to do it. Could someone tell me, how can I connect to signal clicked() on button from ui from other class? because even if I declare ui as public, i can't get to that button from tableView class:
    Qt Code:
    1. Dialog dialog = new Dialog();
    2. dialog->show();
    3. dialog->exec();
    4. connect(dialog->ui->pushButton, SIGNAL(clicked()),this,on_pushButton_clicked()); // <- there i have error
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with adding rows into QTableView

    Look like you forgot the SLOT keyword:
    Qt Code:
    1. connect(dialog->ui->pushButton, SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem with adding rows into QTableView

    Qt Code:
    1. Dialog dialog = new Dialog();
    2. dialog->show();
    3. connect(dialog->ui->pushButton, SIGNAL(clicked()),this,SLOT(on_pushButton_clicked())); //you missed SLOT keyword, and also you should connect before calling exec()
    4. dialog->exec();
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jul 2011
    Location
    Kraków / Poland
    Posts
    32
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with adding rows into QTableView

    Thank you very much, now everything is working.

Similar Threads

  1. Replies: 1
    Last Post: 21st March 2011, 19:58
  2. Weird problem resizing rows and columns in qtableview
    By Tiansen in forum Qt Programming
    Replies: 1
    Last Post: 27th December 2010, 14:26
  3. Adding rows to Tableview
    By codeman in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2009, 10:27
  4. adding rows to tablewidget
    By reshma in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 13:40
  5. Replies: 2
    Last Post: 12th June 2007, 17:23

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.