Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to remove the gap between 2 icon in the column of QTableview



  • Hi guys, I have created a table with just 1 row with multiple column, may I know how to remove the gap between the arrow image in the table and make them look as they stick together?

    Thanks
    Capture.PNG

    from PyQt5 import QtCore, QtGui
    import sys
    
    from PyQt5.QtWidgets import QApplication, QLabel, QTableWidget, QTableWidgetItem, QToolBar, QWidget
    
    imagePath = "arrowREDHead.png"
    imagePath2 = "arrowREDBody.png"
    
    class ImgWidget1(QLabel):
    
        def __init__(self, parent=None):
            super(ImgWidget1, self).__init__(parent)
            pic = QtGui.QPixmap(imagePath)
            self.setPixmap(pic)
    
    class ImgWidget2(QLabel):
    
        def __init__(self, parent=None):
            super(ImgWidget2, self).__init__(parent)
            pic = QtGui.QPixmap(imagePath2)
            self.setPixmap(pic)
    
    
    class Widget(QWidget):
    
        def __init__(self, array1):
            super(Widget, self).__init__()
            tableWidget = QTableWidget(1, len(array1), self)
            tableWidget.setMinimumWidth(800)
            tableWidget.setMinimumHeight(800)
            tableWidget.setShowGrid(False)
            # thumbnail = QTableWidgetItem( QtGui.QIcon( QtGui.QPixmap( imagePath2 ).scaled(5000, 5000)),"")
            # thumbnail2 = QTableWidgetItem( QtGui.QIcon( QtGui.QPixmap( imagePath2 ).scaled(5000, 5000)),"")
            # tableWidget.setItem(3, 5, QtGui.QTableWidgetItem())
            self.array1 = array1
            header = tableWidget.horizontalHeader()
            header. hide()
            # for i in range (0, len(array1)-1):
            #     if(self.array1[i] == "s"):
            #         tableWidget.setItem(0, i, thumbnail)
            
            for i in range (0, len(array1)- 1):
                if(self.array1[i] == "s"):
                    tableWidget.setItem(0, i, QTableWidgetItem( QtGui.QIcon( QtGui.QPixmap( imagePath ).scaled(100, 100)),""))
                else:
                    tableWidget.setItem(0, i, QTableWidgetItem( QtGui.QIcon( QtGui.QPixmap( imagePath2 ).scaled(100, 100)),""))
                
                    
            # tableWidget.cellWidget(0, 0).palette().setColor(Qt.red)
            # tableWidget.cellWidget(0, 0).setStyleSheet('background-color: yellow;')
            # tableWidget.cellWidget(0, 1).setStyleSheet('background-color: red;')
            tableWidget.itemClicked.connect(self.sequenceClicked)
            # tableWidget.cellWidget(0, 1).setStyleSheet('background-color: red;')
            # tableWidget.resizeRowsToContents()
            tableWidget.resizeColumnsToContents()
            tableWidget.setIconSize(QtCore.QSize(45, 45))
    
        def sequenceClicked(self, item): 
            self.itemClicked = item
            item.setBackground(QtGui.QColor(255, 215, 0))
                
                    
    if __name__ == "__main__":
        app = QApplication([])
        array1 = ["a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s","a","s"]
        wnd = Widget(array1)
        wnd.show()
        sys.exit(app.exec_())
    


  • @JohnLim02
    The easiest way of doing it is using stylesheets

    setStylesheet("QTableWidget::item { padding: 40px }")
    


  • @Thank-You said in How to remove the gap between 2 icon in the column of QTableview:

    setStylesheet("QTableWidget::item { padding: 40px }")

    I added this but it is still the same doesn't change anything

          # tableWidget.cellWidget(0, 0).palette().setColor(Qt.red)
            # tableWidget.cellWidget(0, 0).setStyleSheet('background-color: yellow;')
            # tableWidget.cellWidget(0, 1).setStyleSheet('background-color: red;')
            tableWidget.itemClicked.connect(self.sequenceClicked)
            # tableWidget.cellWidget(0, 1).setStyleSheet('background-color: red;')
            # tableWidget.resizeRowsToContents()
            tableWidget.resizeColumnsToContents()
            tableWidget.setIconSize(QtCore.QSize(45, 45))
            tableWidget.setStyleSheet("QTableWidget::item { padding: 40px }")
    

Log in to reply