Control highlight of QTableWidget
-
Hi,
I am using the following the code to select the and print the value from a selected cell
@int col = table->currentColumn(); int row = table->currentRow(); QTableWidgetItem *T1= table->item(row,col); qDebug()<<T1->text();
@
But the problem is, as soon as i press enter the highlighting is changed to next column cell and i am getting wrong values.
for example I am pressing enter on row = 0 col = 0 , but highlight is happening on row = 0 , col =1
hence wrong result.Is there any way to control the highlighting/handling this scenario to get the correct values.
Thanks in Advance.
-
May be the above question is not clear/not sufficient to understand the problem.
the below is the code sample example for my scenario
@
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
epg = new QTableWidget(4,2,this);
epg->horizontalHeader()->hide();
epg->verticalHeader()->hide();
epg->move(100,50);
setMinimumSize(500,300);
}Widget::~Widget()
{}
void Widget::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Up:
qDebug()<<"Up Pressed";
break;case Qt::Key_Down: qDebug()<<"Down Pressed"; break; case Qt::Key_Enter: qDebug()<<"Enter Pressed"; break; }
}
@As i am working on some target board the keys are not exactly Qt keys , some other keys are mapped to Qt keys to perform the action.
now my problem is , when ever i send enter key the highlighting is changed to next column , because which i am getting wrong values as explained above.i have few doubts here:
when ever i navigate through table why am i not getting the "Up Pressed","Down Pressed" , but getting after reaching the end cells.is there any way to receive the keys for table it self instead of QWidget.
-
I am trying the following way to get the keys to only table:
The keys are received in the table only , but the navigation is not happening, that means , the highlight is always the first cell i.e 0 , 0 only,how can i achieve the navigation and yet to receive keys for only table.
please help.... , i need to find a solution for this...
@
void Widget::keyPressEvent(QKeyEvent *event)
{
qDebug()<<"Widget";
switch(event->key())
{case Qt::Key_Up: qDebug()<<"Up Pressed"; break; case Qt::Key_Down: qDebug()<<"Down Pressed"; break; case Qt::Key_Enter: case Qt::Key_Return: qDebug()<<"Enter Pressed"; break; default: epg->keyPressEvent(event); break; }
}
//#if 0
void epg_table::keyPressEvent(QKeyEvent *event)
{
qDebug()<<"EPG";switch(event->key()) { case Qt::Key_Up: qDebug()<<"Up Pressed"; break; case Qt::Key_Down: qDebug()<<"Down Pressed"; break; case Qt::Key_Enter: case Qt::Key_Return: qDebug()<<"Enter Pressed"; break; }
}
//#endif@
-
Hi,
If you want to get the key events without subclassing QTableWidget you can use installEventFilter.
Hope it helps
-
My problem is the highlight is changed to next column before it is reading data.How to stop the highlight moving to next column ?
-
I am trying to get atleast the signals for QTableWidgets , but which are also not working
@
#include "widget.h"
#include<QDebug>
#include<QTableWidget>
#include<QHeaderView>
#include<QScrollBar>
#include<QHBoxLayout>
#include<QPushButton>
#include<QFont>
#include<QLabel>
#include<QTimer>#define ROW_HEIGHT 30
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
table = new epg_table;
table->setParent(this);
table->setFixedSize(700,200);
connect(table, SIGNAL(cellActivated( int row, int column )),this, SLOT(detectRowSelection(int row, int column)) );
connect(table, SIGNAL(cellClicked( int row, int column )),this, SLOT(detectRowSelection(int row, int column)) );
connect(table, SIGNAL(cellEntered( int row, int column )),this, SLOT(detectRowSelection(int row, int column)) );
}epg_table::epg_table()
{
width = 1280;
height = 720;epg_tbl_w = width - 100; epg_tbl_h = 8 * ROW_HEIGHT; chnl_col_w = epg_tbl_w - 900; evnt_col_w = epg_tbl_w - chnl_col_w-100; construct(); connect(this, SIGNAL(cellActivated( int row, int column )),this, SLOT(detectRowSelection(int row, int column)) );
}
void epg_table::detectRowSelection(int row, int col)
{
qDebug()<<"from detectRowSelection";
qDebug()<<"row ="<<row<<"col"<<col;
selectionChanged()
}void epg_table::construct()
{
int row;
int row_max=8;QFont font; font.setPointSize(20); QStringList service_name ; service_name << "200 HBO" << "201 STAR WORLD" << "202 NEO" << "203 WB" << "204 START SPORTS" << "205 LOVE"; service_name << "206 MGM" << "207 GEO"; setRowCount(8); setColumnCount(2); for(row = 0; row < row_max;row++){ setItem(row,0,new QTableWidgetItem(service_name[row])); item(row,0)->setTextColor(Qt::white); item(row,0)->setFont(font); } setItem(0,1,new QTableWidgetItem("X-Men First Class 2:00PM - 3:45PM")); item(0,1)->setTextColor(Qt::white); installEventFilter(this); disable_headers(); adjust_sizes(); move(50,180); QPalette* palette = new QPalette(); palette->setBrush(QPalette::Base,*(new QBrush(*(new QPixmap("./perfect.jpg"))))); setPalette(*palette); setFrameShape(QFrame::NoFrame); // setParent(this); setFocus();
}
void epg_table::disable_headers()
{this->verticalHeader()->setVisible(false); this->horizontalHeader()->setVisible(false); this->verticalScrollBar()->setVisible(false); this->horizontalScrollBar()->setVisible(false);
}
void epg_table:: adjust_sizes()
{
resize(epg_tbl_w,epg_tbl_h);
setColumnWidth(0,chnl_col_w);
setColumnWidth(1,evnt_col_w);
setColumnWidth(2,evnt_col_w);
}bool epg_table::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug("Ate key press %d", keyEvent->key());
return false;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}#if 0
void Widget::keyPressEvent(QKeyEvent *event)
{
table->keyPressEvent(event);
}void epg_table::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Left:
qDebug()<<"Left Key Pressed";
break;case Qt::Key_Right: qDebug()<<"Right Key Pressed"; break; case Qt::Key_Enter: case Qt::Key_Return: qDebug()<<"Enter/Return Key Pressed"; break; }
}
#endif
epg_table::~epg_table()
{}
Widget::~Widget()
{}
@ -
What do you want to achieve with the enter key ?
-
I am launching the video upon enter by taking the values from the cell on which i press enter, but the problem was as soon as i press enter , the highlight of cell is moving to next column and reading the values from that which i don't want.
I tried eventFilter example as suggested by you on some sample program below, now i don't get that problem , but i got a new problem about the indexes.
when i first launch the application i am getting row and col index as -1 , when i am in the second cell i.e [R1,C0] i am getting the indexes as R0, C0 and when i navigate through rows i am not getting the correct values at all
and also upon launch it is not selecting the cell [R0, C0]
can you please point me out with some small example or reference
on how to select the first cell upon launch , and my indices should be read correctly.
I am giving the complete program so that you can compile and see the probelm for your self.widget.h
@
#ifndef WIDGET_H
#define WIDGET_H#include <QtGui/QWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QDebug>class epg_table :public QTableWidget
{
Q_OBJECT
public:
epg_table();
void construct();
private:
QTableWidgetItem *tItem[8];
protected:
bool eventFilter(QObject *obj, QEvent *event);};
class Widget : public QWidget
{
Q_OBJECTpublic:
Widget(QWidget *parent = 0);
~Widget();private:
epg_table *epg;
};#endif // WIDGET_H
@widget.cpp
@
#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);
epg->construct();}
epg_table::epg_table()
{
for(int row = 0; row < 8;row++){
tItem[row] = NULL;
}
}void epg_table::construct()
{
int row;
int row_max=8;QFont font; font.setPointSize(20); static int start = 0; QStringList service_name ; 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"; setRowCount(8); setColumnCount(2); 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 < 8;row++){ tItem[row]->setText(service_name[start]); qDebug()<<"Adding "<<start+1; QTableWidgetItem(service_name[row])); 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); installEventFilter(this); QPalette* palette = new QPalette(); palette->setBrush(QPalette::Base,*(new QBrush(*(new QPixmap("bangles.jpg"))))); setPalette(*palette); setFrameShape(QFrame::NoFrame); setFocus(); int ccol = currentColumn(); int crow = currentRow(); qDebug()<<"Crow = "<<crow<<"Ccol="<<ccol;
}
Widget::~Widget()
{}
bool epg_table::eventFilter(QObject *obj, QEvent *event)
{if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); qDebug("key press %d", keyEvent->key()); switch(keyEvent->key()){ case Qt::Key_Up: { qDebug("UP"); int col = currentColumn(); int row = currentRow(); qDebug()<<"row = "<<row<<"col="<<col; if(row == 0) construct(); } break; case Qt::Key_Down: qDebug("Down");{ int col = currentColumn(); int row = currentRow(); qDebug()<<"row = "<<row<<"col="<<col; if(row == 7) construct(); } break; case Qt::Key_Enter: qDebug()<<"Enter Key"; break; default: break; } return false; }
}
@
main.cpp
@
#include <QtGui/QApplication>
#include "widget.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();return a.exec();
}
@