如何在 QStatusBar 中均匀分布 QLabel
How to distribute QLabels in QStatusBar evenly
我正在尝试以与 QTableView 中的列相同的方式(按列宽)均匀分布 QLabel。
因此,我想替换 QLabels 中的字符串 (data.columns.values.tolist()[index]) for "count: CountofRows" for columns that cannot be summed up and for "sum: SumofRows" for columns可以这样总结。如果可能,只要使用过滤器 (QSortFilterProxyModel),就应该更新它。
我已经阅读了这些帖子:post1 and 但仍然无法完成这项工作。
这是我的代码,在这个阶段它只是在 QStatusBar 中填充 QLabels:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from pandas import *
import sys
class TBWindow(QMainWindow):
def __init__(self, parent=None):
super(TBWindow, self).__init__(parent)
fList = [['Ajvi', 'Ostatní textil', 'Holesovice', 1, 2],
['Ajvi', 'Sukně', 'Holesovice', 2, 5],
['Ajvi', 'Šaty', 'Holesovice', 2, 0],
['Belusi', 'Brože', 'Fler', 0, 1],
['Belusi', 'Brože', 'Holesovice', 0, 5],
['PP', 'Sukně', 'E-shop', 25, 10],
['PP', 'Sukně', 'Fler', 7, 6],
['PP', 'Sukně', 'Holesovice', 15, 13],
['PP', 'Sukně', 'Other', 6, 2],
['PP', 'Sukně', 'Sashe', 6, 2],
['PP', 'Tašky', 'E-shop', 2, 1],
['PP', 'Tašky', 'Holesovice', 3, 1],
['PP', 'Šaty', 'E-shop', 1, 0]]
data = DataFrame(fList, columns = ['Seller', 'Section', 'Store', 'Total_pieces: 2017', 'Total_pieces: 2018'])
self.setWindowTitle('Window')
self.centralwidget = QWidget(self)
self.lineEdit = QLineEdit(self.centralwidget)
self.view = QTableView(self.centralwidget)
self.comboBox = QComboBox(self.centralwidget)
self.label = QLabel(self.centralwidget)
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
self.gridLayout.addWidget(self.view, 1, 0, 1, 3)
self.gridLayout.addWidget(self.comboBox, 0, 2, 1, 1)
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.setCentralWidget(self.centralwidget)
self.label.setText("Filter")
self.model = PandasModel(data)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self.model)
self.view.setModel(self.proxy)
for column in range(self.view.horizontalHeader().count()):
self.view.horizontalHeader().setSectionResizeMode(column, QHeaderView.Stretch)
self.comboBox.addItems(list(data.columns.values))
self.lineEdit.textChanged.connect(self.on_lineEdit_textChanged)
self.comboBox.currentIndexChanged.connect(self.on_comboBox_currentIndexChanged)
## mWdg = QWidget()
## mWdg.setLayout(QHBoxLayout())
## for index, element in enumerate(data.columns.values):
## mWdg.layout().addWidget(QLabel(data.columns.values.tolist()[index]))
## self.statusBar().addPermanentWidget(mWdg)
for index, element in enumerate(data.columns.values):
self.statusBar().addWidget(QLabel(data.columns.values.tolist()[index]), index)
@pyqtSlot(str)
def on_lineEdit_textChanged(self, text):
search = QRegExp(text, Qt.CaseInsensitive, QRegExp.RegExp)
self.proxy.setFilterRegExp(search)
@pyqtSlot(int)
def on_comboBox_currentIndexChanged(self, index):
self.proxy.setFilterKeyColumn(index)
class PandasModel(QAbstractTableModel):
def __init__(self, data, parent=None):
QAbstractTableModel.__init__(self, parent)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[1]
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
main = TBWindow()
main.showMaximized()
sys.exit(app.exec_())
对如何进行有什么建议吗?
QStatusBar.addPermanentWidget(小部件[ stretch=0])
参数:
小部件 – QWidget
拉伸 – QtCore.int
将给定的小部件永久添加到此状态栏,如果它还不是此 QStatusBar 对象的子对象,则重新设置小部件的父级。 stretch 参数用于在状态栏增长和收缩时为给定的小部件计算合适的大小。默认拉伸因子为 0,即给小部件的最小值为 space.
mWdg = QWidget()
mWdg.setLayout(QHBoxLayout())
for index, element in enumerate(data.columns.values):
mWdg.layout().addWidget(QLabel(data.columns.values.tolist()[index]))
self.statusBar().addPermanentWidget(mWdg, 1) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#for index, element in enumerate(data.columns.values):
# self.statusBar().addWidget(QLabel(data.columns.values.tolist()[index]), index)
我正在尝试以与 QTableView 中的列相同的方式(按列宽)均匀分布 QLabel。
因此,我想替换 QLabels 中的字符串 (data.columns.values.tolist()[index]) for "count: CountofRows" for columns that cannot be summed up and for "sum: SumofRows" for columns可以这样总结。如果可能,只要使用过滤器 (QSortFilterProxyModel),就应该更新它。
我已经阅读了这些帖子:post1 and
这是我的代码,在这个阶段它只是在 QStatusBar 中填充 QLabels:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from pandas import *
import sys
class TBWindow(QMainWindow):
def __init__(self, parent=None):
super(TBWindow, self).__init__(parent)
fList = [['Ajvi', 'Ostatní textil', 'Holesovice', 1, 2],
['Ajvi', 'Sukně', 'Holesovice', 2, 5],
['Ajvi', 'Šaty', 'Holesovice', 2, 0],
['Belusi', 'Brože', 'Fler', 0, 1],
['Belusi', 'Brože', 'Holesovice', 0, 5],
['PP', 'Sukně', 'E-shop', 25, 10],
['PP', 'Sukně', 'Fler', 7, 6],
['PP', 'Sukně', 'Holesovice', 15, 13],
['PP', 'Sukně', 'Other', 6, 2],
['PP', 'Sukně', 'Sashe', 6, 2],
['PP', 'Tašky', 'E-shop', 2, 1],
['PP', 'Tašky', 'Holesovice', 3, 1],
['PP', 'Šaty', 'E-shop', 1, 0]]
data = DataFrame(fList, columns = ['Seller', 'Section', 'Store', 'Total_pieces: 2017', 'Total_pieces: 2018'])
self.setWindowTitle('Window')
self.centralwidget = QWidget(self)
self.lineEdit = QLineEdit(self.centralwidget)
self.view = QTableView(self.centralwidget)
self.comboBox = QComboBox(self.centralwidget)
self.label = QLabel(self.centralwidget)
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
self.gridLayout.addWidget(self.view, 1, 0, 1, 3)
self.gridLayout.addWidget(self.comboBox, 0, 2, 1, 1)
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.setCentralWidget(self.centralwidget)
self.label.setText("Filter")
self.model = PandasModel(data)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self.model)
self.view.setModel(self.proxy)
for column in range(self.view.horizontalHeader().count()):
self.view.horizontalHeader().setSectionResizeMode(column, QHeaderView.Stretch)
self.comboBox.addItems(list(data.columns.values))
self.lineEdit.textChanged.connect(self.on_lineEdit_textChanged)
self.comboBox.currentIndexChanged.connect(self.on_comboBox_currentIndexChanged)
## mWdg = QWidget()
## mWdg.setLayout(QHBoxLayout())
## for index, element in enumerate(data.columns.values):
## mWdg.layout().addWidget(QLabel(data.columns.values.tolist()[index]))
## self.statusBar().addPermanentWidget(mWdg)
for index, element in enumerate(data.columns.values):
self.statusBar().addWidget(QLabel(data.columns.values.tolist()[index]), index)
@pyqtSlot(str)
def on_lineEdit_textChanged(self, text):
search = QRegExp(text, Qt.CaseInsensitive, QRegExp.RegExp)
self.proxy.setFilterRegExp(search)
@pyqtSlot(int)
def on_comboBox_currentIndexChanged(self, index):
self.proxy.setFilterKeyColumn(index)
class PandasModel(QAbstractTableModel):
def __init__(self, data, parent=None):
QAbstractTableModel.__init__(self, parent)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[1]
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
main = TBWindow()
main.showMaximized()
sys.exit(app.exec_())
对如何进行有什么建议吗?
QStatusBar.addPermanentWidget(小部件[ stretch=0])
参数: 小部件 – QWidget 拉伸 – QtCore.int
将给定的小部件永久添加到此状态栏,如果它还不是此 QStatusBar 对象的子对象,则重新设置小部件的父级。 stretch 参数用于在状态栏增长和收缩时为给定的小部件计算合适的大小。默认拉伸因子为 0,即给小部件的最小值为 space.
mWdg = QWidget()
mWdg.setLayout(QHBoxLayout())
for index, element in enumerate(data.columns.values):
mWdg.layout().addWidget(QLabel(data.columns.values.tolist()[index]))
self.statusBar().addPermanentWidget(mWdg, 1) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#for index, element in enumerate(data.columns.values):
# self.statusBar().addWidget(QLabel(data.columns.values.tolist()[index]), index)