I want to remove the space between labels. Range number of labels is 1 to 5 and is used in loop ( text got from db )
I Used varios option but everythoing failed (.setAlignment/.setMargin~~~~)
And I googled many documents, but I could not find any solution.
How Can I Do ?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
# self.setGeometry(-1700, -300, 1500, 800)
self.setGeometry( 500, 200, 500, 200)
self.tableWidget = QTableWidget(self)
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.resize(1000, 1000)
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(3)
# self.tableWidget.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
column_labels = ["column1", "column2"]
self.tableWidget.setHorizontalHeaderLabels(column_labels)
self.tableWidget.setItem(0, 0, QTableWidgetItem("test1"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("63,900"))
layout = QHBoxLayout(self)
QFont('Arial', 10)
label1 = QLabel(" ")
label1.setFont(QFont('Arial', 20))
label1.resize(200, 100)
label1.setStyleSheet("QLabel { background-color : red; color : black;}")
label2 = QLabel(" ")
label2.setStyleSheet("QLabel { background-color : blue; color : blue; padding:0}")
label3 = QLabel(" ")
label3.setStyleSheet("QLabel { background-color : lightgreen; color : blue; padding:0}")
layout.setContentsMargins(0,0,0,0)
layout.addWidget(label1)
layout.addWidget(label2)
layout.addWidget(label3)
layout.addStretch(1)
self.title = QLabel("97%")
# self.title.setMargin(0)
f = QFrame()
f.setLayout(layout)
f.setContentsMargins(0,0,0,0)
self.tableWidget.setCellWidget(0, 1, f )
self.tableWidget.setColumnWidth(1, 300)
if __name__ == "__main__":
app = QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()
You need to set the spacing of the labels through setSpacing(0). This code works:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(500, 200, 500, 200)
self.tableWidget = QTableWidget(self)
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.resize(1000, 1000)
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(3)
column_labels = ["column1", "column2"]
self.tableWidget.setHorizontalHeaderLabels(column_labels)
self.tableWidget.setItem(0, 0, QTableWidgetItem("test1"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("63,900"))
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0) # Set spacing to zero to remove space between labels
label1 = QLabel(" ")
label1.setFont(QFont('Arial', 20))
label1.setStyleSheet("QLabel { background-color : red; color : black;}")
layout.addWidget(label1)
label2 = QLabel(" ")
label2.setStyleSheet("QLabel { background-color : blue; color : blue;}")
layout.addWidget(label2)
label3 = QLabel(" ")
label3.setStyleSheet("QLabel { background-color : lightgreen; color : blue;}")
layout.addWidget(label3)
f = QFrame()
f.setLayout(layout)
self.tableWidget.setCellWidget(0, 1, f)
self.tableWidget.setColumnWidth(1, 300)
if __name__ == "__main__":
app = QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()