customers
::customers(QWidget *parent
) : m_ui(new Ui::customers)
{
m_ui->setupUi(this);
createMainTable();
populateMainTable();
}
void customers:: createMainTable()
{
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->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() {
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"));
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++) {
}
m_ui->customerView->setModel(model);
}
void customers
::changeEvent(QEvent *e
) {
switch (e->type()) {
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();
query.exec("INSERT INTO customer (LastName, FirstName, Category, Amount) VALUES('" + list[0] + "', '" + list[1] + "', '" +list[2] + "', '" + list[3] + "')");
populateMainTable();
}
}
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::SingleSelection);
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();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks