QSortFilterProxyModel and QSqlRelationalDelegate in TableView
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
Code:
#include <QtGui>
#include <QtSql>
#include "mainwindow.h"
MainWindow::MainWindow()
{
createOtherDock();
createColorDock();
}
//---------------------------------------------------------------------------
void MainWindow::createOtherDock()
{
otherModel->setTable("other");
otherModel->select();
otherView->setModel(otherModel);
dock->setWidget(otherView);
addDockWidget(Qt::RightDockWidgetArea, dock);
}
//---------------------------------------------------------------------------
void MainWindow::createColorDock()
{
colorModel->setTable("color");
colorModel
->setRelation
(color_Other,
QSqlRelation("other",
"id",
"name"));
colorModel->select();
MyProxyModel->setSourceModel(colorModel);
MyProxyModel->sort(1, Qt::DescendingOrder);
// without Proxy I get a ComboBox
// colorView->setModel(colorModel);
// with Proxy I get no ComboBox
colorView->setModel(MyProxyModel);
// tried QStyledItemDelegate also, no success
dock->setWidget(colorView);
addDockWidget(Qt::RightDockWidgetArea, dock);
}
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
#include <QtSql>
enum {
other_Id = 0,
other_Name = 1,
};
enum {
color_Id = 0,
color_Name = 1,
color_Other = 2,
};
{
Q_OBJECT
public:
MainWindow();
private:
void createOtherDock();
void createColorDock();
};
#endif
main.cpp creates some data to play with
Code:
#include <QApplication>
#include <QtGui>
#include <QtSql>
#include "mainwindow.h"
//---------------------------------------------------------------------------
bool Connect()
{
db.setDatabaseName("data.dat");
if (!db.open())
{
return (false);
}
return (true);
}
//---------------------------------------------------------------------------
void createDataBase()
{
query.exec("DROP TABLE other");
query.exec("DROP TABLE color");
query.exec("CREATE TABLE other ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR(10) NOT NULL)");
query.exec("CREATE TABLE color ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR(40) NOT NULL, "
"otherid INTEGER NOT NULL, "
"FOREIGN KEY (otherid) REFERENCES other)");
query.prepare("INSERT INTO other (id, name) "
"VALUES (:id, :name)");
query.bindValue(":id", 1);
query.bindValue(":name", "A");
query.exec();
query.bindValue(":id", 2);
query.bindValue(":name", "b");
query.exec();
query.bindValue(":id", 3);
query.bindValue(":name", "C");
query.exec();
query.prepare("INSERT INTO color (id, name, otherid) "
"VALUES (:id, :name, :otherid)");
query.bindValue(":id", 1);
query.bindValue(":name", "Red");
query.bindValue(":otherid", 1);
query.exec();
query.bindValue(":id", 2);
query.bindValue(":name", "Green");
query.bindValue(":otherid", 2);
query.exec();
query.bindValue(":id", 3);
query.bindValue(":name", "Blue");
query.bindValue(":otherid", 3);
query.exec();
}
//---------------------------------------------------------------------------
int main(int argc, char *argv[])
{
bool existingData
= QFile::exists("data.dat");
if (!Connect())
{
return(1);
}
if (!existingData)
{
createDataBase();
}
MainWindow Window;
Window.show();
// Window.filterColorView();
return app.exec();
}
Qt Creator project-file
Code:
QT += sql
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h
Re: QSortFilterProxyModel and QSqlRelationalDelegate in TableView
.... and how can I sort these ComboBoxes shown in the Table? ...