Solved Dynamically change delegate QTableView
-
@mrjj
Hi,That would be better, but I didn't find how I could do that.
-
@EagleWatch
well easy option is just keep Delegate pointer
around and simply add a new function to it to update List
via the instance pointer.Alternatively add a new slot in Delegate
void ChangeList(QList<xx> list)
(should store list for use when editor is created)and a new signal in mainwindow
( that looks the same)
and connect them and simple use emit to update the combobox. -
@mrjj
That was one of my ideas but I think I don't understand how the delagate works.When I study the QSqlRelationnalDelegate, I know the combobox is created with the createEditor function.
However, I don't understand how to modify the item in the combobox when the editor already exists.I think I shoud use the function setEditorData but how to get the editor and the index I have to pass to this function ?
-
Hi
Sorry i miss the fact that you use a standard Delegate
QSqlRelationalDelegate
and not a custom one.
I think you need to make a small subclass of QSqlRelationnalDelegate
and override createEditor so you can adjust whats to happen.Since you can call the baseclass's QWidget *QSqlRelationalDelegate::createEditor
and get the Editor back. the (QWidget *) you can use qobject_cast to convert to
combobox and hence adjust its items.But Maybe this is overkill. I would wait and see if others have easier ideas.
-
@mrjj
Hi,I've thinking about what you said and I tried to create a subclass of QSqlRelationnalDelegate to get Editor back via the createEditor function.
However the createEditor function is protected, so I cannot override it.
I have no clue to get Editor back without create my own delegate class.As others have not easier ideas, what you suggests might the best for the moment.
So , how can I override the createEditor function if this is protected ?
-
@EagleWatch
Hi
Whats wrong with#include <QtSql/QSqlRelationalDelegate> class MySQLDelegate : QSqlRelationalDelegate { protected: virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QSqlRelationalDelegate::createEditor(parent,option,index);// call base } virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override { } };
-
I don't get it. Maybe I need to study a little more in C++.
That would be what I would like to get :
#include <QtSql/QSqlRelationalDelegate> class MySQLDelegate : QSqlRelationalDelegate { protected: virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { editor = QSqlRelationalDelegate::createEditor(parent,option,index);// call base } virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override { } private: QWidget *editor; };
Of course, the code will not work because of the "const" word.
-
@EagleWatch
ahh, the const.
well you dont need to store the Editor as its given in
setEditorData(QWidget *editor, const QModelIndex &index) const {
QComboBox *cb=qobject_cast<QComboBox *>(editor);
if (cb) {
use combo...
}// handle other types of widgets or send to base setEditorData
}
-
@mrjj
Hi,
I get it, thank you.
The setEditorData(QWidget *editor, const QModelIndex &index) const {} is protected so I cannot override it.delegate.h:18: error : 'virtual void Delegate::setEditorData(QWidget*, const QModelIndex&) const' is protected virtual void setEditorData(QWidget *editor, const QModelIndex &index) const ^
This function is not present in the QSqlRelationnalDelegate class.
Maybe I have to subclass QItemDelegate instead ? -
Hi, im missing something. following code compiles with no
remarks. its listed under protected and im not sure what goes wrong in your case.#include <QtSql/QSqlRelationalDelegate> class MySQLDelegate : QSqlRelationalDelegate { protected: virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QSqlRelationalDelegate::createEditor(parent,option,index);// call base } virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override { } };
-
@mrjj
I get the error only when I'm using the MySqlDelegate.So, in my projectWindow.h, if I have :
#include "mysqldelegate.h" private: MySqlDelegate *m_delegate;
I get the issue in the MySqlDelegate class.
Do you get the issue if you call your class ?
-
Hi,
Can you show how you are using it ?
-
@SGaist
Hi,Thank you for asking.
When searching to answer you, I realized that I forgot to remove some tests with the m_delegate pointer.
That's why I had the error.Now I have all necessary informations, I'll try to customize this subclass to fit with what I want.
I'll keep you updated guys.
Thanks.
-
Hi guys,
I've some news.
So after some days studying classes I was using, I discoverd that my problem was not because of the QSqlRelationalDelegate class.
In fact, that was the QSqlRelationalTableModel.When I create the QSqlRelationalTableModel, I had to set a relation.
When doing this, the QSqlRelationalTableModel keeps in cache the model given by the relation.Even if we change the relation, the model is still the same, that why the combobox was always showing the same data.
For now, I just delete the QSqlRelationalTableModel and recreate it with the new relation and it works like a charm.
-
Do you mean that if you call setRelation a second time it won't work ?
-
Absolutely.
All tests I've done have confirmed that.
The model doesn't change at all. -
Can you provide a minimal compilable example that shows that ?
You should also check the bug report system to see if it's something known.
-
Considering we have 3 table :
Integrity{
id INTEGER PRIMARY KEY AUTOINCREMENT,
level VARCHAR(100),
definition VARCHAR(500)
}
Confidentiality{
id INTEGER PRIMARY KEY AUTOINCREMENT,
level VARCHAR(100),
definition VARCHAR(500)
}
Proba{
id INTEGER PRIMARY KEY AUTOINCREMENT,
id_crit INT,
somethingelse VARCHAR(500);
}mainWindow.h
private : QSqlRelationalTableModel *m_tableModel; private slots: void slot_refreshRelation();
mainWindow.cpp
m_tableModel = new QSqlRelationalTableModel(); m_tableModel->setTable("Proba"); m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); m_tableModel->setRelation(1, QSqlRelation("Integrity", "id", "level")); m_tableModel->select(); ui->tableView->setModel(m_tableModel); ui->tableView->hideColumn(0); ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView); qDebug() << m_tableModel->relationModel(1)->tableName; // Will show : Integrity connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slot_refreshRelation())); void mainWindow::slot_refreshRelation() { m_table->setRelation(1, QSqlRelation("Confidentiality", "id", "level")); m_table->select(); qDebug() << m_tableModel->relationModel(1)->tableName; // Will show : Integrity }