PDA

View Full Version : QSortFilterProxyModel how to use it in this exampl?



jaafari
21st April 2014, 22:22
1030010302
10301

hello guys!
i am new here! in this exampl that i found in a Qt book i want to make some changes the exampl shows tow tables in one form.. one is for the partments the other one is for employees.. when the user clicks on the department row it filters and shows the employees that are belongs to that department.. what i want to do now is to make some changes in the code so i want to show the employees in another dialog.. when i dubleclick a row of a department or if that is not posable i have to use pushbutton to show employees of that department in the other dialog.. i do not know where to put the code of QSortFilterProxyModel also dont know what to put in the signals and slot or C++ i am new here sorry for my bad english..


please help thanx :p

anda_skoa
22nd April 2014, 08:01
what i want to do now is to make some changes in the code so i want to show the employees in another dialog.. when i dubleclick a row of a department

The view has a doubleClicked() signal.
The rest works like with the button for which you've already got a solution in http://www.qtcentre.org/threads/58685-How-to-Open-a-New-Dialog-when-a-Row-is-Clicked



i do not know where to put the code of QSortFilterProxyModel

Since you move the second table view to the dialog, you also move its model, in this case its model is the filter model.

Cheers,
_

jaafari
23rd April 2014, 11:56
mainform.cpp


departmentModel = new QSqlRelationalTableModel(this);
departmentModel->setTable("department");
departmentModel->setRelation(Department_LocationId, QSqlRelation("location", "id", "name"));
departmentModel->setSort(Department_Name, Qt::AscendingOrder);
departmentModel->setHeaderData(Department_Name, Qt::Horizontal,
tr("Dept."));
departmentModel->setHeaderData(Department_LocationId,
Qt::Horizontal, tr("Location"));
departmentModel->select();

departmentView = new QTableView;
ui->departmentView->setModel(departmentModel);
ui->departmentView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->departmentView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->departmentView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
//ui->departmentView->setItemDelegate(new QSqlRelationalDelegate(this));
ui->departmentView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->departmentView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->departmentView->setColumnHidden(Department_Id, true);
ui->departmentView->resizeColumnsToContents();
ui->departmentView->horizontalHeader()->setStretchLastSection(true);
ui->departmentView->setCurrentIndex(departmentModel->index(0, 0));
//-------------------------------------------------------
connect(ui->editButton, SIGNAL(clicked()),
this, SLOT(editEmployees()));
//--------------------------------------------------------

void MainForm::editEmployees()
{
const QModelIndex index = ui->departmentView->currentIndex();
if (index.isValid()) {
EmployeeForm employeeform(ui->departmentView->model(), index.row(), this);
employeeform.exec();
}
}


employeeform.h


class EmployeeForm : public QDialog
{
Q_OBJECT

public:
explicit EmployeeForm(QAbstractItemModel *model, int row, QWidget *p = 0);
~EmployeeForm();


employeeform.cpp


employeeModel = new QSqlRelationalTableModel(this);
employeeModel->setTable("employee");
employeeModel->setRelation(Employee_DepartmentId,
QSqlRelation("department", "id", "name"));
employeeModel->setSort(Employee_Name, Qt::AscendingOrder);
employeeModel->setHeaderData(Employee_Name, Qt::Horizontal,
tr("Name"));
employeeModel->setHeaderData(Employee_Extension, Qt::Horizontal,
tr("Ext."));
employeeModel->setHeaderData(Employee_Email, Qt::Horizontal,
tr("Email"));

employeeProxy= new QSortFilterProxyModel(this);
employeeProxy->setSourceModel(employeeModel);
employeeProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
employeeProxy->setFilterKeyColumn(-1);
employeeProxy->setDynamicSortFilter(true);



employeeView = new QTableView;
ui->employeeView->setModel(employeeProxy);
// ui->employeeView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->employeeView->setSelectionBehavior(QAbstractItemView::SelectRows );
// ui->employeeView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui->employeeView->horizontalHeader()->setStretchLastSection(true);
ui->employeeView->setColumnHidden(Employee_Id, true);
ui->employeeView->setColumnHidden(Employee_DepartmentId, true);
ui->employeeView->setColumnHidden(Employee_StartDate, true);




i build and run the the program without any error.. when i click button it closes and shows this--->



The program has unexpectedly finished.
/pach/../projects/QtProjects/staffmanager exited with code 0


any help??
:p:p:p