keyPressEvent(QKeyEvent * ev){ if (ev->key() == Qt::Key_Enter) {// по адресу сохранённому поставить курсор в эту ячейку }}
#ifndef KO_TABLE_VIEW_H#define KO_TABLE_VIEW_H#define QWT_DLL#include <QtGui>class QTableView;class PotokMainWindow;///////////////////////////////////////////////////////////////////class QPotokKOTableView : public QTableView{ Q_OBJECTpublic: QPotokKOTableView(PotokMainWindow* _parent, QString _filter);private slots: void delete_row(); void hide_column(); void sort_column(); void delete_column(); void show_all(); void filter();protected: void contextMenuEvent(QContextMenuEvent* event); QAction* act_delete_row; QAction* act_delete_column; QAction* act_hide_column; QAction* act_sort_column; QAction* act_show_all; QAction* act_filter; QString& table_filter; PotokMainWindow* main_wnd;};#endif
#include <QtGui>#include <QSqlDatabase>#include <QSqlError>#include <QSqlQuery>#define QWT_DLL#include "ui_custom_filter.h"#include "potokmain.h"#include "matrix.h"#include "ko_table_view.h"//////////////////////////////////////////////////////////////////////QPotokKOTableView::QPotokKOTableView(PotokMainWindow* _parent, QString _filter) : QTableView(_parent) , table_filter(_filter) , main_wnd(_parent){ act_delete_row = new QAction(QString::fromLocal8Bit("Удалить запись"), this); connect(act_delete_row, SIGNAL(triggered()), this, SLOT(delete_row())); act_hide_column = new QAction(QString::fromLocal8Bit("Спрятать столбец"), this); connect(act_hide_column, SIGNAL(triggered()), this, SLOT(hide_column())); act_sort_column = new QAction(QString::fromLocal8Bit("Отсортировать по столбцу"), this); connect(act_sort_column, SIGNAL(triggered()), this, SLOT(sort_column())); act_delete_column = new QAction(QString::fromLocal8Bit("Удалить столбец"), this); connect(act_delete_column, SIGNAL(triggered()), this, SLOT(delete_column())); act_show_all = new QAction(QString::fromLocal8Bit("Показать все"), this); connect(act_show_all, SIGNAL(triggered()), this, SLOT(show_all())); act_filter = new QAction(QString::fromLocal8Bit("Фильтр..."), this); connect(act_filter, SIGNAL(triggered()), this, SLOT(filter()));}//////////////////////////////////////////////////////////////////////void QPotokKOTableView::contextMenuEvent(QContextMenuEvent* event){ QMenu menu(this); menu.addAction(act_delete_row);// menu.addAction(act_hide_column); menu.addAction(act_sort_column);// menu.addAction(act_delete_column);// menu.addAction(act_show_all);// menu.addAction(act_filter); menu.exec(event->globalPos());}void QPotokKOTableView::hide_column(){ QItemSelectionModel* selection_model = selectionModel(); int ncol = selection_model->currentIndex().column(); if (ncol >= 0) hideColumn(ncol);}void QPotokKOTableView::delete_row(){ QItemSelectionModel* selection_model = selectionModel(); int n_row = selection_model->currentIndex().row(); if (n_row >= 0) model()->removeRow(n_row);}void QPotokKOTableView::delete_column(){ QItemSelectionModel* selection_model = selectionModel(); int n_col = selection_model->currentIndex().column(); if (n_col >= 0) model()->removeColumn(n_col);}void QPotokKOTableView::sort_column(){ QItemSelectionModel* selection_model = selectionModel(); int ncol = selection_model->currentIndex().column(); if (ncol >= 0) model()->sort(ncol);}void QPotokKOTableView::show_all(){ QSqlTableModel* mdl = reinterpret_cast<QSqlTableModel*>(model()); table_filter = QString(""); mdl->setFilter(table_filter); mdl->select();}void QPotokKOTableView::filter(){ QDialog *window = new QDialog; Ui::CustomFilterDialog ui; ui.setupUi(window); QSqlTableModel* mdl = reinterpret_cast<QSqlTableModel*>(model()); QItemSelectionModel* selection_model = selectionModel(); int ncol = selection_model->currentIndex().column(); if (ncol < 0) return; // название столбца QString hdr_string = mdl->headerData(ncol, Qt::Horizontal).toString(); ui.label_column_name->setText(hdr_string); // заполняем комбобоксы для операций ui.combo_operation1->insertItem(0, QString::fromLocal8Bit("РАВНО")); ui.combo_operation1->insertItem(1, QString::fromLocal8Bit("НЕ РАВНО")); ui.combo_operation1->insertItem(2, QString::fromLocal8Bit("БОЛЬШЕ")); ui.combo_operation1->insertItem(3, QString::fromLocal8Bit("МЕНЬШЕ")); ui.combo_operation1->insertItem(4, QString::fromLocal8Bit("БОЛЬШЕ ИЛИ РАВНО")); ui.combo_operation1->insertItem(5, QString::fromLocal8Bit("МЕНЬШЕ ИЛИ РАВНО")); ui.combo_operation2->insertItem(0, QString::fromLocal8Bit("")); ui.combo_operation2->insertItem(1, QString::fromLocal8Bit("РАВНО")); ui.combo_operation2->insertItem(2, QString::fromLocal8Bit("НЕ РАВНО")); ui.combo_operation2->insertItem(3, QString::fromLocal8Bit("БОЛЬШЕ")); ui.combo_operation2->insertItem(4, QString::fromLocal8Bit("МЕНЬШЕ")); ui.combo_operation2->insertItem(5, QString::fromLocal8Bit("БОЛЬШЕ ИЛИ РАВНО")); ui.combo_operation2->insertItem(6, QString::fromLocal8Bit("МЕНЬШЕ ИЛИ РАВНО")); ui.combo_operation2->setCurrentIndex(0); // заполняем комбобоксы для значений int nCurrentRow = selection_model->currentIndex().row(); int nMinRow = 0; int nMaxRow = Min<double>(mdl->rowCount() - 1, 25); int nIndex = 0; ui.combo_value2->insertItem(nIndex, QString("")); QString str_val = mdl->record(nCurrentRow).value(ncol).toString(); ui.combo_value1->insertItem(nIndex, str_val); ui.combo_value2->insertItem(nIndex + 1, str_val); ++nIndex; for (int k = nMinRow; k < nMaxRow; ++k, ++nIndex) { QString str_val = mdl->record(k).value(ncol).toString(); ui.combo_value1->insertItem(nIndex, str_val); ui.combo_value2->insertItem(nIndex + 1, str_val); } // запускаем диалог if (window->exec() == QDialog::Rejected) return; // анализируем состояние диалога и формируем SQL-запрос для фильтра QString operations[6] = { QString("="), QString("<>"), QString(">"), QString("<"), QString(">="), QString("<=") }; int cur_ind = ui.combo_operation1->currentIndex(); QString op1 = operations[cur_ind]; QString val = ui.combo_value1->currentText(); QString filter = hdr_string + op1 + QString("\'") + val + QString("\'") ; cur_ind = ui.combo_operation2->currentIndex(); if (cur_ind > 0) { QString op2 = operations[cur_ind - 1]; QString val2 = ui.combo_value2->currentText(); QString logic = QString(" AND "); if (ui.radio_or->isChecked()) { logic = QString (" OR "); } filter = filter + logic + hdr_string + op2 + QString("\'") + val2 + QString("\'"); } if (table_filter != QString("")) table_filter = QString("(") + table_filter + QString(") AND (") + filter + QString(")"); else table_filter = filter; mdl->setFilter(table_filter); mdl->select(); main_wnd->save_settings();}
C++ (Qt)QSqlTableModel Class Reference
C++ (Qt)enum EditStrategy { OnFieldChange, OnRowChange, OnManualSubmit }