本文整理匯總了Python中PyQt5.QtWidgets.QLineEdit.setSelection方法的典型用法代碼示例。如果您正苦於以下問題:Python QLineEdit.setSelection方法的具體用法?Python QLineEdit.setSelection怎麽用?Python QLineEdit.setSelection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PyQt5.QtWidgets.QLineEdit
的用法示例。
在下文中一共展示了QLineEdit.setSelection方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: StatusBar
# 需要導入模塊: from PyQt5.QtWidgets import QLineEdit [as 別名]
# 或者: from PyQt5.QtWidgets.QLineEdit import setSelection [as 別名]
class StatusBar (QStatusBar):
def __init__(self):
super(StatusBar, self).__init__()
self.__statusMessageLabel = QLabel(self)
self.__statusDataLabel = QLabel(self)
self.__commandLine = QLineEdit(self)
self.addPermanentWidget(self.__statusMessageLabel, 1)
self.addPermanentWidget(self.__commandLine, 1)
self.addPermanentWidget(self.__statusDataLabel)
self.__commandLine.hide()
def setStatus(self, statusMessage, statusData, cursorPosition, cursorAnchor, eventFilter):
commandMode = cursorPosition != -1
self.__commandLine.setVisible(commandMode)
self.__statusMessageLabel.setVisible(not commandMode)
if commandMode:
self.__commandLine.installEventFilter(eventFilter)
self.__commandLine.setFocus()
self.__commandLine.setText(statusMessage)
self.__commandLine.setSelection(cursorPosition, cursorAnchor - cursorPosition)
else:
self.__statusMessageLabel.setText(statusMessage)
self.__statusDataLabel.setText(statusData)
示例2: IntroPage
# 需要導入模塊: from PyQt5.QtWidgets import QLineEdit [as 別名]
# 或者: from PyQt5.QtWidgets.QLineEdit import setSelection [as 別名]
class IntroPage(QWizardPage):
def __init__(self):
super(IntroPage, self).__init__()
self.setTitle(self.tr("New Database"))
self.setSubTitle(self.tr("Name and Location"))
grid = QGridLayout(self)
grid.addWidget(QLabel(self.tr("Database Name: ")), 0, 0)
self._line_dbname = QLineEdit()
self._line_dbname.textChanged.connect(self.__on_dbname_changed)
grid.addWidget(self._line_dbname, 0, 1)
grid.addWidget(QLabel(self.tr("Database Location: ")), 1, 0)
self._line_dblocation = LineEdit()
self._line_dblocation.button.clicked.connect(self.__select_location)
grid.addWidget(self._line_dblocation, 1, 1)
grid.addWidget(QLabel(self.tr("Database Folder: ")), 2, 0)
self._dbfolder = settings.PIREAL_DATABASES
self._line_dbfilename = QLineEdit()
self._line_dbfilename.setText(self._dbfolder)
grid.addWidget(self._line_dbfilename, 2, 1)
self._line_dbname.setText("pireal_database.pdb")
self._line_dbname.setSelection(0, 15)
def __select_location(self):
location = QFileDialog.getExistingDirectory(self,
self.tr("Select Folder"))
if not location:
return
self._line_dblocation.setText(location)
self._dbfolder = os.path.join(self._dbfolder, location)
self._line_dbfilename.setText(
os.path.join(location, self._line_dbname.text()))
def __on_dbname_changed(self, db_name):
self._line_dbfilename.setText(os.path.join(self._dbfolder, db_name))
def get_data(self):
data = {
'name': self._line_dbname.text(),
'location': self._line_dblocation.text(),
'filename': self._line_dbfilename.text(),
}
return data
示例3: RenameDialog
# 需要導入模塊: from PyQt5.QtWidgets import QLineEdit [as 別名]
# 或者: from PyQt5.QtWidgets.QLineEdit import setSelection [as 別名]
class RenameDialog(QDialog):
def __init__(self, parent: QWidget) -> None:
super().__init__(parent)
self._basename: str = ""
self._build_gui()
def _build_gui(self) -> None:
self.resize(600, 100)
self.setWindowTitle("RenameDialog")
self.setWindowModality(Qt.WindowModal)
# Widgets
icon_label = QLabel()
icon_label.setPixmap(QIcon.fromTheme("accessories-text-editor").pixmap(48))
icon_label.setAlignment(Qt.AlignTop)
self.label = QLabel("Rename ...")
self.label.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.name_edit = QLineEdit(self)
self.name_reload = self.name_edit.addAction(QIcon.fromTheme("reload"), QLineEdit.TrailingPosition)
self.name_reload.setShortcut("F5")
self.name_reload.setToolTip("Reset the filename to it's original name")
self.button_box = QDialogButtonBox(self)
self.btn_rename = QPushButton(QIcon.fromTheme("document-save-as"), "Rename")
self.button_box.addButton(self.btn_rename, QDialogButtonBox.AcceptRole)
self.btn_cancel = self.button_box.addButton(QDialogButtonBox.Cancel)
self.btn_rename.setDefault(True)
# layout
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.label)
self.vbox.addWidget(self.name_edit)
self.vbox.addStretch()
self.vbox.addWidget(self.button_box)
hbox = QHBoxLayout()
hbox.addWidget(icon_label)
hbox.addLayout(self.vbox)
self.setLayout(hbox)
# signals
self.name_edit.returnPressed.connect(self.accept)
self.btn_rename.clicked.connect(self._on_rename_clicked)
self.btn_cancel.clicked.connect(self._on_cancel_clicked)
self.name_reload.triggered.connect(self._on_name_reload)
def _on_rename_clicked(self) -> None:
self.accept()
def _on_cancel_clicked(self) -> None:
self.reject()
def _on_name_reload(self) -> None:
self.name_edit.setText(self._basename)
root, ext = os.path.splitext(self._basename)
self.name_edit.setSelection(0, len(root))
def get_old_basename(self) -> str:
return self._basename
def get_new_basename(self) -> str:
return cast(str, self.name_edit.text())
def set_basename(self, basename: str) -> None:
self._basename = basename
self.setWindowTitle("Rename \"{}\"".format(basename))
self.label.setText("Rename \"{}\" to:".format(basename))
self.name_edit.setText(basename)
root, ext = os.path.splitext(basename)
self.name_edit.setSelection(0, len(root))
示例4: LigneCommande
# 需要導入模塊: from PyQt5.QtWidgets import QLineEdit [as 別名]
# 或者: from PyQt5.QtWidgets.QLineEdit import setSelection [as 別名]
class LigneCommande(QWidget):
"""Un TextCtrl muni d'un historique et associé à un bouton pour valider.
On peut personnaliser le texte du bouton (via `texte="Mon texte"`),
ou même directement le bouton, en utilisant `bouton=...`.
"""
def __init__(self, parent, longueur = 500, texte = None, bouton = None,
action = (lambda *args, **kw: True), afficher_bouton = True,
legende = None):
self.parent = parent
self.action = action
QWidget.__init__(self, parent)
sizer = QHBoxLayout()
self.texte = QLineEdit()
self.texte.setMinimumWidth(longueur)
if bouton is None:
self.bouton = QPushButton('OK' if texte is None else texte)
else:
self.bouton = bouton
self.bouton.setVisible(afficher_bouton)
self.bouton.clicked.connect(self.valider)
if legende is not None:
sizer.addWidget(QLabel(legende, self))
sizer.addWidget(self.texte)
sizer.addWidget(self.bouton)
self.setLayout(sizer)
self.initialiser()
def initialiser(self):
self.historique = []
self.position = None
self.setFocus()
self.clear()
def text(self):
return self.texte.text()
def setText(self, value):
self.texte.setText(value)
def setFocus(self):
self.texte.setFocus()
def clear(self):
self.texte.clear()
def getSelection(self):
if self.texte.hasSelectedText():
start = self.texte.selectionStart()
length = len(self.texte.selectedText())
end = start + length
else:
start = end = self.texte.cursorPosition()
return start, end
def cursorPosition(self):
return self.texte.cursorPosition()
def setCursorPosition(self, num):
return self.texte.setCursorPosition(num)
def insert(self, texte):
self.texte.insert(texte)
def setSelection(self, deb, fin):
self.texte.setSelection(deb, fin)
def setToolTip(self, tip):
self.texte.setToolTip(tip)
def valider(self, **kw):
# kw: shift|alt|meta|control=True|False'
commande = str(self.text())
self.position = None
if commande:
self.historique.append(commande)
elif self.historique:
# Appuyer une deuxième fois sur [Entrée] permet de répéter l'action précédente.
commande = self.historique[-1]
self.action(commande, **kw)
def keyPressEvent(self, event):
key = event.key()
commande = self.text()
if key == Qt.Key_Up and self.historique:
# On remonte dans l'historique (-> entrées plus anciennes)
if self.position is None:
# cas d'une commande en cours d'édition :
if commande:
if commande != self.historique[-1]:
# on enregistre la commande en cours
self.historique.append(commande)
self.position = len(self.historique) - 1
else:
self.position = len(self.historique)
#.........這裏部分代碼省略.........