@@ -21,9 +21,7 @@ BrowseWidget::BrowseWidget(QWidget *parent) :
21
21
BrowseWidget::~BrowseWidget ()
22
22
{
23
23
// Delete the model
24
- if (m_model) {
25
- delete m_model;
26
- }
24
+ if (m_model) delete m_model;
27
25
28
26
// Delete the user interface
29
27
delete ui;
@@ -59,9 +57,6 @@ bool BrowseWidget::setTable(QString table)
59
57
ui->filterEdit ->setEnabled (true );
60
58
ui->filterButton ->setEnabled (true );
61
59
62
- // Make sure we save changes when moving to a new row (this is necessary so "Add" works)
63
- connect (ui->tableView ->selectionModel (), SIGNAL (currentChanged (QModelIndex,QModelIndex)), this , SLOT (commitChanges ()));
64
-
65
60
return true ;
66
61
}
67
62
@@ -94,15 +89,27 @@ void BrowseWidget::on_addButton_clicked()
94
89
95
90
// Insert the row
96
91
m_model->insertRecord (-1 , newRecord);
92
+
93
+ // Select the newly inserted row
94
+ ui->tableView ->selectRow (m_model->rowCount ()-1 );
95
+
96
+ // Make sure we save changes when moving to a new row (this is necessary so "Add" works)
97
+ connect (ui->tableView ->selectionModel (), SIGNAL (currentChanged (QModelIndex,QModelIndex)), this , SLOT (currentChanged (QModelIndex,QModelIndex)));
97
98
}
98
99
99
100
void BrowseWidget::on_clearButton_clicked ()
100
101
{
101
- // Remove all rows
102
- m_model-> removeRows ( 1 , m_model-> rowCount ()- 1 );
102
+ // Display messagebox for confirmation
103
+ if (QMessageBox::Yes == QMessageBox::question ( this , " Clear Table " , " Are you sure you want to clear this table? \n\n This action cannot be undone. " )) {
103
104
104
- // Refresh browse widget
105
- m_model->select ();
105
+ // Remove all rows TODO make more efficient
106
+ for (int i = 0 ; i < m_model->rowCount (); i++) {
107
+ m_model->removeRow (i);
108
+ }
109
+
110
+ // Refresh browse widget
111
+ m_model->select ();
112
+ }
106
113
}
107
114
108
115
void BrowseWidget::commitChanges ()
@@ -116,7 +123,20 @@ void BrowseWidget::commitChanges()
116
123
QMessageBox::critical (this , " Operation failed" , m_model->lastError ().text ());
117
124
}
118
125
}
126
+ }
127
+
128
+ void BrowseWidget::currentChanged (QModelIndex previous, QModelIndex current)
129
+ {
130
+ // If the row has changed, commit changes
131
+ if (previous.row () != current.row ()) {
119
132
120
- // Revert back to OnFieldChange
121
- // m_model->setEditStrategy(QSqlTableModel::OnFieldChange);
133
+ // Commit changes
134
+ commitChanges ();
135
+
136
+ // There's no reason to call this anymore, we don't need to know if the row has changed
137
+ disconnect (ui->tableView ->selectionModel (), SIGNAL (currentChanged (QModelIndex,QModelIndex)), this , SLOT (currentChanged (QModelIndex,QModelIndex)));
138
+
139
+ // Revert back to OnFieldChange
140
+ m_model->setEditStrategy (QSqlTableModel::OnFieldChange);
141
+ }
122
142
}
0 commit comments