PDA

View Full Version : QTableWidget special selectionbehavior



Qiieha
11th March 2012, 17:57
Hi,
I have a QTableWidget with i.e 6 columns. One Dataobject consists of columns. So there are 2 Dataobjects and every object has 3 columns.
Now I need I special selection behaviour for my Table. If the user clicks on an item at column 0 and some row, the items of column 0 to 3 at this row should get selsected. The selectionbehavior should be extended selection.

I tried lots of solutions with the QItemSelectionModel, but I didn't get what I want:
My best attempt was following:


void MyTableWidget::sel_slot(QItemSelection selected, QItemSelection deselected)
{
qDebug() << "MyTableWidget::sel_slot() called from signal: QTableWidget::itemSelectionChanged()";
qDebug() << "selected: " << QString::number(selected.indexes().size()) << " deselected: " << QString::number(deselected.indexes().size());
QModelIndexList sel_indexes = selected.indexes();
QModelIndex index;
foreach(index,sel_indexes){
int row_num = index.row();
int col_num = index.column();

int range1, range2;
if(col_num <= 2){
range1 = 0;
range2 = 2;
}
else if(col_num <= 5){
range1 = 3;
range2 = 5;
}

QModelIndex topleft = indexFromItem(item(row_num,range1));
QModelIndex bottomright = indexFromItem(item(row_num,range2));
QItemSelectionModel* model = this->selectionModel();
QItemSelection item_sel = model->selection();
QItemSelection n_itemsel(topleft,bottomright);
item_sel.merge(n_itemsel,QItemSelectionModel::Sele ct);
model->select(item_sel,QItemSelectionModel::Select);
}
}

It works nearly, but only if ctrl is pressed. What should I do with the indexes of deselected? If I do iterate over the deselected-indexes and deselect them, it doesn't work. Maybe somebody can help me....Thanks

Spitfire
13th March 2012, 10:22
If I understood you right, you have 6 columns and you want to select whole rows by clicking on any item but only for columns 0-3 and leave 4&5 unselected?
Is that constant or selected columns change? Does click on the item in column 4 or 5 should select the row as well?

If this odd behaviour doesn't change you can split the table into two views. In first show columns 0-3, in second show columns 4&5.
Use QAbstractItemView::SelectRows in the first column and QAbstractItemView::NoSelection in the second.
Connect scroll signals in both views to synchronize scrolling and you're good to go.

Qiieha
13th March 2012, 10:53
Thank u for your return. :)
Sorry 0-3 is incorrect(my fault). The table have 6 columns. If the user click on an item at row(0 to 2) the row should be selected at column 0 to 2. If the user clicks an item(3 - 5) the row should be selected at column 3 - 5.
The selection should be extended selection. You say I should use 2 tableviews? So I cannot use QTableWidget...

Spitfire
13th March 2012, 17:49
You can use two QTableWidget(s) without any problems.

Just set Qt::NoFrame on the widgets so they will look like one large table and put them in a layout with spacing set to 0.

Qiieha
14th March 2012, 11:03
Ok, thank u,
I did something similar in past, because I needed a sortable frozen column for QTableWidget.
I will try it...