PDA

View Full Version : how to disbale a column in a QTableView



rdjenner
16th November 2010, 15:43
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.

ChrisW67
16th November 2010, 21:02
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).

rdjenner
16th November 2010, 21:41
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();

ChrisW67
16th November 2010, 22:36
You should use
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:
[code]
class ManifestModel: public QSqlTableModel
{
public:
ManifestModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ):
QSqlTableModel(parent, db)
{ }

~ManifestModel() { }

Qt::ItemFlags flags ( const QModelIndex & index ) const
{
if (index.column() == 2)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
};

Then use ManifestModel in place of QSqlTableModel.

rdjenner
17th November 2010, 18:45
This worked perfectly. Thank you so much.

pratham_shah
5th September 2012, 08:21
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. ??

viulskiez
5th September 2012, 18:39
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.

ssaguiar
14th August 2019, 12:28
You should use
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:
[code]
class ManifestModel: public QSqlTableModel
{
public:
ManifestModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ):
QSqlTableModel(parent, db)
{ }

~ManifestModel() { }

Qt::ItemFlags flags ( const QModelIndex & index ) const
{
if (index.column() == 2)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
};

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

d_stranz
14th August 2019, 15:44
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.