Reputation: 428
This is my code,when i click a space bar pushbutton i want to move a cursor forward to one position in line edit.
how to move the cursor one forward position in line edit.
Given bellow is my code:
import sys
from pyface.qt import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.title = QtGui.QLabel('Title')
self.titleEdit = QtGui.QLineEdit()
self.btn = QtGui.QPushButton("spacebar")
self.btn.clicked.connect(self.spacebar)
grid = QtGui.QGridLayout()
grid.setSpacing(10)
grid.addWidget(self.title, 1, 0)
grid.addWidget(self.titleEdit, 1, 1)
grid.addWidget(self.btn, 2, 1)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Review')
self.show()
def spacebar(self):
self.titleEdit.cursorForward(True,int=1)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Upvotes: 0
Views: 185
Reputation: 243897
For these cases you must send a QKeyEvent
and establish the focus after sending it:
def spacebar(self):
key_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_Space, QtCore.Qt.NoModifier, " ")
QtCore.QCoreApplication.sendEvent(self.titleEdit, key_event)
QtCore.QTimer.singleShot(0, self.titleEdit.setFocus)
Upvotes: 1