如何使用pyqt4删除表格小部件中的特定行边框

时间:2019-02-12 11:58:58

标签: python python-2.7 pyqt pyqt4

我在这里有一个示例代码,因为我需要删除表格部件中特定行的边框。我尝试了很多方法,但是找不到任何方法。任何人都可以告诉我如何删除表格小部件的边框。我使用gridlayout(False),但是此方法用于删除表格的所有边框。请帮帮我。

以下是我的代码:

from PyQt4 import QtCore, QtGui
import sys, collections
from functools import partial
class Inventory_items(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Inventory_items, self).__init__(parent)
        print "hello inventory"
        # self.showFullScreen()
        self.inventory_widget = QtGui.QWidget()
        self.inventory_widget.show()
        self.inventory_widget.setStyleSheet("QWidget {background-color: black;color: white;}")
        self.inventory_dict = {"Shirt": {"Men Solid Formal Blue shirt": ["10.10.1(Red)","10.10.2(blue)"],"Unit:Quantity": ["10", "5"],"Discount:0%": ["300", "400"]},
        "Frocks":{"Unit:Quantity":["50","30"],"Baby Girls Partywear Frock":["10.10.1(pink)","10.10.1(black)"],"Discount:0%": ["900", "1200"]},"Shoes":{"Unit:Quantity":["100","300"],"Sparx Men's Running Shoes":["Navy Blue","Orange color"],"Discount:0%": ["2000", "2500"]},"Phones":{"Unit:Quantity":["30","10"],"Realme 2 Pro Smart phone":["32 GB(4 GB RAM)","64 GB(4 GB RAM)"],"Discount:0%": ["15000", "1800"]},"Perfume":{"Unit:Quantity":["10","3"],"Fugo Perfume":["30ml","60ml"],"Discount:0%": ["300", "600"]}}

        self.inventr_gridbox = QtGui.QGridLayout(self.inventory_widget)

        self.table1 = QtGui.QTableWidget()
        self.table1.setFocusPolicy(QtCore.Qt.NoFocus);
        self.table1.setStyleSheet("QTableWidget{gridline-color: white}")
        self.Inventory_scrollarea = QtGui.QScrollArea(widgetResizable=True)
        self.Inventory_scrollarea.setWidget(self.inventory_widget)
        self.Inventory_scrollarea.showFullScreen()

        self.table1.horizontalHeader().hide()
        self.table_item1 = QtGui.QTableWidgetItem()
        self.table1.setRowCount(21)

        self.table1.verticalHeader().hide()
        self.table1.setColumnCount(7)
        stylesheet1 = "::section{Background-color:black;color: white;font-size: 25px;}"
        header1 = self.table1.horizontalHeader()
        vh1 = self.table1.verticalHeader()


        vh1.setDefaultSectionSize(50)
        header1.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
        header1.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
        header1.setResizeMode(2, QtGui.QHeaderView.Stretch)
        header1.setResizeMode(3, QtGui.QHeaderView.Stretch)
        header1.setResizeMode(4, QtGui.QHeaderView.Stretch)
        header1.setResizeMode(5, QtGui.QHeaderView.ResizeToContents)
        header1.setResizeMode(6, QtGui.QHeaderView.ResizeToContents)
        self.table1.setColumnWidth(2,350)

        self.index = 0
        for key, value in self.inventory_dict.items():


            self.shrtlabel = QtGui.QPushButton()
            self.shrtlabel.setIcon(QtGui.QIcon("./{0}.png".format(key)))
            self.shrtlabel.setStyleSheet("QPushButton{outline : None;}")
            self.shrtlabel.setIconSize(QtCore.QSize(40,60))
            self.table1.setCellWidget(self.index,0,self.shrtlabel)
            self.table1.setItem(self.index,1,QtGui.QTableWidgetItem(str(key)))
            keys = value.keys()
            values = value.values()

            if len(keys) == len(values):
                self.table1.setItem(self.index,2,QtGui.QTableWidgetItem(str(keys[0])))
                self.table1.setItem(self.index,3,QtGui.QTableWidgetItem(str(keys[2])))
                self.table1.setItem(self.index,4,QtGui.QTableWidgetItem(str(keys[1])))
                self.table1.setItem(self.index+1,2,QtGui.QTableWidgetItem(str(values[0][0])))
                self.table1.setItem(self.index+1,3,QtGui.QTableWidgetItem(str(values[2][0])))
                self.table1.setItem(self.index+1,4,QtGui.QTableWidgetItem(str(values[1][0])))
                self.table1.setItem(self.index+2,2,QtGui.QTableWidgetItem(str(values[0][1])))
                self.table1.setItem(self.index+2,3,QtGui.QTableWidgetItem(str(values[2][1])))
                self.table1.setItem(self.index+2,4,QtGui.QTableWidgetItem(str(values[1][1])))
                self.index +=3

        self.inventr_gridbox.addWidget(self.table1,1,0)
        self.setCentralWidget(self.Inventory_scrollarea)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    settingobj= Inventory_items()
    settingobj.showFullScreen()
    sys.exit(app.exec_())

