- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 PyQt 的新手,我正在 PyQt4 和 Python 2.7 中制作一个 GUI,它(除其他外)控制少数微 Controller 的设置。为了轻松填充和显示设置列表,以及它们来自哪个 Controller ,我将其放入 QTableWidget 中,其中列是 Controller ,行是设置(所有 Controller 只是彼此重复,但并非所有设置都会 Controller 之间的值相同)。
我遇到的问题是,当用户编辑单元格时,我尝试使用
value = self.Settings1Table.item(n, s).text()
但这只会检索我在填充期间放置在单元格中的值,而不是刚刚通过键盘输入的值。我读过一个方法 currentText()
但据我了解,它要求每个单元格都是它自己的小部件,我不确定这是如何完成的。
整个代码比较大,我觉得没必要全部贴出来,但如果需要更多的代码,我会很乐意提供。谢谢你的帮助,我希望能来。
编辑:这是遍历表中每个项目的方法,它应该获取当前值,但现在只返回我通过 item.setText(str) 设置的值(需要是值用户通过键盘输入)
def ApplyAll1(self):
if not self.CheckHealth():
for s in xrange(NumOfSegs):
n = 0
for item in Settings1List:
value = self.Settings1Table.item(n, s).text()
print value
else:
self.MsgCntrField.setText("CONNECTION ERROR")
self.CheckHealth() 只是错误检查
最佳答案
最后更新时间:2014 年 8 月 19 日 0 : 29
I don't really care about the event, since I am going to loop through the entire table, I do need the data changed by the keyboard instead of 'QtGui.QTableWidgetItem.setText' so yes
好的,它可能只创建事件然后键盘,但是你必须实现 QtGui.QTableWidget.focusInEvent(self, eventQFocusEvent)
和 QtGui.QTableWidget.focusOutEvent(self, eventQFocusEvent)
。所以,请查看示例代码,希望对您有所帮助;
import sys
from PyQt4 import QtGui, QtCore
class QCustomTableWidget (QtGui.QTableWidget):
def __init__ (self, parent = None):
super(QCustomTableWidget, self).__init__(parent)
self.focusKeyboardOn = False
# Setup row & column data
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5']
self.setRowCount(len(listsVerticalHeaderItem))
for index in range(self.rowCount()):
self.setVerticalHeaderItem(index, QtGui.QTableWidgetItem(listsVerticalHeaderItem[index]))
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4']
self.setColumnCount(5)
listsHorizontalHeaderItem = ['Option 1', 'Option 2']
self.setColumnCount(len(listsHorizontalHeaderItem))
for index in range(self.columnCount()):
self.setHorizontalHeaderItem(index, QtGui.QTableWidgetItem(listsHorizontalHeaderItem[index]))
def dataChanged (self, topLeftQModelIndex, bottomRightQModelIndex):
row = topLeftQModelIndex.row()
column = topLeftQModelIndex.column()
dataQTableWidgetItem = self.item(row, column)
if (self.currentItem() == dataQTableWidgetItem) and (self.focusKeyboardOn == True):
self.emit(QtCore.SIGNAL('currentKeyboardDataChanged'), row, column, dataQTableWidgetItem)
self.emit(QtCore.SIGNAL('dataChanged'), row, column, dataQTableWidgetItem)
QtGui.QTableWidget.dataChanged(self, topLeftQModelIndex, bottomRightQModelIndex)
def focusInEvent (self, eventQFocusEvent):
self.focusKeyboardOn = False
QtGui.QTableWidget.focusInEvent(self, eventQFocusEvent)
def focusOutEvent (self, eventQFocusEvent):
self.focusKeyboardOn = True
QtGui.QTableWidget.focusOutEvent(self, eventQFocusEvent)
class QCustomWidget (QtGui.QWidget):
def __init__(self, parent = None):
super(QCustomWidget, self).__init__(parent)
self.myQCustomTableWidget = QCustomTableWidget(self)
self.myQLabel = QtGui.QLabel('Track edited data', self)
myQVBoxLayout = QtGui.QVBoxLayout()
myQVBoxLayout.addWidget(self.myQLabel)
myQVBoxLayout.addWidget(self.myQCustomTableWidget)
self.setLayout(myQVBoxLayout)
self.connect(self.myQCustomTableWidget, QtCore.SIGNAL('currentKeyboardDataChanged'), self.setTrackData)
self.myQCustomTableWidget.setItem(0, 0, QtGui.QTableWidgetItem('Test'))
self.myQCustomTableWidget.setItem(1, 1, QtGui.QTableWidgetItem('Work'))
def setTrackData (self, row, column, dataQTableWidgetItem):
self.myQLabel.setText('Last updated\nRow : %d, Column : %d, Data : %s' % (row + 1, column + 1, str(dataQTableWidgetItem.text())))
if __name__ == '__main__':
myQApplication = QtGui.QApplication(sys.argv)
myQCustomWidget = QCustomWidget()
myQCustomWidget.show()
sys.exit(myQApplication.exec_())
注意:它有 1 个条件有 BUG:如果您在通过键盘编辑时通过“QtGui.QTableWidgetItem.setText”设置。但是,如果您严格要求,我建议创建您自己的小部件并设置您自己的项目委托(delegate)。 (但是,很硬核……)
我不知道你的单元格中的数据是什么。 (这是另一个自定义 QWidget
或者只是普通数据 QTableWidgetItem
)
无论如何,当用户编辑单元格时,您尝试使用此方法获取新值 QAbstractItemView.dataChanged (self, QModelIndex topLeft, QModelIndex bottomRight)
。此方法返回数据的位置已编辑,您可以使用 QTableWidgetItem QTableWidget.item (self, int row, int column)
从索引中获取数据。 (那是你说的问题)但是这项工作只有编辑已经关闭(不是在编辑期间)。
例子;
import sys
from PyQt4 import QtGui, QtCore
class QCustomTableWidget (QtGui.QTableWidget):
def __init__ (self, parent = None):
super(QCustomTableWidget, self).__init__(parent)
# Setup row & column data
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5']
self.setRowCount(len(listsVerticalHeaderItem))
for index in range(self.rowCount()):
self.setVerticalHeaderItem(index, QtGui.QTableWidgetItem(listsVerticalHeaderItem[index]))
listsVerticalHeaderItem = ['Device 1', 'Device 2', 'Device 3', 'Device 4']
self.setColumnCount(5)
listsHorizontalHeaderItem = ['Option 1', 'Option 2']
self.setColumnCount(len(listsHorizontalHeaderItem))
for index in range(self.columnCount()):
self.setHorizontalHeaderItem(index, QtGui.QTableWidgetItem(listsHorizontalHeaderItem[index]))
def dataChanged (self, topLeftQModelIndex, bottomRightQModelIndex):
row = topLeftQModelIndex.row()
column = topLeftQModelIndex.column()
dataQTableWidgetItem = self.item(row, column)
print '###### Data Changed ######'
print 'row :', row + 1
print 'column :', column + 1
print 'data :', dataQTableWidgetItem.text()
self.emit(QtCore.SIGNAL('dataChanged'), row, column, dataQTableWidgetItem)
QtGui.QTableWidget.dataChanged(self, topLeftQModelIndex, bottomRightQModelIndex)
class QCustomWidget (QtGui.QWidget):
def __init__(self, parent = None):
super(QCustomWidget, self).__init__(parent)
self.myQCustomTableWidget = QCustomTableWidget(self)
self.myQLabel = QtGui.QLabel('Track edited data', self)
myQVBoxLayout = QtGui.QVBoxLayout()
myQVBoxLayout.addWidget(self.myQLabel)
myQVBoxLayout.addWidget(self.myQCustomTableWidget)
self.setLayout(myQVBoxLayout)
self.connect(self.myQCustomTableWidget, QtCore.SIGNAL('dataChanged'), self.setTrackData)
def setTrackData (self, row, column, dataQTableWidgetItem):
self.myQLabel.setText('Last updated\nRow : %d, Column : %d, Data : %s' % (row + 1, column + 1, str(dataQTableWidgetItem.text())))
if __name__ == '__main__':
myQApplication = QtGui.QApplication(sys.argv)
myQCustomWidget = QCustomWidget()
myQCustomWidget.show()
sys.exit(myQApplication.exec_())
QAbstractItemView.dataChanged (self, QModelIndex topLeft, QModelIndex bottomRight)
引用:http://pyqt.sourceforge.net/Docs/PyQt4/qabstractitemview.html#dataChanged
问候,
关于python - PyQt QTableWidget 检索编辑字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25351754/
我现在已经用 PyQt 做了几个项目,而且我越来越熟悉 Qt 采用的模型/ View 思想流派。我已经将它用于列表和表格 View 之类的东西,它们背后有一个自定义模型来显示和操作数据。我使用委托(d
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class MainWindow(QMainWindow):
使用下面的示例代码(受 here 的严重影响),右键单击上下文菜单并没有真正正确对齐。 从屏幕截图中可以看出,生成的菜单在鼠标光标上方相当多的位置。我希望菜单的左上角与鼠标指针完全对齐。 有没有办法对
所以我创建了一个自定义上下文菜单,但我想根据某些值将树小部件的某些行中的某些项目灰显。如何禁用菜单上的项目? myUI.setContextMenuPolicy( Qt.CustomContextMe
是否可以禁用 QComboBox在 pyqt 中,就像我们可以在 Win Forms(C#) 中那样做,因为我在 QComboBox 中找不到任何选项手动的。我想启用 QcomboBox仅当管理员登录
我想将 QComboBox 与元组中的“键”和“值”一起使用,该元组类似于 django 模型中使用的元组。例如,我对一个人的性别有以下结构。 SEX_CHOICES = (('M', 'Male')
是否可以让 Altair 或 Vega(-Lite) 渲染到 PyQt 小部件,类似于支持多个后端的 Matplotlib?我知道我可以使用 Qt WebView 小部件来呈现带有 Vega 嵌入的网
在下面的示例代码中(受 here 的影响很大),我希望选择单击单元格的整行而不是单个单元格。如何更改代码以合并它? import re import operator import os import
我正在尝试禁用关闭“x”按钮,并且我认为通过将 DockWidgetFeature 设置为仅可移动和可 float 即可工作。 def CreateDockWidget (self): Pan
我已经按照 Yasin Uludag 的一些有用的在线教程来尝试使用 PyQt(或者更确切地说是 PySide)来创建一个简单的 TreeView ,但是我在使用工具提示时遇到了问题。在以下代码中,工
我正在尝试创建一个场景,我需要从 mousePressEvent 位置画线到最新的鼠标 moveposition 这意味着我需要调用 paintEvent 来自 mousePressEvent ,这可
是Python 3的组合和 PyQt 4受到推崇的?有没有其他选择? 最佳答案 我不明白为什么不,有一个 version available对于正常工作的 Python 3,如果你真的需要 Qt,唯一
我正在尝试显示从二进制文件中读取的图像数据(我编写了用于从文件中检索此数据并将其存储为图像以供 QImage() 使用的代码)。我想做的是将 slider 连接到图形 View 小部件,以便当您移动
我已经准备了很多关于如何在 python 和 pyqt 中将多个信号连接到同一个事件处理程序的帖子。例如,将多个按钮或组合框连接到同一功能。 许多示例展示了如何使用 QSignalMapper 执行此
我有一个 PyQt 主窗口,当用户按下某个按钮时,我需要从中获取一串用户输入。 这是我的用户输入窗口代码: class InputDialog(QtGui.QDialog): ''' t
编辑: 以下 buildout.cfg 用于构建 Qt、PyQt 和 SIP [buildout] parts = pyqt [pyqt] recipe = zc.recipe.cmmi ur
我目前正在开发一个应用程序,该应用程序可以使用 PyQt 访问 sqlalchemy 数据库并将其内容显示到 TableView 或其他一些小部件中。现在为了简单起见,我们只说这是一个电话簿,上面有姓
使用我现在拥有的代码,我可以成功地播放文件中的 .mp3 数据。但是我需要使用 QtCore.QBuffer(不是来自文件)播放相同的数据。当我使用文档的示例时,它会出现意外类型的 QBuffer 错
安装 sip 后,我在尝试安装 PyQt-x11-gpl-4.11 时不断收到这个可爱的错误消息。 mycommandline$ python configure-ng.py --verbose Qu
我正在为一个项目使用 PyQt。但并非突然间我收到一个错误: QPixmap: It is not safe to use pixmaps outside the GUI thread in PyQt
我是一名优秀的程序员,十分优秀!