PDA

View Full Version : Focus of cells in a QTableWidget



SailinShoes
26th May 2008, 18:33
I have some problems with a qtablewidget. It has 4 rows and 3 columns.
It is only possible to move up and down in the cells 0:1, 1:1, 2:1 & 3:1.
The cells 0:1 and 2:1 is editable through spinbox delegates.

To have control of what cell is currently set i keep track via index of row
and index of column (to be able to wrap) and after each move the indexes is updated and i call setCurrentIndex().

The indexes always set correct when i use the up & down keys, but the focus now and then suddenly jumps to the wrong cell?

I have debugged this problem but I cant find anything wrong, there has to be
some feature of qt (qtablewidget?delegates?) I have not understood?

My code is running on an embedded system with qtopia.

What to do?

jpn
26th May 2008, 18:40
Could you show some code? Where do you call setCurrentIndex()? Preferably implement a simple one file test application which shows the problem with similar table widget and nothing else. Notice QAbstractItemView::moveCursor() and QWidget::focusNextPrevChild()...

SailinShoes
27th May 2008, 06:43
void PSetConfiguration::keyPressEvent(QKeyEvent *e)
{

switch (e->key())
{

case ButtonKeyDefines::QKEY_UP:
{

buttonUpPressed();
// Method calls moveUp()

}
break;
case ButtonKeyDefines::QKEY_DOWN:
{

buttonDownPressed();
// Method calls moveDown()

}
break;
default:
{
}
break;

}

}

void PSetConfigurationTable::moveUp()
{

mRowIndex -= 1;

if (mRowIndex < 0)
{

mRowIndex = rowCount() - 1 ;

}

setCurrentCell(mRowIndex, mColumnIndex);

}

void PSetConfigurationTable::moveDown()
{

mRowIndex += 1;

if (mRowIndex >= rowCount())
{

mRowIndex = 0;

}

setCurrentCell(mRowIndex, mColumnIndex);

}

jpn
27th May 2008, 10:56
QAbstractItemView::moveCursor() is meant to be reimplemented if you want to customize keyboard navigation of an item view.

SailinShoes
9th June 2008, 08:19
I use the MoveCursor method but the problem is still there. After I have closed the
delegate editor, and start to press up/down the index of row and column is correct
but the "highlighted cell" can be moved up/down and is then locked for a few presses
and after a few presses jumps to the correct cell (to the row & column index cell)?





#include "PSetConfigurationTable.h"


#include <QKeyEvent>


//! PSetConfigurationTable - Constructor
/*!
* Set up the tablewidget; arguments are the size of row & columns
*/
PSetConfigurationTable::PSetConfigurationTable(QWi dget *parent,
int xPos,
int yPos,
int width,
int height)
: QTableWidget(parent),
mXPos(xPos),
mYPos(yPos),
mWidth(width),
mHeight(height)
{

setTable();

}

//! keyPressEvent
/*!
* Receives the keypress event when
* the table widget is in focus.
*/
void PSetConfigurationTable::keyPressEvent(QKeyEvent *e)
{

if(e->key() == ButtonKeyDefines::QKEY_OK || e->key() == ButtonKeyDefines::QKEY_SOFTBUTTON_RIGHT)
{

if ((currentRow() == 0 && currentColumn() == 1) || (currentRow() == 2 && currentColumn() == 1))
{

QAbstractItemView::edit(currentIndex(), QAbstractItemView::EditKeyPressed, e);

}

}

/*!
* Forward the event to the parent widget
*/
QTableWidget::keyPressEvent(e);

}

QModelIndex PSetConfigurationTable::moveCursor(QAbstractItemVi ew::CursorAction cursorAction,
Qt::KeyboardModifiers /*modifiers*/)
{

QModelIndex current = currentIndex();

switch (cursorAction)
{

case MoveUp:
if (current.row() > 0)
{

current = model()->index(current.row() - 1, current.column(), rootIndex());

}
else
{

current = model()->index(0, current.column(), rootIndex());

}
break;
case MoveDown:
if (current.row() < rows(current) - 1)
{

current = model()->index(current.row() + 1, current.column(), rootIndex());

}
else
{

current = model()->index(rows(current) - 1, current.column(), rootIndex());

}
break;
default:
break;
}

viewport()->update();

return current;

}

int PSetConfigurationTable::rows(const QModelIndex &index) const
{

return model()->rowCount(model()->parent(index));

}

void PSetConfigurationTable::moveUp()
{

QModelIndex current = moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier);
setCell(current.row(), current.column());

}

void PSetConfigurationTable::moveDown()
{

QModelIndex current = moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier);
setCell(current.row(), current.column());

}

//! showEvent
/*!
* If a parameter in a cell table is updated
* the cell size is adjusted to the new value.
*/
void PSetConfigurationTable::showEvent(QShowEvent*)
{

adjustSize();

}

