I want to add a blank entry at the first position in a combobox having its data provided by a database.
Is there another solution than using a QSqlQueryModel as a model for the combobox?
Thank for any help.
I want to add a blank entry at the first position in a combobox having its data provided by a database.
Is there another solution than using a QSqlQueryModel as a model for the combobox?
Thank for any help.
You can populate the combobox with any entries you want coming from any source you want. What's exactly the problem?
My problem is slightly complicated, I will try to explain it.
I have tableview which data are provided by a model gathering its data from a database. On one of the column I have set a custom delegate to have a set of available options presented in a drop down list. This delegate is a bit advanced as the data presented in the DDL are coming from a foreign key relation with another table which are dynamically filtered by a data in another column of the tableview.
What I want is to be able to choose a blank entry (no data set for the column) whereas there is no blank entry in the foreign key relation I am using.
I have the same issue with another column, easier to solve as there is no dynamic filtering. In that case, I finally used a QSqlQueryModel in the custom delegate, and the query I used is an sql "union", the second part of the union sending back an empty entry.
I tried to do the same with the above mention case, and to account for the dynamic filtering I inverted the 2 parts of the sql 'union' in my query (in order to add the 'where' clause at the end of the query). But the QSqlQueryModel is then only seeing the first part and returns only the blank entry.
So as you can see it is not a trivial issue. I was wondering if I did not go in a complet erroneous direction ant that there is a more easier way of solving my problem.
If you have any idea, I will appreciate.
Ok but what's exactly the problem? Your delegate is somehow populating the combobox with data fetched from the model. So why can't you add a blank entry either to the model or directly to the combobox?
As I tired to explain, I did succeeded in adding the blank line in my model (QSqlQueryModel probelm with the union requests) and I do not know how to add a blank entry directly to the combobox.
That is the initial question, how to add a blank entry directly to the combobox with data provided via a model?
If the model contains this special empty entry, it should be added automatically when calling setModel() on the combobox.
The model does not contain the empty entry (I tried to add it and I could not). Therefore, is there a way to add it directly to the combobox?
So why did you write this in your previous post?
Either subclass your query model and reimplement the model interface to return one more entry or implement a proxy model that will do it (which is essentially the same). Or if you don't feel fit for the task, populate the combobox manually using entries returned by your SQL query.I did succeeded in adding the blank line in my model
I am sorry, it was a typo "I did NOT succeeded in adding the blank line in my model".
Thanks for your help. I think I will give the query model subclassing a try.
Subclassing a query model to add an empty entry is very easy. I add here my code in case it can help someone:
Header
//Qt includes
Qt Code:
#include <QSqlQueryModel> Q_OBJECT public: /** * Constructor. * @param pParent parent. */ };To copy to clipboard, switch view to plain text mode
Cpp file
BlankModel::BlankModel(QObject *pParent): QSqlQueryModel(pParent){}
Qt Code:
{ } { if (!pIndex.isValid()) { } if (pIndex.row() == 0) { } else { } }To copy to clipboard, switch view to plain text mode
Last edited by wysota; 21st April 2011 at 12:58. Reason: missing [code] tags
Bookmarks