PDA

View Full Version : Scrolling to selected cell within QTableWidget when selecting cell from other QTable



jonh
8th November 2012, 21:35
I have two basic QTableWidgets. One is basically a one column list with names (table1). The other is a table with manyyyy rows and mannnny columns (table2). Currently I have it programmed to highlight a table2 cell if the text in that cell matches the name from the list in table1 that is clicked. All is fine and this works great. Since table2 has thousands of rows, I want it to not only highlight the correct cell, but then scroll to it and make it visible in table2. I have tried basically everything, I am somewhat of a beginner at this...any ideas?

I attached a picture. The left table is the list (clearly), the right table is the large table with many cells.

When I click on say "pin8" in table1, which isn't visible right now in table 2(cause its in column 9), I want it to scroll to the cell that has "pin8" and have it at least be seen if not centered.

Thank you for any help in advance...


HIGGZ BOSON
the quantum mechanic8398

norobro
8th November 2012, 23:28
Maybe use: QTableWidget::scrollToItem()

jonh
9th November 2012, 16:14
I have tried to implement that function but with no luck....Do you have a sample of it being used where it works correctly that I can look at? There are a lot of scroll-like capabilities yet none really seemed to match what I was trying to do. I tried to create a new QTableWidgetItem that related to the text in the cells, then tried to use scrollToItem to find the particular item when its text was clicked in table1 and scroll to it in table2...no luck...

norobro
9th November 2012, 18:55
Maybe ths simple example will help:
#include <QtGui>
class TableWidget : public QTableWidget
{
Q_OBJECT
public:
TableWidget(int rows, int cols) : QTableWidget(rows, cols) {}
public slots:
void findItem(QTableWidgetItem *item, QTableWidgetItem */* */){
QList<QTableWidgetItem *> list = findItems(item->text(),Qt::MatchExactly);
QTableWidgetItem *foundItem = list[0];
QModelIndex index =model()->index(foundItem->row(), foundItem->column());
scrollTo(index, QAbstractItemView::PositionAtCenter);
}
};
#include "main.moc"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget tableWidget1(3, 1);
tableWidget1.setItem(0,0,new QTableWidgetItem("Item #1"));
tableWidget1.setItem(1,0,new QTableWidgetItem("Item #50"));
tableWidget1.setItem(2,0,new QTableWidgetItem("Item #89"));
TableWidget tableWidget2(100, 10);
for(int i=0;i<100;++i)
tableWidget2.setItem(i, qrand() % 10, new QTableWidgetItem(QString("Item #%1").arg(i+1)));
QObject::connect(&tableWidget1, SIGNAL(currentItemChanged(QTableWidgetItem*,QTable WidgetItem*)),&tableWidget2, SLOT(findItem(QTableWidgetItem*,QTableWidgetItem*) ));
tableWidget1.show();
tableWidget2.show();
return a.exec();
}