我正在使用PyQt4和Qt4Designer开发一个应用程序,我是从设计器和生成的python代码设计的。如下图所示,当我添加第一行时,它占用了整个布局空间,但后来的行具有固定宽度。
显示实际问题的图片
这是我的代码......
main.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'pyqt.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(983, 600)
MainWindow.setMinimumSize(QtCore.QSize(900, 600))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(100, 90, 675, 441))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, -1, -1, -1)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tableWidget = QtGui.QTableWidget(self.gridLayoutWidget)
self.tableWidget.setMinimumSize(QtCore.QSize(589, 439))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.tableWidget.horizontalHeader().setCascadingSectionResizes(False)
self.tableWidget.verticalHeader().setStretchLastSection(True)
self.gridLayout.addWidget(self.tableWidget, 0, 0, 1, 1)
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(100, 20, 85, 27))
self.label.setObjectName(_fromUtf8("label"))
self.AddTask = QtGui.QPushButton(self.centralwidget)
self.AddTask.setGeometry(QtCore.QRect(610, 20, 85, 27))
self.AddTask.setObjectName(_fromUtf8("AddTask"))
self.gridLayoutWidget.raise_()
self.label.raise_()
self.AddTask.raise_()
self.tableWidget.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Stockroom Adidas", None))
self.tableWidget.setToolTip(_translate("MainWindow", "<html><head/><body><p>This is where you can add new jobs</p></body></html>", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name", None))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "SKU", None))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Size", None))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Status", None))
self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:14pt;\">Lobby</span></p></body></html>", None))
self.AddTask.setText(_translate("MainWindow", "Add Task", None))
app.py
from PyQt4.QtCore import QRegExp
from PyQt4.QtGui import QHeaderView
from PyQt4.QtGui import QTableWidgetItem
from main import Ui_MainWindow, QtCore, QtGui
import sys
from newtask import Ui_NewTask
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
resolution = QtGui.QDesktopWidget().screenGeometry()
# positioning window to center
self.move((resolution.width() / 2) - (self.frameSize().width() / 2),
(resolution.height() / 2) - (self.frameSize().height() / 2))
self.setupUi(self)
# Customizing Columns in Table
rowHeader = self.tableWidget.horizontalHeader()
rowHeader.setResizeMode(0, QHeaderView.ResizeToContents)
rowHeader.setResizeMode(1, QHeaderView.Stretch)
rowHeader.setResizeMode(2, QHeaderView.Stretch)
rowHeader.setResizeMode(3, QHeaderView.ResizeToContents)
# self.tableWidget.resizeColumnsToContents()
# self.tableWidget.resizeRowsToContents()
# When clicked on Add Task button
def showDialog():
newdialog = NewTask(parent=self)
newdialog.show()
if newdialog.exec_():
# adding rows to the table
for i in range(len(newdialog.data)):
currentrow = self.tableWidget.rowCount()
self.tableWidget.insertRow(currentrow)
for j in range(len(newdialog.data[i])):
self.tableWidget.setItem(currentrow, j, newdialog.data[i][j])
self.AddTask.clicked.connect(showDialog)
header = self.tableWidget.verticalHeader()
header.setResizeMode(0, QHeaderView.Stretch)
# header.setDefaultSectionSize(12)
我有另一个继承QDialog的类,它有很多代码,不想让这个复杂,不发布它...你可以看到当点击AddTask按钮时我将信号连接到showDialog函数。我在那里添加行,我不知道我在哪里做错了,我尝试了以下所有方法。
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
OR
header.setResizeMode(QHeaderView.Stretch)
OR
header.setResizeMode(QHeaderView.Fixed)
OR
self.tableWidget.rowHeight(20)
OR
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
他们都没有工作......请帮忙......
EDIT1:
当我尝试在qtdesigner中添加行时,面临同样的问题,将图像附加到此处
QtDesigner
答案 0 :(得分:0)
可能self.tableWidget.verticalHeader().setStretchLastSection(True)
导致问题。