Results 1 to 4 of 4

Thread: Scrolling to selected cell within QTableWidget when selecting cell from other QTable

  1. #1
    Join Date
    Nov 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Scrolling to selected cell within QTableWidget when selecting cell from other QTable

    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 mechanicQtable.jpg

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Scrolling to selected cell within QTableWidget when selecting cell from other QTa


  3. #3
    Join Date
    Nov 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Scrolling to selected cell within QTableWidget when selecting cell from other QTa

    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...

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Scrolling to selected cell within QTableWidget when selecting cell from other QTa

    Maybe ths simple example will help:
    Qt Code:
    1. #include <QtGui>
    2. class TableWidget : public QTableWidget
    3. {
    4. Q_OBJECT
    5. public:
    6. TableWidget(int rows, int cols) : QTableWidget(rows, cols) {}
    7. public slots:
    8. void findItem(QTableWidgetItem *item, QTableWidgetItem */* */){
    9. QList<QTableWidgetItem *> list = findItems(item->text(),Qt::MatchExactly);
    10. QTableWidgetItem *foundItem = list[0];
    11. QModelIndex index =model()->index(foundItem->row(), foundItem->column());
    12. scrollTo(index, QAbstractItemView::PositionAtCenter);
    13. }
    14. };
    15. #include "main.moc"
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication a(argc, argv);
    20. QTableWidget tableWidget1(3, 1);
    21. tableWidget1.setItem(0,0,new QTableWidgetItem("Item #1"));
    22. tableWidget1.setItem(1,0,new QTableWidgetItem("Item #50"));
    23. tableWidget1.setItem(2,0,new QTableWidgetItem("Item #89"));
    24. TableWidget tableWidget2(100, 10);
    25. for(int i=0;i<100;++i)
    26. tableWidget2.setItem(i, qrand() % 10, new QTableWidgetItem(QString("Item #%1").arg(i+1)));
    27. QObject::connect(&tableWidget1, SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)),&tableWidget2, SLOT(findItem(QTableWidgetItem*,QTableWidgetItem*)));
    28. tableWidget1.show();
    29. tableWidget2.show();
    30. return a.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Border around selected cell in qtablewidget...
    By pyqt123 in forum Qt Programming
    Replies: 4
    Last Post: 16th July 2012, 14:55
  2. Replies: 2
    Last Post: 18th July 2011, 01:25
  3. Replies: 1
    Last Post: 7th December 2009, 19:56
  4. Highlighting a cell in QTable
    By sumsin in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2007, 07:28
  5. QTable - Cell Thickness
    By Solarity in forum Newbie
    Replies: 4
    Last Post: 16th March 2006, 20:15

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.