-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdateorders.cpp
More file actions
69 lines (58 loc) · 2.29 KB
/
updateorders.cpp
File metadata and controls
69 lines (58 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "updateorders.h"
#include "ui_updateorders.h"
#include <QApplication>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDatabase>
#include <QDate>
#include <QDebug>
UpdateOrders::UpdateOrders(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::UpdateOrders)
, tableView(new QTableView(this)),
model(new QSqlTableModel(this))
{
ui->setupUi(this);
// Set up the QTableView
model->setTable("orders");
model->setEditStrategy(QSqlTableModel::OnManualSubmit); // Manual commit
model->select(); // Load data lazily
tableView->setModel(model);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->table_horizontalLayout->addWidget(tableView);
// Adjust column sizes based on the content
tableView->resizeColumnsToContents();
// Set a minimum width for each column to ensure the table looks good
for (int column = 0; column < model->columnCount(); ++column) {
// Set a minimum width for the column, you can adjust this value as needed
tableView->setColumnWidth(column, qMax(tableView->columnWidth(column), 100)); // Minimum width is 100px
}
}
UpdateOrders::~UpdateOrders()
{
delete ui;
}
void UpdateOrders::on_Update_btn_clicked()
{
// Commit the changes made in the table back to the database
if (model->submitAll()) {
QMessageBox::information(this, "Success", "Product(s) updated successfully.");
refreshTable(); // Refresh the table view to reflect the changes
} else {
QMessageBox::critical(this, "Error", "Failed to update product(s).");
}
}
void UpdateOrders::refreshTable(){
model->select(); // Reload data from the database into the model
tableView->resizeColumnsToContents(); // Adjust column sizes based on content
// Adjust column sizes based on the content
tableView->resizeColumnsToContents();
// Set a minimum width for each column to ensure the table looks good
for (int column = 0; column < model->columnCount(); ++column) {
// Set a minimum width for the column, you can adjust this value as needed
tableView->setColumnWidth(column, qMax(tableView->columnWidth(column), 100)); // Minimum width is 100px
}
}