PyQt allign复选框,并将其放在每一行中 [英] PyQt allign checkbox and put it in every row
问题描述
我正在尝试此与复选框。
可悲的是C ++,Python的任何代码修改都会导致此错误:'QWidget'对象不可调用
我想要做的是在每行上添加一个复选框,这是我的代码:
I'm trying to do this with the check-box.
Sadly is made for C++ and any adaptation of the code for Python has the result this error: 'QWidget' object is not callable
What I wanna to do is to add a check-box on each row, this is my code:
pWidget = QWidget()
pCheckbox = QCheckBox()
pLayout = QVBoxLayout()
pLayout.addWidget(pCheckbox)
pLayout.setAlignment(Qt.AlignCenter)
pLayout.setContentsMargins(0, 0 ,0, 0)
pWidget.setLayout(pLayout)
for char in accounts:
for columnNumber in range(numberColumns):
chkBoxItem = QTableWidgetItem()
chkBoxItem.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
chkBoxItem.setCheckState(Qt.Unchecked)
self.mainAccountTable.insertRow(currentRowCount)
self.mainAccountTable.setItem(currentRowCount, 0, pWidget(chkBoxItem))
self.mainAccountTable.setItem(currentRowCount, 1, QTableWidgetItem(data[1]))
如果我输入 pLayout = Layout( )
表示它不是在Python中实现的。
那么,如何添加居中对齐的复选框,而每一行的文本区域都不在附近?在此先感谢您提供任何提示。
If I put pLayout = Layout()
is saying that it isn't implemented in Python.
So, how I can I add that checkbox aligned in center without the text area near for each row? Thanks in advance for any tips.
后来编辑:
在循环内移动代码是有效的,但我无法控制检查:
Later moving the code inside the loop is working , but I can't control the checks:
for char in accounts:
for columnNumber in range(numberColumns):
pWidget = QWidget()
pCheckbox = QCheckBox()
pLayout = QVBoxLayout(pWidget)
pLayout.addWidget(pCheckbox)
pLayout.setAlignment(Qt.AlignCenter)
pLayout.setContentsMargins(0, 0 ,0, 0)
pWidget.setLayout(pLayout)
#chkBoxItem = QTableWidgetItem()
#chkBoxItem.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
#chkBoxItem.setCheckState(Qt.Unchecked)
self.mainAccountTable.insertRow(currentRowCount)
self.mainAccountTable.setCellWidget(currentRowCount, 0, pWidget)
这是屏幕截图如何现在我检查并建立了集合列表?
This is a screenshoot How can I know what I checked and build set list?
推荐答案
这里是来自ekhumoro的示例来查找被单击时要检查的内容:
Here is an example from ekhumoro to find what is checked when it s being clicked :
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
QtGui.QWidget.__init__(self)
self.table = QtGui.QTableWidget(rows, columns, self)
for column in range(columns):
for row in range(rows):
item = QtGui.QTableWidgetItem('Text%d' % row)
if row % 2:
item.setFlags(QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsEnabled)
item.setCheckState(QtCore.Qt.Unchecked)
self.table.setItem(row, column, item)
self.table.itemClicked.connect(self.handleItemClicked)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.table)
self._list = []
def handleItemClicked(self, item):
if item.checkState() == QtCore.Qt.Checked:
print('"%s" Checked' % item.text())
self._list.append(item.row())
print(self._list)
else:
print('"%s" Clicked' % item.text())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(6, 3)
window.resize(350, 300)
window.show()
sys.exit(app.exec_())
但是您也可以迭代行并使用 .f indChild(type(QtGui.QCheckBox()))。isChecked()
在适当的列上
But you can also iterate on your rows and use .findChild(type(QtGui.QCheckBox())).isChecked()
on the proper column
如:
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
QtGui.QWidget.__init__(self)
self.table = QtGui.QTableWidget(rows, columns, self)
for row in range(rows):
qwidget = QtGui.QWidget()
checkbox = QtGui.QCheckBox()
checkbox.setCheckState(QtCore.Qt.Unchecked)
qhboxlayout = QtGui.QHBoxLayout(qwidget)
qhboxlayout.addWidget(checkbox)
qhboxlayout.setAlignment(Qt.AlignCenter)
qhboxlayout.setContentsMargins(0, 0, 0, 0)
self.table.setCellWidget(row, 0, qwidget)
self.table.setItem(row, 1, QtGui.QTableWidgetItem(str(row)))
layout = QtGui.QVBoxLayout(self)
self.button = QtGui.QPushButton()
self.button.setObjectName("loadButton")
layout.addWidget(self.table)
layout.addWidget(self.button)
self.button.clicked.connect(self.ButtonClicked)
def ButtonClicked(self):
checked_list = []
for i in range(self.table.rowCount()):
if self.table.cellWidget(i, 0).findChild(type(QtGui.QCheckBox())).isChecked():
checked_list.append(self.table.item(i, 1).text())
print checked_list
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window(3, 2)
window.resize(350, 300)
window.show()
sys.exit(app.exec_())
这篇关于PyQt allign复选框,并将其放在每一行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!