Qiieha
11th March 2012, 16: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
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