elmo
13th September 2009, 12:30
I've been trying for some time now to make QTableWidget behave the way I want it.
I have a few MyTableWidgets (which inherit from QTableWidget) on the one tab. I modified QTableWidget so it would display in the first (and as for now only) row QLineEdits (to be precise it displays my class inheriting from QLineEdit) using QTableWidget::setCellWidget().
Now I would like to traverse through all MyTableWidgets on this tab.
If I have only a one column (so it's also the only cell in MyTableWidget) when I press Tab it goes to the next MyTableWidget (and I've reimplemented focusInEvent so the first cell in this MyTableWidget gets the focus). When I press Tab again and again it loops through all cells in the only row in this MyTableWidget, but never leaves MyTableWidget and I don't know where to change this behavior.
So my question is how to change QTableWidget so that after pressing Tab in the last cell it will go to the next widget not first cell (and shift+tab in the first cell to go to the previous widget)?
From all I read it appears I should do something by reimplementing focusNextPrevChild( bool), but I just can't get it to work properly.
edit:
I think I found the solution:
bool MyTableWidget::focusNextPrevChild( bool next )
{
std::cout << "focusNextPrev " << qPrintable(objectName()) << std::endl;
if( next )
{
if( currentColumn() == m_model->columnCount() - 1 )
return QWidget::focusNextPrevChild(next);
return false;
}
else
{
if( currentColumn() == 0 )
return QWidget::focusNextPrevChild(next);
return false;
}
return QTableWidget::focusNextPrevChild(next);
}
void MyTableWidget::focusInEvent( QFocusEvent * e)
{
switch( e->reason() )
{
case Qt::BacktabFocusReason:
cellWidget(0, m_model->columns().count()-1)->setFocus();
break;
case Qt::TabFocusReason:
default:
cellWidget(0,0)->setFocus();
break;
}
}
As far as I checked it does work, though I'm not sure it's entirely correct.
I have a few MyTableWidgets (which inherit from QTableWidget) on the one tab. I modified QTableWidget so it would display in the first (and as for now only) row QLineEdits (to be precise it displays my class inheriting from QLineEdit) using QTableWidget::setCellWidget().
Now I would like to traverse through all MyTableWidgets on this tab.
If I have only a one column (so it's also the only cell in MyTableWidget) when I press Tab it goes to the next MyTableWidget (and I've reimplemented focusInEvent so the first cell in this MyTableWidget gets the focus). When I press Tab again and again it loops through all cells in the only row in this MyTableWidget, but never leaves MyTableWidget and I don't know where to change this behavior.
So my question is how to change QTableWidget so that after pressing Tab in the last cell it will go to the next widget not first cell (and shift+tab in the first cell to go to the previous widget)?
From all I read it appears I should do something by reimplementing focusNextPrevChild( bool), but I just can't get it to work properly.
edit:
I think I found the solution:
bool MyTableWidget::focusNextPrevChild( bool next )
{
std::cout << "focusNextPrev " << qPrintable(objectName()) << std::endl;
if( next )
{
if( currentColumn() == m_model->columnCount() - 1 )
return QWidget::focusNextPrevChild(next);
return false;
}
else
{
if( currentColumn() == 0 )
return QWidget::focusNextPrevChild(next);
return false;
}
return QTableWidget::focusNextPrevChild(next);
}
void MyTableWidget::focusInEvent( QFocusEvent * e)
{
switch( e->reason() )
{
case Qt::BacktabFocusReason:
cellWidget(0, m_model->columns().count()-1)->setFocus();
break;
case Qt::TabFocusReason:
default:
cellWidget(0,0)->setFocus();
break;
}
}
As far as I checked it does work, though I'm not sure it's entirely correct.