Results 1 to 12 of 12

Thread: Background Image in QTableWidget

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question 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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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).

  3. The following user says thank you to wysota for this useful post:

    mclark (31st January 2008)

  4. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background Image in QTableWidget

    Thanks wysota, I'll try your suggestions.

  5. #4
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background Image in QTableWidget

    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?
    Qt Code:
    1. m_pTable = new QTableWidget( 0, 7, m_pMainWin );
    2. m_pTable->setHorizontalHeaderLabels( sLabels );
    3.  
    4. QPalette palette = m_pTable->palette();
    5. QPixmap px( ":/images/ShowLogSplash.png" );
    6. palette.setBrush( QPalette::Window, QBrush( px ) );
    7. m_pTable->setPalette( palette );
    To copy to clipboard, switch view to plain text mode 
    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?
    Qt Code:
    1. m_pTable = new QTableWidget( 0, 7, m_pMainWin );
    2. m_pTable->setHorizontalHeaderLabels( sLabels );
    3.  
    4. QWidget* viewport = m_pTable->viewport();
    5. QLabel lbl( "QLable Text in a Viewport" );
    6. QVBoxLayout* vLayout = new QVBoxLayout;
    7. vLayout->addWidget( &lbl );
    8. viewport->setLayout( vLayout );
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Background Image in QTableWidget

    The palette has to be set on the viewport not on the widget itself.

  7. #6
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background Image in QTableWidget

    Quote Originally Posted by wysota View Post
    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?

    Qt Code:
    1. m_pTable = new QTableWidget( 0, 7, m_pMainWin );
    2. m_pTable->setHorizontalHeaderLabels( sLabels );
    3.  
    4. QPalette palette = m_pTable->palette();
    5. QPixmap px( ":/images/ShowLogSplash.png" );
    6. palette.setBrush( QPalette::Window, QBrush( px ) );
    7. //m_pTable->setPalette( palette );
    8. m_pTable->viewport()->setPalette( palette );
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Background Image in QTableWidget

    No. You have to use QPalette::Base instead of QPalette::Window.

  9. #8
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Background Image in QTableWidget

    Quote Originally Posted by wysota View Post
    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!

    Qt Code:
    1. m_pTable = new QTableWidget( 0, 7, m_pMainWin );
    2. m_pTable->setHorizontalHeaderLabels( sLabels );
    3.  
    4. QPalette palette = m_pTable->palette();
    5. QPixmap px( ":/images/ShowLogSplash.png" );
    6. palette.setBrush( QPalette::Base, QBrush( px ) );
    7. m_pTable->setPalette( palette );
    To copy to clipboard, switch view to plain text mode 

    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?

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Background Image in QTableWidget

    You should be able to query the style() for a default palette.

  11. The following user says thank you to wysota for this useful post:

    mclark (1st February 2008)

  12. #10
    Join Date
    Feb 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Re: Background Image in QTableWidget

    Hi, sorry to bring this article up, but I really need some help.

    Here's my code:
    Qt Code:
    1. QPixmap pixmap("anime.jpg");
    2. QPalette palette = ui->tableWidget->palette();
    3. palette.setBrush(QPalette::Base, QBrush(pixmap));
    4. ui->tableWidget->setPalette(palette);
    5. ui->tableWidget->show();
    To copy to clipboard, switch view to plain text mode 

    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.
    problem.jpg

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

  13. #11
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Qt Code:
    1. QTableWidgetItem::setBackground( QBrush() );
    To copy to clipboard, switch view to plain text mode 
    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.

  14. #12
    Join Date
    Dec 2011
    Posts
    11
    Platforms
    Unix/X11

    Default Re: Background Image in QTableWidget

    I know this is an old thread but i have the same problem

    i have written this:
    Qt Code:
    1. QPalette palette = playlistTable.palette();
    2. QPixmap a = new QPixmap("/home/chris/playlist_background.png");
    3. QBrush brush = new QBrush(a);
    4. palette.setBrush(QPalette.ColorRole.Base, brush);
    5. playlistTable.setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    and it doesn't work

    PS: using QtJambi

Similar Threads

  1. Replies: 3
    Last Post: 18th May 2012, 10:12
  2. Replies: 15
    Last Post: 23rd March 2007, 16:16
  3. Setting background image of QFrame
    By Claymore in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2007, 19:50
  4. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 06:25
  5. [QT4.1.1 XP] background image
    By incapacitant in forum Newbie
    Replies: 3
    Last Post: 1st March 2006, 13:02

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.