Problem with adding rows into QTableView
Hello!
I have problem with QTableView and QStandardItemModel
My header file:
Code:
#ifndef TABLEVIEW_H
#define TABLEVIEW_H
#include <QtGui>
#include <QWidget>
#include <QTableView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QString>
#include <QVector>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSQLiteDriver>
#include <QMessageBox>
#include <QDialog>
#include "dialog.h"
{
Q_OBJECT
public:
private:
int nrow, ncol;
QVector<QString> table;
int db_connect();
void btnApply_clicked();
int a;
private slots:
void dial();
};
#endif // TABLEVIEW_H
source file:
Code:
#include "tableview.h"
#include <QDebug>
tableView
::tableView(QWidget *parent
){
//create layout
//create QTableView
//setting layout
mainLayout->addLayout(horLayout1);
mainLayout->addLayout(horLayout2);
mainLayout->addWidget(btnApply);
mainLayout->addWidget(tblv);
setLayout(mainLayout);
connect(btnApply, SIGNAL(clicked()), this, SLOT(dial()));
btnApply_clicked();
}
void tableView::dial()
{
Dialog dial;
dial.show();
dial.exec();
}
int tableView::db_connect()
{
if(!db.isOpen())
{
db.setDatabaseName("./baza.db");
if (!db.open())
{
QMessageBox::critical(0, qApp
->tr
("Nie udało się nawiązać połączenia z bazą danych!"),
qApp
->tr
("Naciśnij Cancel aby zakończyć."),
QMessageBox::Cancel);
}
return 1;
}
return 0;
}
{
asda
= QString("insert into person values(null, '");
asda += atr1;
asda += "', '";
asda += atr2;
asda += "')";
qDebug() << asda;
query.exec(asda);
if(query.lastError().isValid())
qDebug() << query.lastError();
qDebug() << "1";
qDebug() << "2";
model->setItem(a, 0, item);
qDebug() << "3";
qDebug() << "4";
model->setItem(a, 1, item);
qDebug() << "5";
qDebug() << "6";
model->setItem(a, 2, item);
}
void tableView::btnApply_clicked()
{
if(db_connect()==1)
{
//get number of input row and column
nrow = 2;
ncol = 3;
//create model
//create QTableview Horizontal Header
// model->setHorizontalHeaderItem( r, new QStandardItem( QString("Column_ %0" ).arg(r+1)) );
a = 0;
query.exec("SELECT * from person");
while(query.next())
{
for(int x=0; x<3; x++)
{
sstr = query.value(x).toString();
model->setItem(a, x, item);
}
a++;
}
//set model
tblv->setModel(model);
tblv->resizeRowsToContents();
tblv->setFixedSize(800,600);
tblv->setColumnWidth(2,350);
tblv->setColumnWidth(1,350);
tblv->setColumnWidth(0,50);
tblv->scrollToBottom();
}
}
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
Re: Problem with adding rows into QTableView
You are missing this statment in the tableView ctor
Re: Problem with adding rows into QTableView
but
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.
Re: Problem with adding rows into QTableView
you might be calling db_add before btnApply_clicked
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:
Code:
void Dialog::on_pushButton_clicked()
{
text1 = ui->lineEdit->text();
text2 = ui->lineEdit_2->text();
if(!text1.isEmpty() && !text2.isEmpty())
{
tableView *table;
table->db_add(text1,text2);
}
this->close();
}
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?
Code:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QString>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
Code:
#include "dialog.h"
#include "ui_dialog.h"
#include "tableview.h"
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_2_clicked()
{
this->close();
}
void Dialog::on_pushButton_clicked()
{
text1 = ui->lineEdit->text();
text2 = ui->lineEdit_2->text();
if(!text1.isEmpty() && !text2.isEmpty())
{
tableView *table;
table->db_add(text1,text2);
}
this->close();
}
i get in "Build Issues" "table may be used uninitialized in this function
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:
Code:
Dialog dialog = new Dialog();
dialog->show();
dialog->exec();
connect(dialog->ui->pushButton, SIGNAL(clicked()),this,on_pushButton_clicked()); // <- there i have error
Re: Problem with adding rows into QTableView
Look like you forgot the SLOT keyword:
Code:
connect(dialog->ui->pushButton, SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));
Re: Problem with adding rows into QTableView
Code:
Dialog dialog = new Dialog();
dialog->show();
connect(dialog->ui->pushButton, SIGNAL(clicked()),this,SLOT(on_pushButton_clicked())); //you missed SLOT keyword, and also you should connect before calling exec()
dialog->exec();
Re: Problem with adding rows into QTableView
Thank you very much, now everything is working.