Results 1 to 7 of 7

Thread: QTableWidget - Using top left corner cell

Hybrid View

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

    Default Re: QTableWidget - Using top left corner cell

    "QTableWidget NW corner header item?" at the bottom of this page under Similar Threads has some downloadable code in post #4.

    HTH

  2. #2
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 9 Times in 6 Posts

    Default Re: QTableWidget - Using top left corner cell

    Hi,

    Many thanks to all that replied and helped with this problem, just knowing that it is solvable helps a lot - Since the program I am translating from BB6 uses over a dozen instances of QTableWidget I guess it would be best to lash up new class by subbing QTableWidget and making it act like a BB6 StringGrid - I'll post a copy here later for other guys leaving the dark ages of BB6.

    SteveH

  3. #3
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 9 Times in 6 Posts

    Default Re: QTableWidget - Using top left corner cell

    Hi All,

    After much playing around I think I've got my solution. I'm using QTableWidget without the headers and using a QLabel in row/col zeros to simulate my own header. The following subroutine allows me to set a text string in any cell and insets a QTableWidgetItem if none has already set. It still needs a few frills adding for extra options but shows how to access the widget to set it's color and text.

    Qt Code:
    1. void MainWindow::setTableWidgetString(QTableWidget *tW, int row, int col, QString str)
    2. {
    3. QTableWidgetItem *dispItem ;
    4.  
    5. if((dispItem = tW->item(row, col)) == 0) { // if no item set then insert new one
    6. if((row == 0) || (col == 0)) {
    7. QLabel *L = new QLabel() ; // new header
    8. L->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    9. L->setText(str) ;
    10. L->setFrameShape(QFrame::StyledPanel);
    11. L->setAutoFillBackground(true);
    12. L->setBackgroundRole(QPalette::Window);
    13. P.setBrush(QPalette::Active, QPalette::Window, QBrush(Qt::lightGray));
    14. L->setPalette(P) ;
    15.  
    16. tW->setCellWidget(row, col, L) ;
    17. }
    18. else {
    19. QTableWidgetItem *newItem = new QTableWidgetItem(str) ; // new cell
    20. tW->setItem(row, col, newItem) ;
    21. newItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter) ;
    22. }
    23. }
    24.  
    25. else { // existing cell, update contents
    26. if((row == 0) || (col == 0)) {
    27. QLabel *L = qobject_cast<QLabel *> (tW->cellWidget(row, col)) ; // header
    28. L->setText(str);
    29. }
    30. else
    31. dispItem->setText(str) ; // cell
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 15th April 2010 at 21:55. Reason: changed [qtclass] to [code]

Similar Threads

  1. repaint a cell or row in QTableWidget
    By alphaqt in forum Newbie
    Replies: 6
    Last Post: 26th June 2012, 11:21
  2. QTableWidget NW corner header item?
    By McKee in forum Qt Programming
    Replies: 9
    Last Post: 30th May 2012, 23:44
  3. Replies: 1
    Last Post: 7th December 2009, 18:56
  4. Replies: 1
    Last Post: 7th September 2008, 10:47
  5. Replies: 7
    Last Post: 15th November 2007, 17:19

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
  •  
Qt is a trademark of The Qt Company.