How to set QTableWidget upper left corner using a background image?
Asked Answered
B

3

6

How to set QTableWidget upper left corner using a background image? Now it is white. (Pointed in the image below)

enter image description here

This is my style sheet code:

QWidget {
    background-image: url(src/bg.jpg);
    color: #fffff8;
}

QHeaderView::section {
    background-image: url(src/bg.jpg);
    padding: 4px;
    border: 1px solid #fffff8;
}

QTableWidget {
    gridline-color: #fffff8;
}

QTableWidget QTableCornerButton::section {
    background-image: url(src/bg.jpg);
    border: 1px solid #fffff8;
}
Backwater answered 3/1, 2017 at 10:3 Comment(5)
My guess is you will have to add a column to hold row numbers, and disable the header.Contractor
Works fine for me on linux. You could try setting a different corner widget.Lorant
oh!thanks,when i use setHorizontalHeaderLabels(),QTableWidget upper left corner is white,it is a qt bug? @ScholliiBackwater
@Lorant The setCornerWidget() works for the widget at the crossing of the scrollbars, i.e. bottom-right corner (when scrollbars are visible!). The widget at the crossing of row header and column header (i.e. top left corner) does not appear to be customizable (except that it can be disabled and enabled).Contractor
@Schollii. Thanks - I don't know how I missed that rather obvious point. However, the stylesheet does work for me - so the question hasn't really been answered. Presumably there's some platform-specific issue at play.Lorant
C
1

You will have to use a custom row-headers column:

  • disable the builtin row headers column,
  • add a column to hold row numbers,
  • define slots to update row ID when rows are added/removed and to select row, change its font etc,
  • and finally set the widget to be used in column=0 column header.
Contractor answered 4/1, 2017 at 4:10 Comment(0)
M
5

For me, using PyQt4.13 on Windows, setting a background-color on the QTableWidget QTableCornerButton element in the Qt stylesheet has an effect but setting background-image has not. However, you can make the background-color transparent (rgba(0,0,0,0)) and then you see the background image of the widget shining through. The border should be set again to make it look nice.

Code:

from PyQt4 import QtGui

app = QtGui.QApplication([])

# app.setStyleSheet('QWidget { background-color: #aa8888; } QHeaderView::section { background-color: #88aa88; } QTableWidget QTableCornerButton::section {background-color: #8888aa; }')
# app.setStyleSheet('QWidget { background-image: url(bg.png); } QHeaderView::section { background-image: url(bg.png); } QTableWidget QTableCornerButton::section {background-image: url(bg.png); }')
app.setStyleSheet('QWidget { background-image: url(bg.png); } QHeaderView::section { background-color: rgba(0,0,0,0); } QTableWidget QTableCornerButton::section {background-color: rgba(0,0,0,0); }')

w = QtGui.QTableWidget(2, 2)
w.show()

app.exec_()

Results in (depending on which stylesheet line you use): enter image description here

Left image: set widget, headerview and tablecornerbutton background color works

Center image: set widget background image, try to set headerview and tablecornerbutton background image but without effect

Right image: set widget background image, set headerview and tablecornerbutton background color to transparent. Borders have to be set again probably.

Meraz answered 6/1, 2017 at 15:16 Comment(0)
C
1

You will have to use a custom row-headers column:

  • disable the builtin row headers column,
  • add a column to hold row numbers,
  • define slots to update row ID when rows are added/removed and to select row, change its font etc,
  • and finally set the widget to be used in column=0 column header.
Contractor answered 4/1, 2017 at 4:10 Comment(0)
G
0

Use this style sheet code:

QTableCornerButton::section {
    background-color: rgb(66, 66, 66);
}
Goshen answered 6/6, 2023 at 23:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.