有什么办法可以笑着对行进行排序? Pyqt5

时间:2020-02-17 11:32:55

标签: python pyqt5

我在PyQT5中有一个网格,并且按钮可以通过QTextEdit添加更多行,我可以添加一个按钮来按每个QTextEdit值的降序对所有行进行排序吗?

UPD。有最少的代码现在如何工作

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QTime, QTimer, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication, QLCDNumber, QPushButton, QGridLayout, QLabel, QLineEdit, QTextEdit, QGroupBox
import sys
from somethingtestui import Ui_MainWindow

global rowcnt
rowcnt = 1

cntr = []

class mywindow(QtWidgets.QMainWindow):

def __init__(self):
    super(mywindow, self).__init__()
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    self.ui.pushButton.clicked.connect(self.add)
    self.ui.gridLayout.setRowMinimumHeight(50,50)
    self.ui.gridLayout.setColumnMinimumWidth(50,50)
    # self.sort = QPushButton('Sort')
    # self.ui.gridLayout.addWidget(self.sort, 0, 2, 1, 1)
    # self.sort.clicked.connect(self.sbtn)
    self.ui.gridLayout.setVerticalSpacing(0)

def add(self):
    global rowcnt
    self.row = QTextEdit('')
    self.delbtn = QPushButton('Delete row')
    self.ui.gridLayout.addWidget(self.row, rowcnt, 0, 1, 1)
    self.ui.gridLayout.addWidget(self.delbtn, rowcnt, 1, 1, 1)
    w = self.ui.gridLayout.itemAtPosition(rowcnt,1).widget()
    def delf(currow):
        global rowcnt
        rowcnt = rowcnt-1
        number = self.ui.gridLayout.itemAtPosition(currow,1).widget()
        number2 = self.ui.gridLayout.itemAtPosition(currow,0).widget()
        number.deleteLater()
        number2.deleteLater()
    w.clicked.connect(lambda: delf(mrowcnt))
    rowcnt = rowcnt+1
    mrowcnt = rowcnt-1

和该用户界面

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(70, 30, 441, 121))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 1, 1, 1)
        self.textBrowser = QtWidgets.QTextEdit(self.gridLayoutWidget)
        self.textBrowser.setObjectName("textBrowser")
        self.gridLayout.addWidget(self.textBrowser, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

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

现在,我考虑收集所有textedit文本并在列表中进行排序,但是之后该怎么做-我不知道。也许是它解决问题的愚蠢方法,但我现在没有其他方法。

1 个答案:

答案 0 :(得分:0)

仅更新文本值比在布局中的小部件之间移动要容易得多。您可以调用QTextEdit.toPlainText()来获取每个文本值作为字符串。将所有字符串放在列表中,对列表进行排序,然后在布局中的每个QTextEdit上使用QTextEdit.setText()

def sbtn(self):
    text_edits = [self.ui.gridLayout.itemAt(i).widget() for i in range(self.ui.gridLayout.count()) if isinstance(self.ui.gridLayout.itemAt(i).widget(), QTextEdit)]
    text = sorted([x.toPlainText() for x in text_edits], reverse=True)
    for i in range(len(text)):
        text_edits[i].setText(text[i])