PyQt:删除 QTableWidget 中的空白 space
PyQt: remove blank space in QTableWidget
我想删除 table 布局中的空白 space:
我想要这样的东西:
这是我的代码:
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 本身的大小。但是我不需要使用按钮,所以我怎样才能固定 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)
此外,您需要将小部件 Horizontal sizePolicy 设置为 Minimum,以去除 [=24= 中剩余的白色 space ].为此,根据您的需要,有不同的选择。您可以将尺寸拉伸到 之后的内容,或者放置一个固定尺寸,这可能是获得最接近图像的最佳结果:
table.setSizePolicy(QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
header = table.horizontalHeader()
table.setMaximumWidth(header.length())
table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
显示结果here。
这可以像这样非常简单地实现:
horizontalLayout.addWidget(table)
horizontalLayout.addStretch()
table.horizontalHeader().setResizeMode(QHeaderView.Stretch)
这将添加一个扩展的 spacer 将 table 推到左侧,它还会使 table 列伸展以填充可用的 space.
如果您不希望 table 在主 window 调整大小时调整大小,请执行以下操作:
horizontalLayout.addStretch(1)
设置 stretch-factor 优先 spacer,因此 table 将固定在其最小宽度。当然,您可以根据需要进行调整:
table.setMinimumWidth(400)
我想删除 table 布局中的空白 space:
我想要这样的东西:
这是我的代码:
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 本身的大小。但是我不需要使用按钮,所以我怎样才能固定 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)
此外,您需要将小部件 Horizontal sizePolicy 设置为 Minimum,以去除 [=24= 中剩余的白色 space ].为此,根据您的需要,有不同的选择。您可以将尺寸拉伸到
table.setSizePolicy(QtGui.QSizePolicy.Minimum,QtGui.QSizePolicy.Expanding)
header = table.horizontalHeader()
table.setMaximumWidth(header.length())
table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
显示结果here。
这可以像这样非常简单地实现:
horizontalLayout.addWidget(table)
horizontalLayout.addStretch()
table.horizontalHeader().setResizeMode(QHeaderView.Stretch)
这将添加一个扩展的 spacer 将 table 推到左侧,它还会使 table 列伸展以填充可用的 space.
如果您不希望 table 在主 window 调整大小时调整大小,请执行以下操作:
horizontalLayout.addStretch(1)
设置 stretch-factor 优先 spacer,因此 table 将固定在其最小宽度。当然,您可以根据需要进行调整:
table.setMinimumWidth(400)