enter image description here

在这里我只想显示项目的边框,而不是下面的描述项目

1 个答案:

答案 0 :(得分:1)

数据的结构是一棵树,因此您应该使用QTreeView或QTreeWidget,然后可以使用委托设置边框:

from PyQt4 import QtCore, QtGui

class InventoryDelegate(QtGui.QStyledItemDelegate):
    def paint(self, painter, option, index):
        super(InventoryDelegate, self).paint(painter, option, index)
        if not index.parent().isValid():
            painter.save()
            painter.setPen(QtGui.QPen(QtGui.QColor("green")))
            r = QtCore.QRect(option.rect)
            r.adjust(0, 1, 0, -1)
            painter.drawLine(r.topLeft(), r.topRight())
            painter.drawLine(r.bottomLeft(), r.bottomRight())
            painter.restore()

    def sizeHint(self, option, index):
        s = super(InventoryDelegate, self).sizeHint(option, index)
        s.setHeight(55)
        return s

class Inventory_Items(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Inventory_Items, self).__init__(parent)
        self.inventory_dict = {
            "Shirt": {
                "Men Solid Formal Blue shirt": ["10.10.1(Red)", "10.10.2(blue)"],
                "Discount:0%": ["300", "400"],
                "Unit:Quantity": ["10", "5"]
            },
            "Frocks": {
                "Baby Girls Partywear Frock": ["10.10.1(pink)", "10.10.1(black)"],
                "Discount:0%": ["900", "1200"],
                "Unit:Quantity": ["50", "30"]
            },
            "Shoes": {
                "Sparx Men's Running Shoes": ["Navy Blue", "Orange color"],
                "Discount:0%": ["2000", "2500"],
                "Unit:Quantity": ["100", "300"]
            },
            "Phones": {
                "Realme 2 Pro Smart phone": ["32 GB(4 GB RAM)", "64 GB(4 GB RAM)"],
                "Discount:0%": ["15000", "1800"],
                "Unit:Quantity": ["30", "10"]
            },
            "Perfume": {
                "Fugo Perfume": ["30ml", "60ml"],
                "Discount:0%": ["300", "600"],
                "Unit:Quantity": ["10", "3"]
            }
        }

        self.inventory_widget = QtGui.QTreeWidget(columnCount=7, 
            expandsOnDoubleClick=False,
            indentation=0,
            focusPolicy=QtCore.Qt.NoFocus,
            iconSize=QtCore.QSize(40,60))

        self.inventory_widget.setColumnWidth(2, 350)
        self.inventory_widget.setStyleSheet("QWidget {background-color: black;color: white;}")
        header = self.inventory_widget.header()
        header.hide()
        header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
        header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
        header.setResizeMode(2, QtGui.QHeaderView.Stretch)
        header.setResizeMode(3, QtGui.QHeaderView.Stretch)
        header.setResizeMode(4, QtGui.QHeaderView.Stretch)
        header.setResizeMode(5, QtGui.QHeaderView.ResizeToContents)
        header.setResizeMode(6, QtGui.QHeaderView.ResizeToContents)

        delegate = InventoryDelegate(self.inventory_widget)
        self.inventory_widget.setItemDelegate(delegate)

        for key, value in self.inventory_dict.items():
            icon = QtGui.QIcon("./{}.png".format(key))
            it = QtGui.QTreeWidgetItem()
            self.inventory_widget.addTopLevelItem(it)
            it.setIcon(0, icon)
            k = list(value.keys())
            for i, t in enumerate([key] + k):
                it.setText(i+1, t)

            e = [[] for _ in value[k[0]]]
            for k, v in value.items():
                for i, val in enumerate(v):
                    e[i].append(val)
            for r in e:
                child = QtGui.QTreeWidgetItem()
                for i, e_ in enumerate(r):
                    child.setText(i+2, e_) 
                    it.addChild(child)  
        self.inventory_widget.expandAll()
        self.setCentralWidget(self.inventory_widget)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    settingobj= Inventory_Items()
    settingobj.showFullScreen()
    sys.exit(app.exec_())