我在使用React表单并正确管理状态时遇到问题.我在表单中有一个时间输入字段(在模态中).初始值设置为状态变量getInitialState,并从父组件传入.这本身就可以.
当我想通过父组件更新默认的start_time值时,问题出现了.更新本身发生在父组件中setState start_time: new_time.但是在我的表单中,默认的start_time值永远不会改变,因为它只定义一次getInitialState.
我试图用来componentWillUpdate强制改变状态setState start_time: next_props.start_time,这确实有效,但给了我Uncaught RangeError: Maximum call stack size exceeded错误.
所以我的问题是,在这种情况下更新状态的正确方法是什么?我是否以某种方式思考这个错误?
现行代码:
@ModalBody = React.createClass
getInitialState: ->
start_time: @props.start_time.format("HH:mm")
#works but takes long and causes:
#"Uncaught RangeError: Maximum call stack size exceeded"
componentWillUpdate: (next_props, next_state) ->
@setState(start_time: next_props.start_time.format("HH:mm"))
fieldChanged: (fieldName, event) ->
stateUpdate = {}
stateUpdate[fieldName] = event.target.value
@setState(stateUpdate)
render: ->
React.DOM.div
className: "modal-body"
React.DOM.form null,
React.createElement FormLabelInputField,
type: "time"
id: "start_time"
label_name: "Start Time" …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在我的Python项目中实现一些继承,并且遇到了障碍.我正在尝试创建一个baseParentClass,它将处理许多子类的基本功能.在这个具体的例子中,我试图初始化一个具有许多属性(设置为0)的实例,存储为Child中的类变量列表(称为ATTRS).我不确定如何在父类中使用此ATTRS.
class Parent(object):
def __init__():
for attr in ATTRS:
setattr(self, attr, 0)
class Child(Parent):
#class variable
ATTRS = [attr1, attr2, attr3]
def __init__():
super(Child, self).__init__()
Run Code Online (Sandbox Code Playgroud)
我可以将ATTRS作为self.ATTRS存储在Child中,然后在Parent中成功使用self.ATTRS,但是将它们存储为类变量似乎更为可取.
或者我可以将ATTRS作为参数传递给:
class Child(Parent):
#class variable
ATTRS = [attr1, attr2, attr3]
def __init__():
super(Child, self).__init__(ATTRS)
Run Code Online (Sandbox Code Playgroud)
但我想知道这是否在某种程度上打败了使用继承的重点?
我会感激任何想法,提示或反馈我是否完全咆哮错误的树!
谢谢
我遇到了以下问题.我正在尝试将lambda函数连接到Signal以最终传递一些额外的数据.
def createTimeComboBox(self,slotCopy):
timeComboBox = QComboBox()
#...
cmd = lambda func=self.test:func()
self.connect(timeComboBox, SIGNAL("currentIndexChanged(int)"),cmd)
#...
def test(self, value):
print value
Run Code Online (Sandbox Code Playgroud)
当我运行时,createTimeComboBox(),我收到此错误:
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud)
更改
self.connect(timeComboBox, SIGNAL("currentIndexChanged(int)"),cmd)
Run Code Online (Sandbox Code Playgroud)
至
self.connect(timeComboBox, SIGNAL("currentIndexChanged(int)"),self.test)
Run Code Online (Sandbox Code Playgroud)
工作正常,但我希望能够传递slotCopy变量,所以假设我需要使用该lambda方法.
我曾与一个以前做过QPushButton的clicked()信号,并能正常工作.
def createToDoctorButton(self,extraData):
toDoctorButton = QPushButton()
cmd = lambda func=self.goToDoctor:func(extraData)
self.connect(toDoctorButton, SIGNAL('clicked()'),cmd)
return toDoctorButton
def goToDoctor(self,extraData):
print extraData
Run Code Online (Sandbox Code Playgroud)
我希望这是有道理的 - 有没有人有任何想法?谢谢你的任何建议!干杯戴夫
我正在努力实现拖放工作.我希望能够从QPushButton拖放到QTableView的单元格中.我在线查看了一些教程,但似乎第一步陷入困境.下面的例子中,从惊人zetcode教程改变: http://zetcode.com/tutorials/pyqt4/dragdrop/
使用下面的代码,当我将按钮拖动到tableWidget的dragEnterEvent似乎被调用,但是一旦我将鼠标悬停在桌子上,我得到我不能砸在桌子上那个符号,所以绝不能去掉事件:(
我不得不承认我对pyqt很新,所以可能会遗漏一些非常简单的东西.非常感谢我能得到的任何帮助!干杯戴夫
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Button(QtGui.QPushButton):
def __init__(self, title, parent):
super(Button, self).__init__(title, parent)
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.RightButton:
return
mimeData = QtCore.QMimeData()
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.start(QtCore.Qt.MoveAction)
def mousePressEvent(self, e):
QtGui.QPushButton.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
class MyTable(QtGui.QTableWidget):
def __init__(self, rows, columns, parent):
super(MyTable, self).__init__(rows, columns, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
print e.accept()
def dropEvent(self, e):
print 'blah'
position = e.pos() …Run Code Online (Sandbox Code Playgroud)