//! setTable
/*!
* Set the table size, add delegate,
* initiate header items & set properties.
*/
void PSetConfigurationTable::setTable()
{

setRowCount(4);
setColumnCount(3);

// #FFE600
setStyleSheet("QTableWidget {selection-background-color : black } ");

pItemDelegate = new PSetItemDelegate;
setItemDelegate(pItemDelegate);

setEditTriggers(QAbstractItemView::EditKeyPressed) ;

pItemHeaderA = new QTableWidgetItem();
setHorizontalHeaderItem(0, pItemHeaderA);

pItemHeaderB = new QTableWidgetItem();
setHorizontalHeaderItem(1, pItemHeaderB);

pItemHeaderC = new QTableWidgetItem();
setHorizontalHeaderItem(2, pItemHeaderC);

horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);

setGeometry( mXPos, mYPos, mWidth, mHeight);

setFrameShape(QFrame::NoFrame);
horizontalHeader()->hide();
verticalHeader()->hide();
setShowGrid(false);

}

//! populateTable
/*!
* Add items & pset parameter data to the tablewidget.
* If the cell is editable setData is used. It sets the
* item's data for the given role to the specified value.
*/
void PSetConfigurationTable::populateTable(QString torque, QString batchSize)
{

/*!
* Column Torque
*/

pItemTextTorque = new QTableWidgetItem();
pItemTextTorque->setText("Torque");
setItem(ROW_TORQUE, FIRST_COLUMN, pItemTextTorque);

pItemTorque = new QTableWidgetItem();
pItemTorque->setData(Qt::EditRole, QVariant(torque));
pItemTorque->setTextAlignment(Qt::AlignHCenter);
setItem(ROW_TORQUE, SECOND_COLUMN, pItemTorque);

pItemTextNm = new QTableWidgetItem();
pItemTextNm->setText("Nm");
pItemTextNm->setTextAlignment(Qt::AlignLeft);
setItem(ROW_TORQUE, THIRD_COLUMN, pItemTextNm);

/*!
* Column Batch
*/

pItemTextBatch = new QTableWidgetItem();
pItemTextBatch->setText("Batch");
setItem(ROW_BATCH, FIRST_COLUMN, pItemTextBatch);

pItemBatch = new QTableWidgetItem();
pItemBatch->setText("ON");
pItemBatch->setTextAlignment(Qt::AlignHCenter);
setItem(ROW_BATCH, SECOND_COLUMN, pItemBatch);

/*!
* Column Batch size
*/

pItemTextBatchSize = new QTableWidgetItem();
pItemTextBatchSize->setText("Batch Size");
setItem(ROW_BATCHSIZE, FIRST_COLUMN, pItemTextBatchSize);

pItemBatchSize = new QTableWidgetItem(batchSize);
pItemBatchSize->setData(Qt::EditRole, QVariant(batchSize));
pItemBatchSize->setTextAlignment(Qt::AlignHCenter);
setItem(ROW_BATCHSIZE, SECOND_COLUMN, pItemBatchSize);

/*!
* Column Rehit
*/

pItemTextRehit = new QTableWidgetItem();
pItemTextRehit->setText("Rehit");
setItem(ROW_REHIT, FIRST_COLUMN, pItemTextRehit);

pItemRehit = new QTableWidgetItem();
pItemRehit->setText("OFF");
pItemRehit->setTextAlignment(Qt::AlignHCenter);
setItem(ROW_REHIT, SECOND_COLUMN, pItemRehit);

setCell(0, 1);

}

//! setCell
/*!
* Sets the current cell to be the cell at position (row, column).
*/
void PSetConfigurationTable::setCell(int row, int column)
{

setCurrentCell(row, column);

}

void PSetConfigurationTable::setTorqueValue(QString torqueValue)
{

QTableWidgetItem *torqueItem = item( 0, 1 );
torqueItem->setText(torqueValue);

}

void PSetConfigurationTable::setBatchSizeValue(QString batchSizeValue)
{

QTableWidgetItem *batchSizeItem = item( 2, 1 );
batchSizeItem->setText(batchSizeValue);

}

//! getTorqueValue
/*!
* item(row, column) returns the item for the given row and column.
* The items torque value is returned.
*/
QString PSetConfigurationTable::getTorqueValue()
{

QTableWidgetItem *torqueItem = item( 0, 1 );
return torqueItem->text();

}

//! getBatchSizeValue
/*!
* item(row, column) returns the item for the given row and column.
* The items batch size value is returned.
*/
QString PSetConfigurationTable::getBatchSizeValue()
{

QTableWidgetItem *batchSizeItem = item( 2, 1 );
return batchSizeItem->text();

}