Background in QTableWidget

  • Hi. I have class inherited from QTableWidget. I want to set background from file, but line
    "setStyleSheet("background-image: url("board.jpg");");" or
    setStyleSheet("background-image: url(./board.jpg);") doesn't work... What is solution?

  • Hi and welcome to DevNet!

    I think this can be done via QPalette/QPixmap:
    @ QTableWidget* table = new QTableWidget;

    QPalette palette = table->palette();
    QPixmap jpgImage( "board.jpg");
    palette.setBrush(QPalette::Base, QBrush(jpgImage));

    Try it, I didn't test it..

  • Hmmm. Where should be file board.jpg? In folder "build-Checkers_V01-Desktop_Qt_5_3_MSVC2013_64bit-Debug" or just in "Checkers_V01"?

  • It depends.. Try it in both folders to see if it works. Or use an absolute path like "C:\Path\To\Image\board.jpg"

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    [quote author="Binary91" date="1417095175"]Hi and welcome to DevNet!
    @QPixmap jpgImage( "board.jpg");@

    This is a relative path so the file must be in the same folder as the executable.

    On a related note, since you are using Qt, you should use the *nix forward slash notation, it will avoid many problem with paths

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.