How can i disable the selection highlighted green in table widget using pyqt4

时间:2019-03-18 08:27:14

标签: python-2.7 pyqt4

Here is my sample code.I need to remove green highlighted selection in my table widget for that i wrote QtGui.QAbstractItemView.NoSelection but using this line i am not able to move my cursor up,down ,left and right in table widget.Can any one please tell me how to disable the green selection and i want to move my cursor using with arrow keys in keyboard..Please help me..Thank you in advance.. Given below is my code:

import sys
from PyQt4 import QtCore,QtGui
global data_arraylist,count,pushBtnList,lineeditList
class Table_Program(QtGui.QMainWindow ):
    def __init__(self,config=None, parent=None):
        super(Table_Program, self).__init__(parent)
        global data_arraylist,count,pushBtnList,lineeditList
        self.scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
        self.mainw2 = QtGui.QWidget()
        self.mainw2.showFullScreen()
        self.scrollArea_left.setWidget(self.mainw2)
        self.newvbox = QtGui.QGridLayout(self.mainw2)
        self.table = QtGui.QTableWidget()
        self.table_item = QtGui.QTableWidgetItem()
        self.table.setRowCount(1)
        self.table.verticalHeader().hide()
        self.table.setColumnCount(4)
        self.table.setHorizontalHeaderLabels(("S.no, Item Description,Qty,Rate(Rs:),"",").split(','))
        self.newvbox.addWidget(self.table,0,0)
        self.table.setColumnWidth(2,170)
        self.table.setColumnWidth(3,200)
        self.btn1 = QtGui.QPushButton(icon=QtGui.QIcon("./plus1.png"))
        self.btn1.setIconSize(QtCore.QSize(30,20))
        self.table.setCellWidget(0,0,self.btn1)
        self.btn1.clicked.connect(self.insert_rows)
        self.table.setItem(0,4,QtGui.QTableWidgetItem(str(" ")))
        self.setCentralWidget(self.scrollArea_left)
        # QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Up), self, self.moveup)
    def insert_rows(self,typ=None):
        layout_item = QtGui.QHBoxLayout()
        cellWidget = QtGui.QWidget()
        self.itemlineedit = QtGui.QLineEdit()
        self.itemlineedit.setFrame(False)
        self.search_btn = QtGui.QPushButton(icon=QtGui.QIcon('search.png'))
        self.search_btn.setIconSize(QtCore.QSize(20,20))
        layout_item.addWidget(self.itemlineedit)
        layout_item.addStretch()
        layout_item.addWidget(self.search_btn)
        cellWidget.setLayout(layout_item)
        global pushBtnList
        ct = self.table.rowCount()
        self.table.insertRow(ct-1)
        self.btn = QtGui.QPushButton()
        self.btn.setIcon(QtGui.QIcon("./delete.png"))
        self.btn.setIconSize(QtCore.QSize(30,60))
        self.table.setItem(ct-1,0,QtGui.QTableWidgetItem(str(ct)))
        self.table.setCellWidget(ct -1,5,self.btn)
        index = QtCore.QPersistentModelIndex(self.table.model().index(ct -1, 5))
        self.quantybutton = QtGui.QLineEdit()
        self.itemlineedit.textChanged.connect(partial(self.product_rate,ct))
        completer = QtGui.QCompleter()
        self.itemlineedit.setCompleter(completer)
        self.model = QtGui.QStringListModel()
        self.itemlineedit.resize(100,50)
        completer.setModel(self.model)
        self.item_rate = QtGui.QLabel()
        self.table.setCellWidget(ct-1, 3,self.item_rate)
        self.table.setCellWidget(ct-1, 2,  self.quantybutton)
        self.table.setCellWidget(ct -1, 1, cellWidget)
        self.table.setRowHeight(ct-1,50)
        self.table.setColumnWidth(1,170)


    def get_data(self,model):
        model.setStringList(["item1","item2","item3"])
    def product_rate(self,text,index):
        self.quantybutton.setText("1")
        self.item_rate.setText(str("20"))
        self.get_data(self.model)
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    tb = Table_Program()
    tb.show()
    tb.resize(600,300)
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案是实现一个自定义QStyle,以禁用QTableWidget中的Highlight选项:

from PyQt4 import QtCore, QtGui

class CustomStyle(QtGui.QCommonStyle):
    def drawPrimitive(self, element, option, painter, widget):
        if element == QtGui.QStyle.PE_PanelItemViewItem:
            option.state &= ~QtGui.QStyle.State_Selected
        super(CustomStyle, self).drawPrimitive(element, option, painter, widget)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = QtGui.QTableWidget(5, 4)
    w.setStyle(CustomStyle()) # <--- set style
    w.show()
    sys.exit(app.exec_())