Results 1 to 2 of 2

Thread: How to add one empty row in ComboBox with model

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default How to add one empty row in ComboBox with model

    Hi again

    I've a ComboBox fill by QSqlQueryModel:

    Qt Code:
    1. ComboKierowcy = new QComboBox(parent);
    2. QSqlQueryModel *ComboKierowcyModel = new QSqlQueryModel();
    3.  
    4. ComboKierowcyModel->setQuery( "SELECT * FROM kierowcy ORDER BY Nazwisko" );
    5. if (ComboKierowcyModel->lastError().isValid())
    6. qDebug() << ComboKierowcyModel->lastError();
    7.  
    8. ComboKierowcy->setModel(ComboKierowcyModel);
    9. ComboKierowcy->setModelColumn(1);
    To copy to clipboard, switch view to plain text mode 

    Everything is ok, but I need a one empty position on the first place. How add this?

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. #include <QSqlQueryModel>
    2.  
    3. class MyQueryModel : public QSqlQueryModel
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MyQueryModel(QObject *parent = 0);
    9.  
    10. virtual QVariant data(const QModelIndex& item, int role = Qt::DisplayRole) const;
    11. virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
    12. };
    To copy to clipboard, switch view to plain text mode 

    In the myquerymodel.cpp file:
    Qt Code:
    1. MyQueryModel::MyQueryModel(QObject *parent) : QSqlQueryModel(parent)
    2. {
    3. }
    4.  
    5. QVariant MyQueryModel::data(const QModelIndex& item, int role) const
    6. {
    7. if(item.row() == 0)
    8. return QVariant();
    9.  
    10. QModelIndex fakeItem = this->QSqlQueryModel::index(item.row()-1, item.column());
    11. return this->QSqlQueryModel::data(fakeItem, role);
    12. }
    13.  
    14. int MyQueryModel::rowCount(const QModelIndex& parent) const
    15. {
    16. return this->QSqlQueryModel::rowCount(parent) + 1;
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by victor.fernandez; 3rd August 2009 at 13:12.

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Replies: 1
    Last Post: 3rd June 2009, 19:08
  3. Help ComboBox and Model
    By weepdoo in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2008, 17:50
  4. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  5. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.