Results 1 to 7 of 7

Thread: QTableView switching currentIndex not performing as expected

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView switching currentIndex not performing as expected

    Hello all,
    I have composed a simple compilable example to demonstrate the following issue.
    When I click in the tableView, if I am in column 0 I want it to switch selection to column 1 ->This works ok
    I installed an EventFilter on the tableView and check for certain keys (Left, Right, Up, and Down) for table navigation, I catch them and emit a signal, which calls the same slot as table->SIGNAL(clicked(QModelIndex)) and this appears to get in the spot to move the selection, but it doesn't actually set the true currentIndex (at least not from what I see).

    Wondering if anyone out there could give me a hand with this, as it seems it should be very straightforward...
    file:main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    file: mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. table = new QTableView(this);
    7. model = new QStandardItemModel(this);
    8. table->setModel(model);
    9. table->installEventFilter(this);
    10.  
    11. //populate model with data...
    12. for(int i=0;i<5; i++)
    13. {
    14. for(int j=0; j<2; j++)
    15. {
    16. QString text = "row: "+QString::number(i) + "/col: "+QString::number(j);
    17. QStandardItem *item = new QStandardItem(QString(text));
    18. model->setItem(i,j,item);
    19. }
    20. }
    21. table->resizeRowsToContents();
    22. table->resizeColumnsToContents();
    23. table->setFixedSize(300,400);
    24. QVBoxLayout *vbl = new QVBoxLayout(this);
    25. vbl->addWidget(table,1);
    26. setLayout(vbl);
    27. setFixedSize(800,600);
    28. connect(table,SIGNAL(activated(QModelIndex)),this,SLOT(updateDesc(QModelIndex)));
    29. connect(table,SIGNAL(clicked(QModelIndex)),this,SLOT(updateDesc(QModelIndex)));
    30. connect(this,SIGNAL(updateFromArrow(QModelIndex)),this,SLOT(updateDesc(QModelIndex)));
    31. }
    32. bool MainWindow::eventFilter(QObject *o, QEvent *e)
    33. {
    34. if(o == table && e->type() == QEvent::KeyPress)
    35. {
    36. //key was pressed on param detail selection...
    37. int row = table->currentIndex().row();
    38. int col = table->currentIndex().column();
    39. int rowCnt = model->rowCount()-1;
    40. int colCnt = model->columnCount()-1;
    41. QKeyEvent *keyEv = static_cast <QKeyEvent*> (e);
    42. if(keyEv->key() == Qt::Key_Left)
    43. {
    44. qDebug()<<"Key LEFT";
    45. if(col != 0)
    46. emit updateFromArrow(model->index(row,col-1));
    47. else
    48. emit updateFromArrow(model->index(row,col));
    49. }//end if key left
    50. else if(keyEv->key() == Qt::Key_Right)
    51. {
    52. if(col != colCnt)
    53. emit updateFromArrow(model->index(row,col+1));
    54. else
    55. emit updateFromArrow(model->index(row,col));
    56. }//end keyright
    57. else if(keyEv->key() == Qt::Key_Up)
    58. {
    59. if(row != 0)
    60. emit updateFromArrow(model->index(row-1,col));
    61. else
    62. emit updateFromArrow(model->index(row,col));
    63. }//end if key up
    64. else if(keyEv->key() == Qt::Key_Down)
    65. {
    66. if(row != rowCnt)
    67. emit updateFromArrow(model->index(row+1,col));
    68. else
    69. emit updateFromArrow(model->index(row,col));
    70. }//end if key down
    71. }
    72. return false;
    73.  
    74. }
    75. void MainWindow::updateDesc(QModelIndex mi)
    76. {
    77. qDebug()<<"tree Clicked: row:"<<mi.row()<<"col:"<<mi.column()<<"curindxCol:"<<table->currentIndex().column();
    78. QModelIndex m = model->index(mi.row(),mi.column());
    79. if(mi.column() == 0) //if we are in first column, then jump to 2nd column <-works on clicking, not on keypress from eventFilter
    80. {
    81. qDebug()<<"Changing index, in col 0 for param";
    82. m = model->index(mi.row(),1);
    83. //table->selectionModel()->select(table->model()->index(mi.row(),1), QItemSelectionModel::ClearAndSelect);
    84. table->setCurrentIndex(m);
    85.  
    86. }
    87.  
    88. }
    89.  
    90. MainWindow::~MainWindow()
    91. {
    92.  
    93. }
    To copy to clipboard, switch view to plain text mode 
    finally file: mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QtGui>
    6.  
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14. ~MainWindow();
    15. QTableView *table;
    16. signals:
    17. void updateFromArrow(QModelIndex);
    18. public slots:
    19. void updateDesc(QModelIndex);
    20.  
    21. protected:
    22. bool eventFilter(QObject *o, QEvent *e);
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Thanks to all who can help with this!
    AlphaWolfXV - and Have a happy new year!
    Attached Files Attached Files

Similar Threads

  1. Invalid QSqlRecord after performing another query to DB
    By crew4ok in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2012, 01:18
  2. Replies: 6
    Last Post: 5th May 2010, 23:25
  3. Replies: 3
    Last Post: 12th December 2009, 08:51
  4. QDir entryList performing slowly
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2009, 16:21
  5. QTableView not working as expected.
    By junxuan in forum Qt Programming
    Replies: 7
    Last Post: 30th July 2009, 08:17

Tags for this Thread

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.