Results 1 to 7 of 7

Thread: tableview , model and owncolumn

  1. #1
    Join Date
    May 2010
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default tableview , model and owncolumn

    Hi.

    I have something like this :
    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(this);
    2. model->setQuery("SELECT name,surname FROM students WHERE class= "+id);
    3. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
    4. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Surname"));
    5. model->insertColumn(2);
    6. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Amount"),Qt::EditRole);
    To copy to clipboard, switch view to plain text mode 
    And i want to set third column to editable value. I have to type there text.

    How force model to set column(2) with textbox field?

    Thanks in advance

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: tableview , model and owncolumn

    The user can't edit the model directly, you have to edit through the view.
    Like this, for example:

    Qt Code:
    1. QModelIndex index = ui->tvMemories->QAbstractItemView::currentIndex();
    2. ui->tvMemories->edit(index);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2010
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: tableview , model and owncolumn

    thank You for Your reply

    but i have to do something like this:



    insert line edit in last cell and next get value to insert to database after click save

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: tableview , model and owncolumn

    Create a delegate

  5. #5
    Join Date
    May 2010
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: tableview , model and owncolumn

    thanks

    i have this.

    in header :

    Qt Code:
    1. namespace Ui {
    2. class DodajWplate;
    3. class LineEditDelegate;
    4. }
    5.  
    6.  
    7.  
    8. class DodajWplate : public QDialog {
    9. ...
    10. };
    11.  
    12.  
    13. class LineEditDelegate : public QItemDelegate
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. LineEditDelegate(QObject* parent = 0);
    19.  
    20. QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    21. void setEditorData(QWidget* editor, const QModelIndex& index) const;
    22. };
    To copy to clipboard, switch view to plain text mode 

    in cpp :

    Qt Code:
    1. QWidget* LineEditDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const
    2. {
    3. QLineEdit* lewtv = new QLineEdit(parent);
    4.  
    5. return lewtv;
    6. }
    7.  
    8. void LineEditDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
    9. {
    10. QString value = index.model()->data(index, Qt::EditRole).toString();
    11.  
    12. QLineEdit* lewtv = static_cast<QLineEdit*>(editor);
    13.  
    14. lewtv->setText(value);
    15. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. ui->tableView->setItemDelegateForColumn(2, new LineEditDelegate);
    To copy to clipboard, switch view to plain text mode 

    When i want to build i have error :

    undefined reference to `LineEditDelegate::LineEditDelegate(QObject*)'

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: tableview , model and owncolumn

    Do you have
    Qt Code:
    1. LineEditDelegate::LineEditDelegate(QObject *parent) :
    2. QItemDelegate(parent)
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 
    in your cpp file?

  7. #7
    Join Date
    May 2010
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: tableview , model and owncolumn

    thanks.
    program runs. but tableview in column(2) doesn't have lineedit widget i have to change something? Maybe something is missing

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. TableView/Model Display Question
    By SixDegrees in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2010, 09:06
  3. Replies: 12
    Last Post: 24th July 2010, 08:12
  4. tableview model view problem
    By skuda in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 14:02
  5. Replies: 1
    Last Post: 10th October 2007, 10:11

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.