Results 1 to 9 of 9

Thread: how to disbale a column in a QTableView

  1. #1
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default how to disbale a column in a QTableView

    I have looked through the post in an effort to try to find a solution, but nothing gave me what I was lkooking for. I have a QTableView with a QSqlTable as its model. I want to disable columns for entry in the view and cannot find a solution with regard QTableView, only QTableWidget. Can someone provide a small sample code of how to do this. Thank you in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to disbale a column in a QTableView

    If all the columns should be read-only only in this view, but not in other views of the same model) then try setting QAbstractItemView::NoEditTriggers with QAbstractItemView::setEditTriggers().

    If some of the columns or cells in the model should be read-only in all views on the model then override QAbstractItemModel::flags() and remove the Qt::ItemIsEditable flag on relevant columns/rows/cells.

    You could also write a proxy model that removes the relevant edit flags and use it for this this view only.

    If some of the columns in the views should be read-only in this view, but read-write elsewhere, then you should look at setting a delegate on the column. If the delegate has a do-nothing (return 0) implementation of QAbstractItemDelegate::createEditor() then it will probably disable editing (I have not tested this).

  3. #3
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to disbale a column in a QTableView

    Fairly new at qt. How would I incoporate this. Here is my code below. I only want the column titled "This Shipment" in my view to be editable.

    // build temp table
    updateStr = QString("insert into tt_manifest(company, so_number, manifest_number, upc, description, order_qty, total_shipped, qty_shipped, back_order_qty, rowid) select m.company, m.so_number, m.manifest_number, i.upc, i.description, CASE WHEN h.type = 'C' THEN 0 ELSE s.order_qty - s.total_shipped END, s.total_shipped, m.quantity, m.back_order_quantity, m.rowid from so_manifest m join so_details s on s.comp = m.company and s.so_number = m.so_number and s.line = m.so_detail_line join in_master i on i.comp = s.comp and i.part_number = s.part_number join so_header h on h.comp = m.company and h.so_number = m.so_number where m.company = %1 and m.so_number = %2 and m.manifest_number = %3").arg(ui->compLineEdit->text()).arg(ui->orderNumberLineEdit->text()).arg(maxManifestNumber);
    query.prepare(updateStr);
    query.exec(updateStr);

    model = new QSqlTableModel(this);
    model->setTable("tt_manifest");
    model->setHeaderData(upc, Qt::Horizontal, QObject::tr("Item"));
    model->setHeaderData(description, Qt::Horizontal, QObject::tr("Description"));
    model->setHeaderData(order_qty, Qt::Horizontal, QObject::tr("Order Qty"));
    model->setHeaderData(total_shipped, Qt::Horizontal, QObject::tr("Shipped to Date"));
    model->setHeaderData(qty_shipped, Qt::Horizontal, QObject::tr("This Shipment"));
    model->setHeaderData(back_order_qty, Qt::Horizontal, QObject::tr("Back Order Qty"));
    model->setHeaderData(rowid, Qt::Horizontal, QObject::tr("RowId"));
    model->select();

    ui->tableView->close();
    ui->tableView->setModel(model);
    ui->tableView->setColumnHidden(company,true);
    ui->tableView->setColumnHidden(sonumber,true);
    // ui->tableView->setColumnHidden(so_number,true);
    ui->tableView->setColumnHidden(manifest_number,true);
    ui->tableView->setColumnHidden(rowid,true);

    ui->tableView->resizeColumnsToContents();
    ui->tableView->verticalHeader()->hide();

    ui->tableView->show();

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to disbale a column in a QTableView

    You should use [code] tags around your code.

    Which option do you want to apply? Something like this will disable editing of all but the one column in all views on the model:
    Qt Code:
    1. class ManifestModel: public QSqlTableModel
    2. {
    3. public:
    4. ManifestModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ):
    5. QSqlTableModel(parent, db)
    6. { }
    7.  
    8. ~ManifestModel() { }
    9.  
    10. Qt::ItemFlags flags ( const QModelIndex & index ) const
    11. {
    12. if (index.column() == 2)
    13. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    14. else
    15. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 
    Then use ManifestModel in place of QSqlTableModel.

  5. The following 4 users say thank you to ChrisW67 for this useful post:

    AlphaWolfXV (1st August 2013), Hogwarts (14th July 2011), oberlus (26th August 2011), rdjenner (17th November 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to disbale a column in a QTableView

    This worked perfectly. Thank you so much.

  7. #6
    Join Date
    Mar 2012
    Location
    India
    Posts
    22
    Thanks
    3
    Qt products
    Qt4

    Default Re: how to disbale a column in a QTableView

    Hi, I am also facing the same problem. This solution worked perfectly for QTableWidgetItem, but I have also added QWidget instead of using QTableWidgetItem for some of the rows in the column. This solution is not disabling the QWidget.
    Am I missing something. ??

  8. #7
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: how to disbale a column in a QTableView

    Hi, I am also facing the same problem. This solution worked perfectly for QTableWidgetItem, but I have also added QWidget instead of using QTableWidgetItem for some of the rows in the column. This solution is not disabling the QWidget.
    Am I missing something. ??
    If I'm not mistaken, you are using QTableWidget, not QTableView.
    ~ We are nothing in this universe ~

  9. #8
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to disbale a column in a QTableView

    Quote Originally Posted by ChrisW67 View Post
    You should use [code] tags around your code.

    Which option do you want to apply? Something like this will disable editing of all but the one column in all views on the model:
    Qt Code:
    1. class ManifestModel: public QSqlTableModel
    2. {
    3. public:
    4. ManifestModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ):
    5. QSqlTableModel(parent, db)
    6. { }
    7.  
    8. ~ManifestModel() { }
    9.  
    10. Qt::ItemFlags flags ( const QModelIndex & index ) const
    11. {
    12. if (index.column() == 2)
    13. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    14. else
    15. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 
    Then use ManifestModel in place of QSqlTableModel.
    I am searching for some way to disable 3 columns contents in a tableview and has found your code.
    As I am new to QT programming, I wish to ask if it's possible to tell how to use this code? Where I need to implement it? In what file?

    I really thank you for any help because I haven't found a solution yet.

    Thanks

  10. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to disbale a column in a QTableView

    I wish to ask if it's possible to tell how to use this code?
    If you look again at the code, you will see that in this case the poster has derived a custom model class (ManifestModel) from the Qt class QSqlTableModel and has implemented the enable / disable code by overriding the flags() method.

    All Qt model classes are derived from the QAbstractItemModel base class, which defines a number of pure virtual methods which must be implemented in any class derived from this base class. In particular, most custom models will implement rowCOunt(), columnCount(), data(), flags() and possibly a few other methods to define the behavior of their model.

    If you don't understand this description, I would strongly suggest that you read the documentation on Qt Model / View architecture (google it) and study the many examples that come with the Qt distribution that deal with model / view programming, editable and non-editable models, customizing view appearance, etc.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QTableView column trouble
    By nategoofs in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2009, 21:14
  2. How can I lock the first column in QTableView
    By alfred4923 in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2009, 09:55
  3. Coloring a QTableview column
    By Potch in forum Qt Programming
    Replies: 2
    Last Post: 5th April 2009, 00:18
  4. Currently sorted column in QTableView
    By borges in forum Newbie
    Replies: 1
    Last Post: 21st September 2007, 17:52
  5. QTableView without the first counter column?
    By pmaktieh.sirhc in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2007, 23:03

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.