PDA

View Full Version : problem in population QStandardItemModel from Mysql database



roncriss
7th December 2009, 04:40
Hi guys ,
I tried to populate QStandardItemModel from Mysql database,but when i view the Tableview only the last low from the database is displayed in the tableview.
where did i go wrong?

Here are the codes related to my problem,


customers::customers(QWidget *parent) :
QWidget(parent),
m_ui(new Ui::customers)
{
m_ui->setupUi(this);
createMainTable();
populateMainTable();
}
void customers:: createMainTable()
{

QStandardItemModel *model = new QStandardItemModel(0, 5);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("First Name"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Category"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Total Amount"));

m_ui->customerView->setModel(model);
m_ui->customerView->setSelectionBehavior(QAbstractItemView::SelectRows );
m_ui->customerView->setSelectionMode(QAbstractItemView::SingleSelectio n);
m_ui->customerView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;

m_ui->customerView->setColumnWidth(0, 50);
m_ui->customerView->setColumnWidth(1, 180);
m_ui->customerView->setColumnWidth(2, 180);
m_ui->customerView->setColumnWidth(3, 90);
m_ui->customerView->setColumnWidth(4, 90);


m_ui->customerView->verticalHeader()->hide();

//connect(m_ui->customerView->, SIGNAL(clicked(QModelIndex)), this, SLOT(changeClientInfos(QModelIndex)));

}
void customers::populateMainTable() {

QStandardItemModel *model = new QStandardItemModel(0, 5);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("First Name"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("Category"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("Total Amount"));

QSqlQuery query;
query.exec(QString("SELECT id,LastName, FirstName, Category,Amount FROM customer"));
while (query.next()) {

db_id = query.value(0).toString();
db_LastName = query.value(1).toString();
db_FirstName = query.value(2).toString();
db_Category = query.value(3).toString();
db_Amount = query.value(4).toString();

}
int no_cus = query.exec("SELECT COUNT(*) FROM customer");
for (int i = 0; i < no_cus; i++) {

model->setItem(i, 0, new QStandardItem(db_id));
model->setItem(i, 1, new QStandardItem(db_LastName));
model->setItem(i, 2, new QStandardItem(db_FirstName));
model->setItem(i, 3, new QStandardItem(db_Category));
model->setItem(i, 4, new QStandardItem(db_Amount));

}

m_ui->customerView->setModel(model);


}

void customers::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}

/* Get data from a form and insert into database */

void customers::on_newCustomer_clicked()
{
dialog = new newCustomer(this);
int accepted = dialog->exec();
if(accepted == 1 )
{

QList<QString> list = dialog->addCustomer();

QSqlQuery query;
query.exec("INSERT INTO customer (LastName, FirstName, Category, Amount) VALUES('" + list[0] + "', '" + list[1] + "', '" +list[2] + "', '" + list[3] + "')");
populateMainTable();
}
}

spirit
7th December 2009, 08:27
did you try to use QSqlTableModel/QSqlQueryModel?

JD2000
7th December 2009, 15:15
You would be better off using one of the prebuilt SQL models, but I think your problem is that you have created your QStandardItemModel to contain no rows!

Try changing the zero to something else or using the appendRow() method.