PDA

View Full Version : QTableWidget - Using top left corner cell



SteveH
5th March 2010, 19:45
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

user_mail07
5th March 2010, 21:42
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.

SteveH
5th March 2010, 22:25
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

user_mail07
5th March 2010, 23:24
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:



QStyleOptionHeader headerOption;
headerOption.init(yourTextBtn);
QStyle::State state = QStyle::State_None;
if (yourTextBtn->isEnabled())
state |= QStyle::State_Enabled;

headerOption.state = state;
headerOption.rect =yourTextBtn->rect(); //Corner of table widget

headerOption.text = yourTextBtn->text();

headerOption.position = QStyleOptionHeader::OnlyOneSection; //Only one header section..
QStylePainter painter(yourTextBtn);
painter.drawControl(QStyle::CE_Header, headerOption);

norobro
6th March 2010, 00:22
"QTableWidget NW corner header item?" at the bottom of this page under Similar Threads has some downloadable code in post #4.

HTH

SteveH
6th March 2010, 17:56
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

SteveH
12th March 2010, 19:33
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.


void MainWindow::setTableWidgetString(QTableWidget *tW, int row, int col, QString str)
{
QTableWidgetItem *dispItem ;

if((dispItem = tW->item(row, col)) == 0) { // if no item set then insert new one
if((row == 0) || (col == 0)) {
QLabel *L = new QLabel() ; // new header
L->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
L->setText(str) ;
L->setFrameShape(QFrame::StyledPanel);
L->setAutoFillBackground(true);
L->setBackgroundRole(QPalette::Window);
QPalette P ;
P.setBrush(QPalette::Active, QPalette::Window, QBrush(Qt::lightGray));
L->setPalette(P) ;

tW->setCellWidget(row, col, L) ;
}
else {
QTableWidgetItem *newItem = new QTableWidgetItem(str) ; // new cell
tW->setItem(row, col, newItem) ;
newItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter) ;
}
}

else { // existing cell, update contents
if((row == 0) || (col == 0)) {
QLabel *L = qobject_cast<QLabel *> (tW->cellWidget(row, col)) ; // header
L->setText(str);
}
else
dispItem->setText(str) ; // cell
}

}