Background Image in QTableWidget
Does anyone know how to place either text or a background image in an empty table.
I am using a QTableWidget and if the table is empty I must direct the user to a configuration dialog (but not take them there automatically).
I am using Qt 4.2.2 and can't find anything about background images in QTableWidget.
Based on my Qt experience I don't believe placing text in an empty table is possible unless I add a row and the text was placed in a cell, but this is not what is required.
Is this possible or must I find a different solution?
Re: Background Image in QTableWidget
You can set a background image using QWidget::setPalette() and setting a pixmap as a brush for the Base role. As for the text, you can apply an event filter on the table viewport or subclass the table and intercept QEvent::Paint or override paintEvent respectively and paint your text where you want it. Or you can even put a QLabel on the table (as the child of the viewport).
Re: Background Image in QTableWidget
Thanks wysota, I'll try your suggestions.
Re: Background Image in QTableWidget
wysota, I though I would try what appeared to be the easiest possibilities first.
Quote:
You can set a background image using QWidget::setPalette() and setting a pixmap as a brush for the Base role.
The following displayed the image under the horizontal header but not on the white table surface (I could see it extend to the end of the table where the header was not drawn). Is there something I'm missing here?
Code:
m_pTable->setHorizontalHeaderLabels( sLabels );
QPixmap px
( ":/images/ShowLogSplash.png" );
m_pTable->setPalette( palette );
Quote:
Or you can even put a QLabel on the table (as the child of the viewport).
I used the following code to try to implement this but I feel I'm missing the point you were making. It shows nothing on the table surface. Is this remotely what you were suggesting?
Code:
m_pTable->setHorizontalHeaderLabels( sLabels );
QWidget* viewport
= m_pTable
->viewport
();
QLabel lbl
( "QLable Text in a Viewport" );
vLayout->addWidget( &lbl );
viewport->setLayout( vLayout );
I'll now look at the event filter suggestion you made, although that looks more complicated than I wanted to try.
Thanks for you help.
Re: Background Image in QTableWidget
The palette has to be set on the viewport not on the widget itself.
Re: Background Image in QTableWidget
Quote:
Originally Posted by
wysota
The palette has to be set on the viewport not on the widget itself.
Setting the palette on the viewport of the table displays no hint of the image. Does the following code express what you are referring to?
Code:
m_pTable->setHorizontalHeaderLabels( sLabels );
QPixmap px
( ":/images/ShowLogSplash.png" );
//m_pTable->setPalette( palette );
m_pTable->viewport()->setPalette( palette );
Re: Background Image in QTableWidget
No. You have to use QPalette::Base instead of QPalette::Window.
Re: Background Image in QTableWidget
Quote:
Originally Posted by
wysota
No. You have to use QPalette::Base instead of QPalette::Window.
Thanks so much for your help. QPalette::Base is correct, but not when setting the palette on the viewport. QPalette::Base must be used when setting the palette on the table. Once I did that the image displays as we expected!
Code:
m_pTable->setHorizontalHeaderLabels( sLabels );
QPixmap px
( ":/images/ShowLogSplash.png" );
m_pTable->setPalette( palette );
One last question. I will need to display a normal background when a table row is added. Is there an efficient way to do this other than either saving the original QPalette and repeating the code with an empty QBrush or creating a white background image to use?
Re: Background Image in QTableWidget
You should be able to query the style() for a default palette.
1 Attachment(s)
Re: Background Image in QTableWidget
Hi, sorry to bring this article up, but I really need some help.
Here's my code:
Code:
QPalette palette
= ui
->tableWidget
->palette
();
ui->tableWidget->setPalette(palette);
ui->tableWidget->show();
What did I do wrong, that only the background behind cells gets colored?
Cells are still white.
I'm using designer.
Here's image of the problem.
Attachment 5989
Is there an easy way to make cells transparent or somehow use the image as background?
Any help would be appreciated.
Re: Background Image in QTableWidget
You're problem is different than the one I started this thread about. I had an empty table - no rows, no columns and wanted an image to display. You need transparent cells so the background image can show through.
While I don't know how to do this, I would start by giving the cells no brush.
If that didn't work I would look at setting the opacity of the brush (see QBrush documentation) or maybe this could be handled with a stylesheet.
If you get nowhere with these suggestions, start a new thread. There are some very bright developers on this forum and someone must know the answer to this problem.
Re: Background Image in QTableWidget
I know this is an old thread but i have the same problem
i have written this:
Code:
QPalette palette
= playlistTable.
palette();
palette.
setBrush(QPalette.
ColorRole.
Base, brush
);
playlistTable.setPalette(palette);
and it doesn't work
PS: using QtJambi