[Interest] QTableView, readonly and selectable column text/data?

Mark Gaiser markg85 at gmail.com
Fri Aug 15 14:52:14 CEST 2014


On Fri, Aug 15, 2014 at 1:59 PM, Mark Gaiser <markg85 at gmail.com> wrote:
> Hi,
>
> I have a QTableView with data from a custom QSqlQueryModel.
> So far so good, data displays just fine.
>
> I'm searching for functionality to select text in any column as if the
> flag: Qt::ItemIsEditable is set. However, setting that also allows the
> user to edit (type) in the field which is not what i want.
>
> As an example, if you set readonly to true on a QLineEdit you get the
> exact functionality that i want. But i don't know how to get the
> QLineEdit (or whatever is used for a cell) object from a QTableView
> without subclassing it.
>
> Is there a simple way to get the functionality as described above
> without subclassing QTableView?
>
> Best regards,
> Mark

Right.. Answering my own question in case others find themselves stuck
with the same requirement.
QTableView::setItemDelegate is the solution. That takes a
QAbstractItemDelegate. You should make your own delegate (on top of
QItemDelegate) like so:

CustomDelegate : public QItemDelegate
{
  ...
};

Then you should re-implement the createEditor method with whatever
component you want. In my case i simply make a QLineEdit and set
readOnly to false. That did it for me.



More information about the Interest mailing list