Results 1 to 5 of 5

Thread: Focus of cells in a QTableWidget

  1. #1
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Focus of cells in a QTableWidget

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Focus of cells in a QTableWidget

    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()...
    J-P Nurmi

  3. #3
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Focus of cells in a QTableWidget

    Qt Code:
    1. void PSetConfiguration::keyPressEvent(QKeyEvent *e)
    2. {
    3.  
    4. switch (e->key())
    5. {
    6.  
    7. case ButtonKeyDefines::QKEY_UP:
    8. {
    9.  
    10. buttonUpPressed();
    11. // Method calls moveUp()
    12.  
    13. }
    14. break;
    15. case ButtonKeyDefines::QKEY_DOWN:
    16. {
    17.  
    18. buttonDownPressed();
    19. // Method calls moveDown()
    20.  
    21. }
    22. break;
    23. default:
    24. {
    25. }
    26. break;
    27.  
    28. }
    29.  
    30. }
    31.  
    32. void PSetConfigurationTable::moveUp()
    33. {
    34.  
    35. mRowIndex -= 1;
    36.  
    37. if (mRowIndex < 0)
    38. {
    39.  
    40. mRowIndex = rowCount() - 1 ;
    41.  
    42. }
    43.  
    44. setCurrentCell(mRowIndex, mColumnIndex);
    45.  
    46. }
    47.  
    48. void PSetConfigurationTable::moveDown()
    49. {
    50.  
    51. mRowIndex += 1;
    52.  
    53. if (mRowIndex >= rowCount())
    54. {
    55.  
    56. mRowIndex = 0;
    57.  
    58. }
    59.  
    60. setCurrentCell(mRowIndex, mColumnIndex);
    61.  
    62. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Focus of cells in a QTableWidget

    QAbstractItemView::moveCursor() is meant to be reimplemented if you want to customize keyboard navigation of an item view.
    J-P Nurmi

  5. #5
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Focus of cells in a QTableWidget

    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)?

    Qt Code:
    1. #include "PSetConfigurationTable.h"
    2.  
    3.  
    4. #include <QKeyEvent>
    5.  
    6.  
    7. //! PSetConfigurationTable - Constructor
    8. /*!
    9. * Set up the tablewidget; arguments are the size of row & columns
    10. */
    11. PSetConfigurationTable::PSetConfigurationTable(QWidget *parent,
    12. int xPos,
    13. int yPos,
    14. int width,
    15. int height)
    16. : QTableWidget(parent),
    17. mXPos(xPos),
    18. mYPos(yPos),
    19. mWidth(width),
    20. mHeight(height)
    21. {
    22.  
    23. setTable();
    24.  
    25. }
    26.  
    27. //! keyPressEvent
    28. /*!
    29. * Receives the keypress event when
    30. * the table widget is in focus.
    31. */
    32. void PSetConfigurationTable::keyPressEvent(QKeyEvent *e)
    33. {
    34.  
    35. if(e->key() == ButtonKeyDefines::QKEY_OK || e->key() == ButtonKeyDefines::QKEY_SOFTBUTTON_RIGHT)
    36. {
    37.  
    38. if ((currentRow() == 0 && currentColumn() == 1) || (currentRow() == 2 && currentColumn() == 1))
    39. {
    40.  
    41. QAbstractItemView::edit(currentIndex(), QAbstractItemView::EditKeyPressed, e);
    42.  
    43. }
    44.  
    45. }
    46.  
    47. /*!
    48. * Forward the event to the parent widget
    49. */
    50. QTableWidget::keyPressEvent(e);
    51.  
    52. }
    53.  
    54. QModelIndex PSetConfigurationTable::moveCursor(QAbstractItemView::CursorAction cursorAction,
    55. Qt::KeyboardModifiers /*modifiers*/)
    56. {
    57.  
    58. QModelIndex current = currentIndex();
    59.  
    60. switch (cursorAction)
    61. {
    62.  
    63. case MoveUp:
    64. if (current.row() > 0)
    65. {
    66.  
    67. current = model()->index(current.row() - 1, current.column(), rootIndex());
    68.  
    69. }
    70. else
    71. {
    72.  
    73. current = model()->index(0, current.column(), rootIndex());
    74.  
    75. }
    76. break;
    77. case MoveDown:
    78. if (current.row() < rows(current) - 1)
    79. {
    80.  
    81. current = model()->index(current.row() + 1, current.column(), rootIndex());
    82.  
    83. }
    84. else
    85. {
    86.  
    87. current = model()->index(rows(current) - 1, current.column(), rootIndex());
    88.  
    89. }
    90. break;
    91. default:
    92. break;
    93. }
    94.  
    95. viewport()->update();
    96.  
    97. return current;
    98.  
    99. }
    100.  
    101. int PSetConfigurationTable::rows(const QModelIndex &index) const
    102. {
    103.  
    104. return model()->rowCount(model()->parent(index));
    105.  
    106. }
    107.  
    108. void PSetConfigurationTable::moveUp()
    109. {
    110.  
    111. QModelIndex current = moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier);
    112. setCell(current.row(), current.column());
    113.  
    114. }
    115.  
    116. void PSetConfigurationTable::moveDown()
    117. {
    118.  
    119. QModelIndex current = moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier);
    120. setCell(current.row(), current.column());
    121.  
    122. }
    123.  
    124. //! showEvent
    125. /*!
    126. * If a parameter in a cell table is updated
    127. * the cell size is adjusted to the new value.
    128. */
    129. void PSetConfigurationTable::showEvent(QShowEvent*)
    130. {
    131.  
    132. adjustSize();
    133.  
    134. }
    135.  
    136. //! setTable
    137. /*!
    138. * Set the table size, add delegate,
    139. * initiate header items & set properties.
    140. */
    141. void PSetConfigurationTable::setTable()
    142. {
    143.  
    144. setRowCount(4);
    145. setColumnCount(3);
    146.  
    147. // #FFE600
    148. setStyleSheet("QTableWidget {selection-background-color : black } ");
    149.  
    150. pItemDelegate = new PSetItemDelegate;
    151. setItemDelegate(pItemDelegate);
    152.  
    153. setEditTriggers(QAbstractItemView::EditKeyPressed);
    154.  
    155. pItemHeaderA = new QTableWidgetItem();
    156. setHorizontalHeaderItem(0, pItemHeaderA);
    157.  
    158. pItemHeaderB = new QTableWidgetItem();
    159. setHorizontalHeaderItem(1, pItemHeaderB);
    160.  
    161. pItemHeaderC = new QTableWidgetItem();
    162. setHorizontalHeaderItem(2, pItemHeaderC);
    163.  
    164. horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    165. verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    166.  
    167. setGeometry( mXPos, mYPos, mWidth, mHeight);
    168.  
    169. setFrameShape(QFrame::NoFrame);
    170. horizontalHeader()->hide();
    171. verticalHeader()->hide();
    172. setShowGrid(false);
    173.  
    174. }
    175.  
    176. //! populateTable
    177. /*!
    178. * Add items & pset parameter data to the tablewidget.
    179. * If the cell is editable setData is used. It sets the
    180. * item's data for the given role to the specified value.
    181. */
    182. void PSetConfigurationTable::populateTable(QString torque, QString batchSize)
    183. {
    184.  
    185. /*!
    186. * Column Torque
    187. */
    188.  
    189. pItemTextTorque = new QTableWidgetItem();
    190. pItemTextTorque->setText("Torque");
    191. setItem(ROW_TORQUE, FIRST_COLUMN, pItemTextTorque);
    192.  
    193. pItemTorque = new QTableWidgetItem();
    194. pItemTorque->setData(Qt::EditRole, QVariant(torque));
    195. pItemTorque->setTextAlignment(Qt::AlignHCenter);
    196. setItem(ROW_TORQUE, SECOND_COLUMN, pItemTorque);
    197.  
    198. pItemTextNm = new QTableWidgetItem();
    199. pItemTextNm->setText("Nm");
    200. pItemTextNm->setTextAlignment(Qt::AlignLeft);
    201. setItem(ROW_TORQUE, THIRD_COLUMN, pItemTextNm);
    202.  
    203. /*!
    204. * Column Batch
    205. */
    206.  
    207. pItemTextBatch = new QTableWidgetItem();
    208. pItemTextBatch->setText("Batch");
    209. setItem(ROW_BATCH, FIRST_COLUMN, pItemTextBatch);
    210.  
    211. pItemBatch = new QTableWidgetItem();
    212. pItemBatch->setText("ON");
    213. pItemBatch->setTextAlignment(Qt::AlignHCenter);
    214. setItem(ROW_BATCH, SECOND_COLUMN, pItemBatch);
    215.  
    216. /*!
    217. * Column Batch size
    218. */
    219.  
    220. pItemTextBatchSize = new QTableWidgetItem();
    221. pItemTextBatchSize->setText("Batch Size");
    222. setItem(ROW_BATCHSIZE, FIRST_COLUMN, pItemTextBatchSize);
    223.  
    224. pItemBatchSize = new QTableWidgetItem(batchSize);
    225. pItemBatchSize->setData(Qt::EditRole, QVariant(batchSize));
    226. pItemBatchSize->setTextAlignment(Qt::AlignHCenter);
    227. setItem(ROW_BATCHSIZE, SECOND_COLUMN, pItemBatchSize);
    228.  
    229. /*!
    230. * Column Rehit
    231. */
    232.  
    233. pItemTextRehit = new QTableWidgetItem();
    234. pItemTextRehit->setText("Rehit");
    235. setItem(ROW_REHIT, FIRST_COLUMN, pItemTextRehit);
    236.  
    237. pItemRehit = new QTableWidgetItem();
    238. pItemRehit->setText("OFF");
    239. pItemRehit->setTextAlignment(Qt::AlignHCenter);
    240. setItem(ROW_REHIT, SECOND_COLUMN, pItemRehit);
    241.  
    242. setCell(0, 1);
    243.  
    244. }
    245.  
    246. //! setCell
    247. /*!
    248. * Sets the current cell to be the cell at position (row, column).
    249. */
    250. void PSetConfigurationTable::setCell(int row, int column)
    251. {
    252.  
    253. setCurrentCell(row, column);
    254.  
    255. }
    256.  
    257. void PSetConfigurationTable::setTorqueValue(QString torqueValue)
    258. {
    259.  
    260. QTableWidgetItem *torqueItem = item( 0, 1 );
    261. torqueItem->setText(torqueValue);
    262.  
    263. }
    264.  
    265. void PSetConfigurationTable::setBatchSizeValue(QString batchSizeValue)
    266. {
    267.  
    268. QTableWidgetItem *batchSizeItem = item( 2, 1 );
    269. batchSizeItem->setText(batchSizeValue);
    270.  
    271. }
    272.  
    273. //! getTorqueValue
    274. /*!
    275. * item(row, column) returns the item for the given row and column.
    276. * The items torque value is returned.
    277. */
    278. QString PSetConfigurationTable::getTorqueValue()
    279. {
    280.  
    281. QTableWidgetItem *torqueItem = item( 0, 1 );
    282. return torqueItem->text();
    283.  
    284. }
    285.  
    286. //! getBatchSizeValue
    287. /*!
    288. * item(row, column) returns the item for the given row and column.
    289. * The items batch size value is returned.
    290. */
    291. QString PSetConfigurationTable::getBatchSizeValue()
    292. {
    293.  
    294. QTableWidgetItem *batchSizeItem = item( 2, 1 );
    295. return batchSizeItem->text();
    296.  
    297. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableWidget, header cells margin
    By DpoHro in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2008, 00:38
  2. Force focus to a QTabWidget page's widget
    By thomaspu in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 07:54
  3. QTableWidget swapping rows with widgets in cells
    By dnnc in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2007, 15:11
  4. Replies: 3
    Last Post: 8th September 2006, 19:54
  5. QTableWidget with overlapping cells (span)
    By rhi in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2006, 19:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.