Results 1 to 5 of 5

Thread: QTableWidget index reading

  1. #1
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableWidget index reading

    Hi,

    I am working on some simple EPG for SETTOP and struck with some probelms.

    request to please review my code and let me know how can i over come problems.

    following is my requirement:


    As soon as i launch the guide

    1) selection should be on the first cell and should print row = 0 col =0, currently there is no selection--> how to select R0C0 Cell

    then
    2) as i navigate through epg it should print the cell index i.e row and col value as and when cell is highlighted
    currently the result is

    3)when ever i press enter getting correct index , but immediately after launching epg, getting row = -1 and col = -1 -->how to correct it

    Qt Code:
    1. #include "widget.h"
    2. #include <QKeyEvent>
    3. #include<QScrollBar>
    4.  
    5.  
    6. Widget::Widget(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9. epg = new epg_table;
    10. epg->setParent(this);
    11. epg->setRowCount(8);
    12. epg->setColumnCount(2);
    13. epg->horizontalHeader()->hide();
    14. epg->verticalHeader()->hide();
    15. epg->verticalScrollBar()->hide();
    16. epg->setColumnWidth(0,300);
    17. epg->setColumnWidth(1,400);
    18. epg->resize(700,500);
    19. epg->move(100,50);
    20. connect(this,SIGNAL(UPDOWN1()),epg,SLOT(findindex()));
    21. epg->installEventFilter(this);
    22. epg->construct();
    23.  
    24. }
    25.  
    26. epg_table::epg_table()
    27. {
    28. for(int row = 0; row < 8;row++) {
    29. tItem[row] = NULL;
    30. }
    31. service_name << "200 HBO" << "201 STAR WORLD" << "202 NEO" << "203 WB" << "204 START SPORTS" << "205 LOVE";
    32. service_name << "206 MGM" << "207 GEO"<<"300 STAR Movies" << "201 STAR Cricket" << "202 Sony" << "203 Kushi" << "204 STARTUtsav" << "205 LOVE";
    33. service_name << "203 PIX" << "204 SETMax";
    34.  
    35. }
    36.  
    37.  
    38. void epg_table::findindex()
    39. {
    40. qDebug()<<"SCol="<<currentColumn();
    41. qDebug()<<"SRow="<<currentRow();
    42. }
    43.  
    44. void epg_table::construct()
    45. {
    46. int row;
    47. int row_max=8;
    48.  
    49. QFont font;
    50. font.setPointSize(20);
    51. static int start = 0;
    52.  
    53. for(row = 0; row < row_max;row++){
    54.  
    55. if(!tItem[row]) {
    56. qDebug()<<"Allocating "<<row+1;
    57. tItem[row] = new QTableWidgetItem();
    58. setItem(row,0,tItem[row]);
    59. item(row,0)->setTextColor(Qt::red);
    60. item(row,0)->setFont(font);
    61. }
    62. }
    63.  
    64. for(row = 0; row < row_max;row++){
    65.  
    66. tItem[row]->setText(service_name[start]);
    67. qDebug()<<"Adding "<<start+1;
    68. start++;
    69. start= (start==16?0:start);
    70. }
    71.  
    72. setItem(0,1,new QTableWidgetItem("X-Men First Class 2:00PM - 3:45PM"));
    73. item(0,1)->setTextColor(Qt::red);
    74.  
    75. QPalette* palette = new QPalette();
    76. palette->setBrush(QPalette::Base,*(new QBrush(*(new QPixmap("bangles.jpg")))));
    77. setPalette(*palette);
    78. setFrameShape(QFrame::NoFrame);
    79. // setParent(this);
    80. setFocus();
    81. }
    82.  
    83. Widget::~Widget()
    84. {
    85.  
    86.  
    87. }
    88.  
    89. bool Widget::eventFilter(QObject *obj, QEvent *event)
    90. {
    91.  
    92. if (obj == epg && event->type() == QEvent::KeyPress) {
    93. QKeyEvent *ke = static_cast<QKeyEvent*>(event);
    94. switch(ke->key())
    95. {
    96. case Qt::Key_Enter:
    97. qDebug()<<"ECol="<<epg->currentColumn();
    98. qDebug()<<"ERow="<<epg->currentRow();
    99.  
    100. break;
    101.  
    102. case Qt::Key_Up:
    103.  
    104. qDebug()<<"UCol="<<epg->currentColumn();
    105. qDebug()<<"URow="<<epg->currentRow();
    106. emit UPDOWN1();
    107. break;
    108.  
    109. case Qt::Key_Down:
    110.  
    111. qDebug()<<"DCol="<<epg->currentColumn();
    112. qDebug()<<"DRow="<<epg->currentRow();
    113. emit UPDOWN1();
    114. break;
    115.  
    116. default:
    117. break;
    118. }
    119. return false;//moves the highlight up and down but does not give index properly
    120. // return true;//does not move the highlight anywhere and always returns row and col as zero
    121. // return QWidget::eventFilter(obj, event);
    122. }else {
    123.  
    124. //return QWidget::eventFilter(obj, event);
    125. return true;
    126. }
    127.  
    128. }
    To copy to clipboard, switch view to plain text mode 

    sample output for return false case
    at first
    DCol= -1
    DRow= -1
    1st down
    DCol= 0
    DRow= 0
    2nd down
    DCol= 0
    DRow= 1
    3rd down
    DCol= 0
    DRow= 2

    1st Up from 3rd down
    UCol= 0
    URow= 3
    2nd Up
    UCol= 0
    URow= 2
    3rd Up
    UCol= 0
    URow= 1
    #endif

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableWidget index reading

    They do not come much more obvious than QTableWidget::setCurrentCell(). The QTableWidget also provides a convenient signal when the current cell changes.

    BTW: You will want to revisit your non-use of widget layouts before your project is finished.
    Last edited by ChrisW67; 16th April 2014 at 00:24.

  3. #3
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget index reading

    Hi Chris,
    Thank you very much for your reply , and i really appreciate your help.

    I used following calls, now i am getting the values as expected , but any ways please suggest me if any other wrong implementation you find in code.
    I am not a regular user of Qt, so i first see functionality working rather than how it is done so may not cover all corner cases and obviously may not work all the time.

    you are saying something about Layouts , please let me know what is the problem with the code.

    Thanks again ...

    Qt Code:
    1. connect(this,SIGNAL(currentCellChanged(int,int,int,int)),this,SLOT(getCurrPrevIndex(int,int,int,int))); // in constructor
    2. setCurrentCell(0,0,QItemSelectionModel::SelectCurrent); // in construct fn of my class epg
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableWidget index reading

    You have a QWidget sub-class called Widget that presumably represents your UI. At construction time it creates a QTableWidget that it owns and contains by virtue of that onwership. You then manually size and position the table widget inside its parent. There is no ongoing connection between the size of the Widget and the size or position of the contained QTableWidget, so resizing one will not adjust the other. This is the purpose of Qt layouts. You should read Layout Management to see how Qt is designed to maintain the content of widgets in the face of resizing etc.

    If your EPG program will run only on a single fixed size screen then this might not be a concern.

  5. The following user says thank you to ChrisW67 for this useful post:

    PstdEr (16th April 2014)

  6. #5
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget index reading

    Thanks a lot Chris, your first suggestion solved my problem.
    i will surely look into layouts and change my application soon..

Similar Threads

  1. How to find out row index of a QTableWidget.
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2012, 16:32
  2. pyqt4 reading values from rows of QTableWidget
    By stephen76 in forum Newbie
    Replies: 2
    Last Post: 10th January 2011, 10:55
  3. Spread of reading data with QTableWidget and QTableView
    By xiongxiongchuan in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2010, 10:17
  4. Reading/writing QTableWidget
    By Salazaar in forum Newbie
    Replies: 15
    Last Post: 23rd June 2007, 14:20
  5. reading and writing data from a QTableWidget
    By zorro68 in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2007, 21:51

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.