【发布时间】:2017-06-17 11:03:03
【问题描述】:
我想删除我在布局中放置的表格中的空白:
我想要这样的东西:
这是我的代码:
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()
我尝试在我的表格右侧放置一个按钮,这有助于我调整表格本身的大小。但是我不需要使用按钮,那么如何固定表格的宽度?
【问题讨论】:
标签: python qt layout pyqt qtablewidget