gpt4 book ai didi

python - PyQt:删除 QTableWidget 中的空格

转载 作者:太空宇宙 更新时间:2023-11-03 14:07:42 33 4
gpt4 key购买 nike

我想删除布局中表格中的空白区域:

Blank space

我想要这样的东西:

what I would obtain

这是我的代码:

import sys
from PyQt4 import QtGui
from PyQt4.QtGui import *
from PyQt4.QtSql import *
from PyQt4.QtCore import *
import sys


class Example(QMainWindow):

def __init__(self):
super(Example, self).__init__()
self.initUI()


def initUI(self):


self.resize(640, 480)
self.center()
self.setWindowTitle('')
self.setWindowIcon(QtGui.QIcon('icon.ico'))

exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.qApp.quit)

self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)



menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)

table = QTableWidget()
db = QSqlDatabase.addDatabase("")
db.setDatabaseName('xxx.db')

db.open()

query = QSqlQuery ("xxxx")
queryCount = QSqlQuery ("xxxx")
queryCount.next()
numberUserRow = queryCount.value(0).toInt()
table.setColumnCount(2)
table.setRowCount(numberUserRow[0])

table.setHorizontalHeaderItem(0, QTableWidgetItem("Post"));
table.setHorizontalHeaderItem(1, QTableWidgetItem("Data"));


MSG_POST = 3
DATA_SALVATAGGIO_POST = 4
index=0
while (query.next()):
table.setItem(index,0,QTableWidgetItem(query.value(MSG_POST).toString()))
table.setItem(index,1,QTableWidgetItem(query.value(DATA_SALVATAGGIO_POST).toString()))
index = index+1

self.statusBar().showMessage('Ready')
window = QWidget()

mainLayout = QGridLayout()

verticalLayout = QVBoxLayout()
horizontalLayout = QHBoxLayout()

horizontalLayout.addWidget(table)
verticalLayout.addLayout(horizontalLayout)

mainLayout.addLayout(verticalLayout,0,0)

window.setLayout(mainLayout)


self.setCentralWidget(window);
self.show()


def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())

def main():

app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

我试着在我的 table 右边放一个按钮,这有助于我调整 table 本身的大小。但是我不需要使用按钮,那么如何固定表格的宽度呢?

最佳答案

最好的方法是在表格的右侧使用 QSpacerItem

    horizontalSpacer = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Expanding, QSizePolicy.Expanding)
horizontalLayout.addWidget(table)
# Add the widget and the spacer to the horizontal layout
horizontalLayout.addItem(horizontalSpacer)

此外,您需要将小部件 Horizo​​ntal sizePolicy 设置为 Minimum,以去除表格中剩余的空白区域。为此,根据您的需要,有不同的选择。您可以将尺寸拉伸(stretch)至以下内容 this ,或放置一个固定尺寸,这可能是获得与您的图像最接近的结果的最佳方式:

    table.setSizePolicy(QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
header = table.horizontalHeader()
table.setMaximumWidth(header.length())
table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

显示结果here .

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

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