我有一个窗口,其中包含QTableView
which 栏目根据内容进行调整并且是宽度固定. The QTableView
嵌套在一个QWidget
依次嵌套在QScrollArea
依次嵌套在tabbed QMdiArea
哪一个是centralWidget
of a QMainWindow
.
当。。。的时候QScrollArea
如图所示,QTableView
我想删除的最后一列右侧有多余的空间:
我想要QTableView
以适应列的精确宽度(即最右边的列之后没有额外的空间)。
我尝试使用tableView.setFixedWidth(desired_width)
但唯一可行的方法是迭代所有列并获取它们的宽度并将它们加在一起并添加verticalHeader
width 和滚动条宽度并将其作为desired_width
.
它确实以这种方式工作,但对于这样一个明显的要求来说,它似乎非常复杂:我认为很多程序开发人员希望他们的表的宽度适合列的宽度,这让我想知道也许有一种方法可以自动执行此操作,无需额外的计算,而我不知道。
所以我的问题是:是否有更简单的方法来达到相同的结果?
这是我的代码供参考:
代码为t_main
模块负责创建QMainWindow
:
import sys
import t_wdw
from PySide import QtGui
class Main_Window(QtGui.QMainWindow):
def __init__(self):
super(Main_Window,self).__init__()
self.initUI()
def initUI(self):
self.statusBar()
# Defines QActions for menu
self.new_window=QtGui.QAction("&Window alpha",self)
self.new_window.triggered.connect(self.open_window)
# Creates the menu
self.menu_bar=self.menuBar()
self.menu1=self.menu_bar.addMenu('&Menu 1')
self.menu1.addAction(self.new_window)
# Creates a QMdiArea to manage all windows.
self.wmanager=QtGui.QMdiArea()
self.wmanager.setViewMode(QtGui.QMdiArea.TabbedView)
self.setCentralWidget(self.wmanager)
self.showMaximized()
# Opens the new window that holds the QTableView
def open_window(self):
t_wdw.launch_window()
t_wdw.window_alpha=self.wmanager.addSubWindow(t_wdw.window)
t_wdw.window_alpha.show()
def main():
app=QtGui.QApplication(sys.argv)
main_wdw=Main_Window()
sys.exit(app.exec_())
if __name__=="__main__":
main()
代码为t_wdw
负责创建的模块SubWindow
与QTableView
.
from PySide import QtGui
from PySide import QtCore
def launch_window():
global window
# Creates a new window
window=QtGui.QScrollArea()
window1=QtGui.QWidget()
row=0
data=[["VH"+str(row+i),1,2,3] for i in range(20)]
headers=["HH1","HH2","HH3"]
# Creates a table view with columns resized to fit the content.
model=my_table(data,headers)
tableView=QtGui.QTableView()
tableView.setModel(model)
tableView.resizeColumnsToContents()
# Fixes the width of columns and the height of rows.
tableView.horizontalHeader().setResizeMode(QtGui.QHeaderView.Fixed)
tableView.verticalHeader().setResizeMode(QtGui.QHeaderView.Fixed)
"""
Here is the solution I resorted to to fix the tableView width
equal to its content and that I want to make simpler
"""
vhwidth=tableView.verticalHeader().width()
desired_width=QtGui.QStyle.PM_ScrollBarExtent*2+vhwidth+1
for i in range(len(headers)):
desired_width+=tableView.columnWidth(i)
tableView.setFixedWidth(desired_width)
"""
Sets the layouts and widgets using a QHBoxLayout because
I want the QTableView to be centered on the window
and more widgets and layouts are to be added later.
"""
window1.main_layout=QtGui.QHBoxLayout()
window1.main_layout.addStretch(0)
window1.main_layout.addWidget(tableView)
window1.main_layout.addStretch(0)
window.setLayout(window1.main_layout)
window.setWidget(window1)
class my_table(QtCore.QAbstractTableModel):
def __init__(self,data,headers,parent=None):
QtCore.QAbstractTableModel.__init__(self,parent)
self.__data=data
self.__headers=headers
def rowCount(self,parent):
return len(self.__data)
def columnCount(self,parent):
return len(self.__headers)
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
return self.__data[row][column+1]
def headerData(self,section,orientation,role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self.__headers[section]
if orientation == QtCore.Qt.Vertical:
return self.__data[section][0]
对于那些阅读代码的人来说,还有一个额外的问题:为什么我需要乘法PM_ScrollBarExtent
2 才能得到正确的结果?
P.S:我正在使用 PySide 1.2.1,但 PyQt 或 C++ 中的答案很好。