PDA

View Full Version : QTableWidget index reading



PstdEr
15th April 2014, 22:19
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



#include "widget.h"
#include <QKeyEvent>
#include<QScrollBar>


Widget::Widget(QWidget *parent)
: QWidget(parent)
{
epg = new epg_table;
epg->setParent(this);
epg->setRowCount(8);
epg->setColumnCount(2);
epg->horizontalHeader()->hide();
epg->verticalHeader()->hide();
epg->verticalScrollBar()->hide();
epg->setColumnWidth(0,300);
epg->setColumnWidth(1,400);
epg->resize(700,500);
epg->move(100,50);
connect(this,SIGNAL(UPDOWN1()),epg,SLOT(findindex( )));
epg->installEventFilter(this);
epg->construct();

}

epg_table::epg_table()
{
for(int row = 0; row < 8;row++) {
tItem[row] = NULL;
}
service_name << "200 HBO" << "201 STAR WORLD" << "202 NEO" << "203 WB" << "204 START SPORTS" << "205 LOVE";
service_name << "206 MGM" << "207 GEO"<<"300 STAR Movies" << "201 STAR Cricket" << "202 Sony" << "203 Kushi" << "204 STARTUtsav" << "205 LOVE";
service_name << "203 PIX" << "204 SETMax";

}


void epg_table::findindex()
{
qDebug()<<"SCol="<<currentColumn();
qDebug()<<"SRow="<<currentRow();
}

void epg_table::construct()
{
int row;
int row_max=8;

QFont font;
font.setPointSize(20);
static int start = 0;

for(row = 0; row < row_max;row++){

if(!tItem[row]) {
qDebug()<<"Allocating "<<row+1;
tItem[row] = new QTableWidgetItem();
setItem(row,0,tItem[row]);
item(row,0)->setTextColor(Qt::red);
item(row,0)->setFont(font);
}
}

for(row = 0; row < row_max;row++){

tItem[row]->setText(service_name[start]);
qDebug()<<"Adding "<<start+1;
start++;
start= (start==16?0:start);
}

setItem(0,1,new QTableWidgetItem("X-Men First Class 2:00PM - 3:45PM"));
item(0,1)->setTextColor(Qt::red);

QPalette* palette = new QPalette();
palette->setBrush(QPalette::Base,*(new QBrush(*(new QPixmap("bangles.jpg")))));
setPalette(*palette);
setFrameShape(QFrame::NoFrame);
// setParent(this);
setFocus();
}

Widget::~Widget()
{


}

bool Widget::eventFilter(QObject *obj, QEvent *event)
{

if (obj == epg && event->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent*>(event);
switch(ke->key())
{
case Qt::Key_Enter:
qDebug()<<"ECol="<<epg->currentColumn();
qDebug()<<"ERow="<<epg->currentRow();

break;

case Qt::Key_Up:

qDebug()<<"UCol="<<epg->currentColumn();
qDebug()<<"URow="<<epg->currentRow();
emit UPDOWN1();
break;

case Qt::Key_Down:

qDebug()<<"DCol="<<epg->currentColumn();
qDebug()<<"DRow="<<epg->currentRow();
emit UPDOWN1();
break;

default:
break;
}
return false;//moves the highlight up and down but does not give index properly
// return true;//does not move the highlight anywhere and always returns row and col as zero
// return QWidget::eventFilter(obj, event);
}else {

//return QWidget::eventFilter(obj, event);
return true;
}

}


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

ChrisW67
15th April 2014, 23:24
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.

PstdEr
16th April 2014, 05:28
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 ...




connect(this,SIGNAL(currentCellChanged(int,int,int ,int)),this,SLOT(getCurrPrevIndex(int,int,int,int) )); // in constructor
setCurrentCell(0,0,QItemSelectionModel::SelectCurr ent); // in construct fn of my class epg

ChrisW67
16th April 2014, 06:44
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.

PstdEr
16th April 2014, 22:37
Thanks a lot Chris, your first suggestion solved my problem.
i will surely look into layouts and change my application soon..