How to add one empty row in ComboBox with model
Hi again :)
I've a ComboBox fill by QSqlQueryModel:
Code:
ComboKierowcyModel->setQuery( "SELECT * FROM kierowcy ORDER BY Nazwisko" );
if (ComboKierowcyModel->lastError().isValid())
qDebug() << ComboKierowcyModel->lastError();
ComboKierowcy->setModel(ComboKierowcyModel);
ComboKierowcy->setModelColumn(1);
Everything is ok, but I need a one empty position on the first place. How add this?
Re: How to add one empty row in ComboBox with model
You will have to subclass QSqlQueryModel and reimplement data() and rowCount() and perhaps some more methods. You can do something like this:
In myquerymodel.h file:
Code:
#include <QSqlQueryModel>
{
Q_OBJECT
public:
virtual QVariant data
(const QModelIndex
& item,
int role
= Qt
::DisplayRole) const;
virtual int rowCount
(const QModelIndex
& parent
= QModelIndex()) const;
};
In the myquerymodel.cpp file:
Code:
{
}
QVariant MyQueryModel
::data(const QModelIndex
& item,
int role
) const {
if(item.row() == 0)
QModelIndex fakeItem
= this
->QSqlQueryModel
::index(item.
row()-1, item.
column());
return this->QSqlQueryModel::data(fakeItem, role);
}
int MyQueryModel::rowCount(const QModelIndex& parent) const
{
return this->QSqlQueryModel::rowCount(parent) + 1;
}