Results 1 to 3 of 3

Thread: QSortFilterProxyModel how to use it in this exampl?

  1. #1
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QSortFilterProxyModel how to use it in this exampl?

    Screenshot from 2014-04-20 19:40:52.pngScreenshot from 2014-04-20 19:37:44.png
    Screenshot from 2014-04-20 19:38:59.png

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel how to use it in this exampl?

    Quote Originally Posted by jaafari View Post
    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/5868...Row-is-Clicked

    Quote Originally Posted by jaafari View Post
    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    jaafari (22nd April 2014)

  4. #3
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Wink Re: QSortFilterProxyModel how to use it in this exampl?

    mainform.cpp
    Qt Code:
    1. departmentModel = new QSqlRelationalTableModel(this);
    2. departmentModel->setTable("department");
    3. departmentModel->setRelation(Department_LocationId, QSqlRelation("location", "id", "name"));
    4. departmentModel->setSort(Department_Name, Qt::AscendingOrder);
    5. departmentModel->setHeaderData(Department_Name, Qt::Horizontal,
    6. tr("Dept."));
    7. departmentModel->setHeaderData(Department_LocationId,
    8. Qt::Horizontal, tr("Location"));
    9. departmentModel->select();
    10.  
    11. departmentView = new QTableView;
    12. ui->departmentView->setModel(departmentModel);
    13. ui->departmentView->setSelectionBehavior(QAbstractItemView::SelectRows);
    14. ui->departmentView->setSelectionMode(QAbstractItemView::SingleSelection);
    15. ui->departmentView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    16. //ui->departmentView->setItemDelegate(new QSqlRelationalDelegate(this));
    17. ui->departmentView->setSelectionMode(QAbstractItemView::SingleSelection);
    18. ui->departmentView->setSelectionBehavior(QAbstractItemView::SelectRows);
    19. ui->departmentView->setColumnHidden(Department_Id, true);
    20. ui->departmentView->resizeColumnsToContents();
    21. ui->departmentView->horizontalHeader()->setStretchLastSection(true);
    22. ui->departmentView->setCurrentIndex(departmentModel->index(0, 0));
    23. //-------------------------------------------------------
    24. connect(ui->editButton, SIGNAL(clicked()),
    25. this, SLOT(editEmployees()));
    26. //--------------------------------------------------------
    27.  
    28. void MainForm::editEmployees()
    29. {
    30. const QModelIndex index = ui->departmentView->currentIndex();
    31. if (index.isValid()) {
    32. EmployeeForm employeeform(ui->departmentView->model(), index.row(), this);
    33. employeeform.exec();
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    employeeform.h
    Qt Code:
    1. class EmployeeForm : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit EmployeeForm(QAbstractItemModel *model, int row, QWidget *p = 0);
    7. ~EmployeeForm();
    To copy to clipboard, switch view to plain text mode 

    employeeform.cpp
    Qt Code:
    1. employeeModel = new QSqlRelationalTableModel(this);
    2. employeeModel->setTable("employee");
    3. employeeModel->setRelation(Employee_DepartmentId,
    4. QSqlRelation("department", "id", "name"));
    5. employeeModel->setSort(Employee_Name, Qt::AscendingOrder);
    6. employeeModel->setHeaderData(Employee_Name, Qt::Horizontal,
    7. tr("Name"));
    8. employeeModel->setHeaderData(Employee_Extension, Qt::Horizontal,
    9. tr("Ext."));
    10. employeeModel->setHeaderData(Employee_Email, Qt::Horizontal,
    11. tr("Email"));
    12.  
    13. employeeProxy= new QSortFilterProxyModel(this);
    14. employeeProxy->setSourceModel(employeeModel);
    15. employeeProxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
    16. employeeProxy->setFilterKeyColumn(-1);
    17. employeeProxy->setDynamicSortFilter(true);
    18.  
    19.  
    20.  
    21. employeeView = new QTableView;
    22. ui->employeeView->setModel(employeeProxy);
    23. // ui->employeeView->setSelectionMode(QAbstractItemView::SingleSelection);
    24. ui->employeeView->setSelectionBehavior(QAbstractItemView::SelectRows);
    25. // ui->employeeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    26. ui->employeeView->horizontalHeader()->setStretchLastSection(true);
    27. ui->employeeView->setColumnHidden(Employee_Id, true);
    28. ui->employeeView->setColumnHidden(Employee_DepartmentId, true);
    29. ui->employeeView->setColumnHidden(Employee_StartDate, true);
    To copy to clipboard, switch view to plain text mode 


    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??

Similar Threads

  1. QSortFilterProxyModel nothing changes
    By unix7777 in forum Qt Programming
    Replies: 7
    Last Post: 19th August 2012, 08:13
  2. Qsortfilterproxymodel
    By migel in forum Newbie
    Replies: 1
    Last Post: 3rd October 2011, 16:40
  3. Using QSortFilterProxyModel
    By Jennie Bystrom in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2007, 10:28
  4. QSortFilterProxyModel
    By evgenM in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 11:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.