Results 1 to 10 of 10

Thread: Add a blank entry in a combobox

  1. #1
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Add a blank entry in a combobox

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Add a blank entry in a combobox

    You can populate the combobox with any entries you want coming from any source you want. What's exactly the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Add a blank entry in a combobox

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Add a blank entry in a combobox

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Add a blank entry in a 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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Add a blank entry in a combobox

    If the model contains this special empty entry, it should be added automatically when calling setModel() on the combobox.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Add a blank entry in a 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?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Add a blank entry in a combobox

    Quote Originally Posted by lynnH View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Add a blank entry in a combobox

    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.

  10. #10
    Join Date
    Apr 2010
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Add a blank entry in a combobox

    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:
    1. #include <QSqlQueryModel>
    2.  
    3. class BlankModel : public QSqlQueryModel {
    4. Q_OBJECT
    5.  
    6. public:
    7.  
    8. /**
    9.   * Constructor.
    10.   * @param pParent parent.
    11.   */
    12. BlankModel(QObject *pParent = 0);
    13.  
    14. QVariant data(const QModelIndex & pIndex, int pRole) const;
    15.  
    16. virtual int rowCount ( const QModelIndex & pParent = QModelIndex() ) const;
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    Cpp file

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

    Qt Code:
    1. int BlankModel::rowCount(const QModelIndex & pParent) const
    2. {
    3. return QSqlQueryModel::rowCount(pParent) + 1;
    4. }
    5.  
    6. QVariant BlankModel::data(const QModelIndex & pIndex, int pRole) const
    7. {
    8. if (!pIndex.isValid())
    9. {
    10. return QVariant();
    11. }
    12.  
    13. if (pIndex.row() == 0)
    14. {
    15. return QVariant();
    16. }
    17. else
    18. {
    19. QModelIndex vNextIndex = QSqlQueryModel::index(pIndex.row() - 1,pIndex.column());
    20. return QSqlQueryModel::data(vNextIndex,pRole);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 21st April 2011 at 12:58. Reason: missing [code] tags

Similar Threads

  1. QWebView showing blank pages.
    By gontham in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2010, 10:02
  2. customize QTableView to add blank rows
    By rosenth in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2010, 11:47
  3. issue about blank area appear
    By cherrydou in forum Qt Programming
    Replies: 5
    Last Post: 23rd May 2008, 07:34
  4. QWizard with QWizardPages gives blank pages?
    By Tiansen in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2008, 06:45
  5. Blank file using QXmlStreamWriter
    By sgmurphy19 in forum Qt Programming
    Replies: 4
    Last Post: 14th November 2007, 20:45

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.