gpt4 book ai didi

python - 删除 QTableWidget 单元格中的空间

转载 作者:太空宇宙 更新时间:2023-11-03 20:26:56 24 4
gpt4 key购买 nike

我想将 QLineEditQLable 放入 QTableWidget 中的同一单元格。该单元格小部件创建了以下我从互联网上修改的代码。

class HiddenLabel(QLabel):
'''
QLable hide when mouse pressed
'''
def __init__(self, buddy, taskline, parent = None):
super(HiddenLabel, self).__init__(parent)
self.setFixedHeight(30)
self.buddy = buddy
self.taskline = taskline

# When it's clicked, hide itself and show its buddy
def mousePressEvent(self, event):
# left click to edit
if event.button() == QtCore.Qt.LeftButton:
self.hide()
self.buddy.setText(self.taskline.plain_text)
self.buddy.show()
self.buddy.setFocus() # Set focus on buddy so user doesn't have to click again



class EditableCell(QWidget):
'''
QLineEdit show when HiddenLabel is hidden
'''
def __init__(self, taskline, parent = None):
super(EditableCell, self).__init__(parent)
self.taskline = taskline
# Create ui
self.myEdit = QLineEdit()
self.myEdit.setFixedHeight(30)
self.myEdit.hide() # Hide line edit
self.myEdit.editingFinished.connect(self.textEdited)
# Create our custom label, and assign myEdit as its buddy
self.myLabel = HiddenLabel(self.myEdit, self.taskline)
self.myLabel.setText(self.taskline.enrich_text())
# Change vertical size policy so they both match and you don't get popping when switching
#self.myLabel.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)

# Put them under a layout together
hLayout = QHBoxLayout()
hLayout.addWidget(self.myLabel)
hLayout.addWidget(self.myEdit)
self.setLayout(hLayout)

def textEdited(self):
# If the input is left empty, revert back to the label showing
print('edit finished')
print(self.myEdit.text())
taskline = TaskLine()
taskline.parser(self.myEdit.text())
self.taskline = taskline
self.myLabel.setText(taskline.enrich_text())
self.myEdit.hide()
self.myLabel.show()

通过左键单击,单元格将在 QLineEditQLabel 之间切换。 enter image description here
如屏幕截图所示,我想删除单元格边框和单元格小部件之间的空白区域。

我认为可以通过样式设置来调整,但是我没有找到任何关于Qt样式设置的有用文档。希望有人能帮忙

最佳答案

您必须将布局边距设置为 0:

hLayout.setContentsMargins(0, 0, 0, 0)

the docs指出它们依赖于风格和平台:

void QLayout::setContentsMargins(int left, int top, int right, int bottom)

Sets the left, top, right, and bottom margins to use around the layout.

By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

关于python - 删除 QTableWidget 单元格中的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57752379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com