roko
roko

Reputation: 131

QT4 Qtableview disable selection of rows

I am using qtableview-s to show some data from sqlite tables. I have 2 tableviews which are essentialy same. They are both showing bus stops (same model) in rows. In first table i am selecting departure and what i would like to achieve is that in second table all entries before the selected one are made non selectable so that user cannot move backward to select them. I was able to hide them using setRowHidden(row,true) but i would like still to see them but not be able to select them.

I tried using flags Qt::ItemFlags (using flags method in custom model) for the row but no matter what i use the rows are still selectable. Does anyone know how to disable row in QTableView so that is still shown but not selectable.

Upvotes: 3

Views: 8207

Answers (3)

roko
roko

Reputation: 131

Thanks for the tips/help but in the mean time i found solution (well bug in my code). It was bug in my custom model. I was returning wrong flags for item. For others that might try to do something similar. You have to implement flags method in custom model (QSQLQueryModel derived) and return flag Qt::NoItemFlags for items that you dont want selected. I was returning QAbstractItemModel::flags(index) but there are some default flags allready set.

Qt::ItemFlags busStopsTableModel::flags(const QModelIndex &index) const
 {
    if(index.row()>lastDisableRowID){

        return QAbstractItemModel::flags(index)|Qt::ItemIsSelectable;
    }
    else
    {
        return Qt::NoItemFlags;
    }

 }

Upvotes: 5

buck
buck

Reputation: 1532

You could install an event filter on the QTableView and override mouse press / mouse move events (or create a class inheriting QTableView to do the same thing).

Event filter code would look like:

From the class instantiating & using the QTableView:

QTableView* view = new QTableView(this);
view->installEventFilter(this);

Create the eventFilter method for this same class:

bool MyClass::eventFilter(QObject* object, QEvent* event)
{
    if(object == view && (event->type() == QEvent::MousePress || event->type() == QEvent::MouseMove)) {
        // if statement to see if event position is on one of the rows you want to disable
            // if true, return true
    }
    return false;
}

Upvotes: 0

Rawler
Rawler

Reputation: 1620

Sorry. The flags in custom model was my only idea too. I'm assuming your original data-source is QSQLQueryModel? Did you create a subclass and override, or did you create a QAbstractProxyModel?

Upvotes: 0

Related Questions