Hello,
step by step I'm learning from here but unfortunately I've a question more:

When I use a QSqlRelationalTableModel with a TableView and a QSqlRelationalDelegate I can see relations as correct entries and get a combobox when selecting it for editing. But when I use a QSortFilterProxyModel, the entries are correct, but the Combobox is not shown when I want to edit I have to enter the key-vaues when I want to edit the field (which works but is not user friendly). Is there a way to get a ComboBox for them even if I use a ProxyModel?

Thanks for any help
Armin

In the example, when I comment Line 44 and uncomment line 41 it works as expected, getting a ComboBox when double-cilciking in the color-table's other-column (but without the model wich I need for sorting).

mainwindow.cpp
Qt Code:
  1. #include <QtGui>
  2. #include <QtSql>
  3. #include "mainwindow.h"
  4. MainWindow::MainWindow()
  5. {
  6. createOtherDock();
  7. createColorDock();
  8. }
  9. //---------------------------------------------------------------------------
  10. void MainWindow::createOtherDock()
  11. {
  12. QDockWidget *dock = new QDockWidget(tr("Other"), this);
  13.  
  14. otherModel = new QSqlRelationalTableModel(this);
  15. otherModel->setTable("other");
  16. otherModel->select();
  17.  
  18. otherView = new QTableView(dock);
  19. otherView->setModel(otherModel);
  20.  
  21. dock->setWidget(otherView);
  22. addDockWidget(Qt::RightDockWidgetArea, dock);
  23. }
  24. //---------------------------------------------------------------------------
  25. void MainWindow::createColorDock()
  26. {
  27. QDockWidget *dock = new QDockWidget(tr("Color"), this);
  28.  
  29. colorModel = new QSqlRelationalTableModel(this);
  30. colorModel->setTable("color");
  31. colorModel->setRelation(color_Other, QSqlRelation("other", "id", "name"));
  32. colorModel->select();
  33.  
  34. MyProxyModel = new QSortFilterProxyModel(this);
  35. MyProxyModel->setSourceModel(colorModel);
  36. MyProxyModel->sort(1, Qt::DescendingOrder);
  37.  
  38. colorView = new QTableView(dock);
  39.  
  40. // without Proxy I get a ComboBox
  41. // colorView->setModel(colorModel);
  42.  
  43. // with Proxy I get no ComboBox
  44. colorView->setModel(MyProxyModel);
  45.  
  46. // tried QStyledItemDelegate also, no success
  47. colorView->setItemDelegate(new QSqlRelationalDelegate(this));
  48.  
  49. dock->setWidget(colorView);
  50. addDockWidget(Qt::RightDockWidgetArea, dock);
  51. }
To copy to clipboard, switch view to plain text mode 

mainwindow.h
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QtGui>
  5. #include <QtSql>
  6.  
  7. enum {
  8. other_Id = 0,
  9. other_Name = 1,
  10. };
  11. enum {
  12. color_Id = 0,
  13. color_Name = 1,
  14. color_Other = 2,
  15. };
  16.  
  17. class MainWindow : public QMainWindow
  18. {
  19. Q_OBJECT
  20.  
  21. public:
  22. MainWindow();
  23.  
  24. private:
  25. void createOtherDock();
  26. void createColorDock();
  27.  
  28.  
  29. QTableView *otherView;
  30. QTableView *colorView;
  31.  
  32. QSortFilterProxyModel *MyProxyModel;
  33. };
  34. #endif
To copy to clipboard, switch view to plain text mode 

main.cpp creates some data to play with
Qt Code:
  1. #include <QApplication>
  2. #include <QtGui>
  3. #include <QtSql>
  4.  
  5. #include "mainwindow.h"
  6. //---------------------------------------------------------------------------
  7. bool Connect()
  8. {
  9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  10. db.setDatabaseName("data.dat");
  11. if (!db.open())
  12. {
  13. QMessageBox::warning(0, QObject::tr("Error"), db.lastError().text());
  14. return (false);
  15. }
  16. return (true);
  17. }
  18. //---------------------------------------------------------------------------
  19. void createDataBase()
  20. {
  21. QSqlQuery query;
  22. query.exec("DROP TABLE other");
  23. query.exec("DROP TABLE color");
  24.  
  25. query.exec("CREATE TABLE other ("
  26. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
  27. "name VARCHAR(10) NOT NULL)");
  28.  
  29. query.exec("CREATE TABLE color ("
  30. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
  31. "name VARCHAR(40) NOT NULL, "
  32. "otherid INTEGER NOT NULL, "
  33. "FOREIGN KEY (otherid) REFERENCES other)");
  34.  
  35. query.prepare("INSERT INTO other (id, name) "
  36. "VALUES (:id, :name)");
  37. query.bindValue(":id", 1);
  38. query.bindValue(":name", "A");
  39. query.exec();
  40. query.bindValue(":id", 2);
  41. query.bindValue(":name", "b");
  42. query.exec();
  43. query.bindValue(":id", 3);
  44. query.bindValue(":name", "C");
  45. query.exec();
  46.  
  47. query.prepare("INSERT INTO color (id, name, otherid) "
  48. "VALUES (:id, :name, :otherid)");
  49. query.bindValue(":id", 1);
  50. query.bindValue(":name", "Red");
  51. query.bindValue(":otherid", 1);
  52. query.exec();
  53. query.bindValue(":id", 2);
  54. query.bindValue(":name", "Green");
  55. query.bindValue(":otherid", 2);
  56. query.exec();
  57. query.bindValue(":id", 3);
  58. query.bindValue(":name", "Blue");
  59. query.bindValue(":otherid", 3);
  60. query.exec();
  61. }
  62. //---------------------------------------------------------------------------
  63.  
  64. int main(int argc, char *argv[])
  65. {
  66. QApplication app(argc, argv);
  67.  
  68. bool existingData = QFile::exists("data.dat");
  69. if (!Connect())
  70. {
  71. return(1);
  72. }
  73. if (!existingData)
  74. {
  75. createDataBase();
  76. }
  77. MainWindow Window;
  78. Window.show();
  79. // Window.filterColorView();
  80. return app.exec();
  81. }
To copy to clipboard, switch view to plain text mode 

Qt Creator project-file
Qt Code:
  1. QT += sql
  2. SOURCES += main.cpp \
  3. mainwindow.cpp
  4. HEADERS += mainwindow.h
To copy to clipboard, switch view to plain text mode