Results 1 to 2 of 2

Thread: Column Not Editable

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

    Default Column Not Editable

    I have a QSqlTableModel and a QTableView.
    Trying to make just one column not editable.
    Found several examples in previous posts, but nothing is making sense to me as how to accomplish this in my code.

    The signal/slot for when a cell is clicked is working ok.

    Here is what I have so far:

    Qt Code:
    1. void MainWindow::checkEditable(QModelIndex index) {
    2. qDebug() << "view has been clicked!"; // good so far
    3. int col = index.column();
    4. int row = index.row();
    5. qDebug() << "clicked column is " << col; // got the column ok
    6. if (col != 0) { // it should be ok to edit
    7. // now what???
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Am I on the wrong track or is this simple to finish?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Column Not Editable

    Hi Waynew,
    An easy way to do this is to subclass QSqlTableModel. All you have to do is reimplement QSqlTableModel::flags(). If you are using Qt Creator add a new C++ class, put the flags() declaration in the header file and put the following in the cpp file:
    Qt Code:
    1. Qt::ItemFlags YourCustomModel::flags( const QModelIndex &index) const
    2. {
    3. Qt::ItemFlags flags = QSqlTableModel::flags(index);
    4. if (index.column() == 1 )
    5. flags &= ~Qt::ItemIsEditable;
    6. return flags;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Change the "1" to the column you want to not be editable.

    HTH

  3. The following user says thank you to norobro for this useful post:

    waynew (1st August 2010)

Similar Threads

  1. QTableWidget, one column editable
    By stella1016 in forum Qt Programming
    Replies: 5
    Last Post: 11th July 2012, 09:35
  2. editable rectangles
    By parmar ranjit in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2008, 09:59
  3. QTreeWidget - Making a Column editable.
    By Preeteesh in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2007, 12:02
  4. Setting one* column of QTreeView to be editable
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2006, 20:31
  5. Editable QListView Column ??
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2006, 18:55

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.