PDA

View Full Version : Add a blank entry in a combobox



lynnH
19th April 2011, 16:48
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.

wysota
19th April 2011, 22:07
You can populate the combobox with any entries you want coming from any source you want. What's exactly the problem?

lynnH
20th April 2011, 08:34
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.

wysota
20th April 2011, 09:26
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?

lynnH
20th April 2011, 11:24
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?

wysota
20th April 2011, 11:52
If the model contains this special empty entry, it should be added automatically when calling setModel() on the combobox.

lynnH
20th April 2011, 12:01
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?

wysota
20th April 2011, 16:57
The model does not contain the empty entry
So why did you write this in your previous post?

I did succeeded in adding the blank line in my model

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.

lynnH
20th April 2011, 17:10
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.

lynnH
21st April 2011, 10:40
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

#include <QSqlQueryModel>

class BlankModel : public QSqlQueryModel {
Q_OBJECT

public:

/**
* Constructor.
* @param pParent parent.
*/
BlankModel(QObject *pParent = 0);

QVariant data(const QModelIndex & pIndex, int pRole) const;

virtual int rowCount ( const QModelIndex & pParent = QModelIndex() ) const;

};


Cpp file

BlankModel::BlankModel(QObject *pParent): QSqlQueryModel(pParent){}


int BlankModel::rowCount(const QModelIndex & pParent) const
{
return QSqlQueryModel::rowCount(pParent) + 1;
}

QVariant BlankModel::data(const QModelIndex & pIndex, int pRole) const
{
if (!pIndex.isValid())
{
return QVariant();
}

if (pIndex.row() == 0)
{
return QVariant();
}
else
{
QModelIndex vNextIndex = QSqlQueryModel::index(pIndex.row() - 1,pIndex.column());
return QSqlQueryModel::data(vNextIndex,pRole);
}
}