PDA

View Full Version : Background Image in QTableWidget



mclark
31st January 2008, 22:58
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?

wysota
31st January 2008, 23:26
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).

mclark
31st January 2008, 23:29
Thanks wysota, I'll try your suggestions.

mclark
1st February 2008, 16:10
wysota, I though I would try what appeared to be the easiest possibilities first.


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?

m_pTable = new QTableWidget( 0, 7, m_pMainWin );
m_pTable->setHorizontalHeaderLabels( sLabels );

QPalette palette = m_pTable->palette();
QPixmap px( ":/images/ShowLogSplash.png" );
palette.setBrush( QPalette::Window, QBrush( px ) );
m_pTable->setPalette( palette );

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?

m_pTable = new QTableWidget( 0, 7, m_pMainWin );
m_pTable->setHorizontalHeaderLabels( sLabels );

QWidget* viewport = m_pTable->viewport();
QLabel lbl( "QLable Text in a Viewport" );
QVBoxLayout* vLayout = new QVBoxLayout;
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.

wysota
1st February 2008, 17:36
The palette has to be set on the viewport not on the widget itself.

mclark
1st February 2008, 19:50
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?


m_pTable = new QTableWidget( 0, 7, m_pMainWin );
m_pTable->setHorizontalHeaderLabels( sLabels );

QPalette palette = m_pTable->palette();
QPixmap px( ":/images/ShowLogSplash.png" );
palette.setBrush( QPalette::Window, QBrush( px ) );
//m_pTable->setPalette( palette );
m_pTable->viewport()->setPalette( palette );

wysota
1st February 2008, 19:52
No. You have to use QPalette::Base instead of QPalette::Window.

mclark
1st February 2008, 20:18
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!


m_pTable = new QTableWidget( 0, 7, m_pMainWin );
m_pTable->setHorizontalHeaderLabels( sLabels );

QPalette palette = m_pTable->palette();
QPixmap px( ":/images/ShowLogSplash.png" );
palette.setBrush( QPalette::Base, QBrush( px ) );
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?

wysota
1st February 2008, 20:49
You should be able to query the style() for a default palette.

MSGhost
27th February 2011, 07:11
Hi, sorry to bring this article up, but I really need some help.

Here's my code:

QPixmap pixmap("anime.jpg");
QPalette palette = ui->tableWidget->palette();
palette.setBrush(QPalette::Base, QBrush(pixmap));
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.
5989

Is there an easy way to make cells transparent or somehow use the image as background?
Any help would be appreciated.

mclark
28th February 2011, 15:51
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.

QTableWidgetItem::setBackground( QBrush() );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.

clepto
6th June 2012, 17:06
I know this is an old thread but i have the same problem

i have written this:

QPalette palette = playlistTable.palette();
QPixmap a = new QPixmap("/home/chris/playlist_background.png");
QBrush brush = new QBrush(a);
palette.setBrush(QPalette.ColorRole.Base, brush);
playlistTable.setPalette(palette);

and it doesn't work

PS: using QtJambi