PyQt5学习笔记一

Q: 如何使窗口置顶?

QtCore.Qt.WindowStaysOnTopHint

Example

# -*- coding: utf-8 -*-

import sys

from PyQt5.QtWidgets import QApplication , QMainWindow

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(368, 164)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

if __name__ == '__main__':
    '''
    主函数
    '''

    app = QApplication(sys.argv)
    mainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(mainWindow)
    mainWindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
    mainWindow.show()
    sys.exit(app.exec_())

Q: QTextCursor 如何使用?

API参考

前提条件:获得指针对象

textCursor = self.textEdit.textCursor()
  • 获取指定区间的文本(相对于整篇文档)

    textCursor.setPosition(3, QTextCursor.MoveAnchor)
    textCursor.setPosition(9, QTextCursor.KeepAnchor)
    lineContent = textCursor.selectedText()

str = ‘0123456789’
3-9之间的字符为 345678

  • 获取光标所在行的文本

    textCursor.select(QTextCursor.LineUnderCursor)
    lineContent = textCursor.selectedText()
  • 获取光标所在行的文本的前5个字符

    textCursor.movePosition(QTextCursor.StartOfLine)
    end = textCursor.position() + 5
    textCursor.setPosition(end, QTextCursor.KeepAnchor)
    原文作者:ZeronoFreya
    原文地址: https://segmentfault.com/a/1190000009814946
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