Results 1 to 2 of 2

Thread: QTableView and SqlLite

  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableView and SqlLite

    I use SQLite 3.
    in that database I have table
    Qt Code:
    1. CREATE TABLE [languages] (
    2. id INTEGER PRIMARY KEY AUTOINCREMENT,
    3. language TEXT NOT NULL ,
    4. used BOOL NOT NULL
    5. );
    To copy to clipboard, switch view to plain text mode 

    I create new model and set that options
    Qt Code:
    1. m_langugesTableModel = new QSqlTableModel(NULL, database);
    2. m_langugesTableModel->setTable("languages");
    3. m_langugesTableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4.  
    5. res = m_langugesTableModel->select();
    6. res = m_langugesTableModel->removeColumn(0);
    7. res = m_langugesTableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Language"));
    8. res = m_langugesTableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Enabled"));
    To copy to clipboard, switch view to plain text mode 

    than I create new QTableView and set model for that view.
    Qt Code:
    1. tv_languages->setModel(m_languagesTableModel);
    To copy to clipboard, switch view to plain text mode 

    I want to edit used field of database using QCheckBox, but if I create Delegate, than I don't now how create correct paint method for that delegate.
    If I create my own model, than CheckBox draw in one place, but to edit value I had to twice click near checkbox.

    I try use delegate and fags() method of my model, but than I got two checkboxes, when edit value.

    How can I get CheckBox in table view, than edit with one click?

    Please, help me to solve my problem.
    Last edited by Myrgy; 4th October 2009 at 13:38.

  2. #2
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView and SqlLite

    Now I make model, that correctly show data from databse, but if I remove ID collumn from model, than I get QSqlQuery::value: not positioned on a valid record.


    Model for table.
    Qt Code:
    1. class LangableModel:public QSqlTableModel
    2. {
    3. int colWithCheckBox;
    4. public:
    5. LangableModel(QSqlDatabase db)
    6. :QSqlTableModel(NULL,db)
    7. {
    8. colWithCheckBox = 1;
    9. }
    10.  
    11. Qt::ItemFlags flags( const QModelIndex &index) const
    12. {
    13. Qt::ItemFlags flags = QSqlTableModel::flags(index);
    14. if (index.column() == colWithCheckBox)
    15. {
    16. return Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsUserCheckable;
    17. }
    18. return flags;
    19. }
    20. QVariant data ( const QModelIndex & index, int role ) const
    21. {
    22. QVariant value = QSqlTableModel::data(index, role);
    23. if (index.column() == colWithCheckBox)
    24. {
    25. if(role == Qt::CheckStateRole)
    26. return (QSqlTableModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
    27. else
    28. return QVariant();
    29. }
    30. return value;
    31. }
    32.  
    33. bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )
    34. {
    35. if (index.column() == colWithCheckBox)
    36. {
    37. QString val = (value==Qt::Checked)?"1":"0";
    38. switch( role )
    39. {
    40. case Qt::CheckStateRole:
    41. qDebug()<<"val:"<<val<<" index:"<<index;
    42. return QSqlTableModel::setData(index, val, Qt::EditRole);
    43. default:
    44. return false;
    45. }
    46. }else
    47. return QSqlTableModel::setData(index, value, role);
    48. }
    49. };
    To copy to clipboard, switch view to plain text mode 

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.