Results 1 to 7 of 7

Thread: QTableWidget - Using top left corner cell

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

    Default QTableWidget - Using top left corner cell

    Hi all,

    Is there a way to place text in the top left corner of a QTableWidget ?
    Is there a way to make the top left cell appear with a raised frame like all the other header cells ?

    I've searched the forums back for a couple of years but can't find anything refering to this (another question - when using the forum search function how do you search for matches of all words in list - the faq says select the 'all words' option from the selector buttons but I could'nt find it)

    I'm trying to convert a huge Borland Builder 6 program to Qt and need to impliment the StringGrid widget of BB6. I could make a widget from scratch to give me this function but I would rather use the existing widgets even if it gives me initial problems in the translation - at least keeping the program updated will then be easier.

    Regards
    SteveH

  2. #2
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: QTableWidget - Using top left corner cell

    You can add QAbstractButton or pushButton as table corner button to QtableWidget. Then set the text to that push button or styled that button as raised frame. I am not sure if I understood your problem exactly. You want to display text as I have shown in attached image.
    Attached Images Attached Images

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

    Default Re: QTableWidget - Using top left corner cell

    Hi user_mail07,

    Yes, thats pretty much what I want to do but I can't seem to access the corner as all the item or header addresses are zero based, I tried using -1 to access the corner but either nothing happens or the program crashes.
    Obviously I am missing the correct access format

    I am looking now at turning off the headers and inserting a widget (label, styled panel ?) in all the row/col zeros.

    Regards
    SteveH

  4. #4
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: QTableWidget - Using top left corner cell

    Okay so I think to get corner of table widget you can use QStyleOptionHeader. I have used something similar in the past but trying to find that code. You can design abstractButton inside the eventFilter and then use QStyleOptionHeader to draw the text and button. Then install eventFilter to mainTableWidget. To get the text at table corner use something:


    Qt Code:
    1. QStyleOptionHeader headerOption;
    2. headerOption.init(yourTextBtn);
    3. QStyle::State state = QStyle::State_None;
    4. if (yourTextBtn->isEnabled())
    5. state |= QStyle::State_Enabled;
    6.  
    7. headerOption.state = state;
    8. headerOption.rect =yourTextBtn->rect(); //Corner of table widget
    9.  
    10. headerOption.text = yourTextBtn->text();
    11.  
    12. headerOption.position = QStyleOptionHeader::OnlyOneSection; //Only one header section..
    13. QStylePainter painter(yourTextBtn);
    14. painter.drawControl(QStyle::CE_Header, headerOption);
    To copy to clipboard, switch view to plain text mode 

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

    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

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

    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

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.