PDA

View Full Version : QSortFilterProxyModel and QSqlRelationalDelegate in TableView



Armin
6th February 2010, 22:07
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


#include <QtGui>
#include <QtSql>
#include "mainwindow.h"
MainWindow::MainWindow()
{
createOtherDock();
createColorDock();
}
//---------------------------------------------------------------------------
void MainWindow::createOtherDock()
{
QDockWidget *dock = new QDockWidget(tr("Other"), this);

otherModel = new QSqlRelationalTableModel(this);
otherModel->setTable("other");
otherModel->select();

otherView = new QTableView(dock);
otherView->setModel(otherModel);

dock->setWidget(otherView);
addDockWidget(Qt::RightDockWidgetArea, dock);
}
//---------------------------------------------------------------------------
void MainWindow::createColorDock()
{
QDockWidget *dock = new QDockWidget(tr("Color"), this);

colorModel = new QSqlRelationalTableModel(this);
colorModel->setTable("color");
colorModel->setRelation(color_Other, QSqlRelation("other", "id", "name"));
colorModel->select();

MyProxyModel = new QSortFilterProxyModel(this);
MyProxyModel->setSourceModel(colorModel);
MyProxyModel->sort(1, Qt::DescendingOrder);

colorView = new QTableView(dock);

// without Proxy I get a ComboBox
// colorView->setModel(colorModel);

// with Proxy I get no ComboBox
colorView->setModel(MyProxyModel);

// tried QStyledItemDelegate also, no success
colorView->setItemDelegate(new QSqlRelationalDelegate(this));

dock->setWidget(colorView);
addDockWidget(Qt::RightDockWidgetArea, dock);
}



mainwindow.h


#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,
};

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private:
void createOtherDock();
void createColorDock();

QSqlRelationalTableModel *otherModel;
QSqlRelationalTableModel *colorModel;

QTableView *otherView;
QTableView *colorView;

QSortFilterProxyModel *MyProxyModel;
};
#endif


main.cpp creates some data to play with


#include <QApplication>
#include <QtGui>
#include <QtSql>

#include "mainwindow.h"
//---------------------------------------------------------------------------
bool Connect()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("data.dat");
if (!db.open())
{
QMessageBox::warning(0, QObject::tr("Error"), db.lastError().text());
return (false);
}
return (true);
}
//---------------------------------------------------------------------------
void createDataBase()
{
QSqlQuery query;
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[])
{
QApplication app(argc, 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


QT += sql
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h

Armin
7th February 2010, 08:01
.... and how can I sort these ComboBoxes shown in the Table? ...