在QlineEdit中查找表格

时间:2018-03-17 10:01:04

标签: python-3.x qt pyqt5

我根据游标所在的行创建了一个向现有表添加行的函数。这很好用。但是,如果光标不在表上,它应该找到该表并在最后添加新行(我想确保文档上始终只有1个表)。如何找到表格以便在末尾添加一行?

这是我关于该功能的代码(我评论了它失败的地方):

from PyQt5.QtGui import QTextTableFormat, QTextLength, QTextCursor, QTextFrameFormat

self.button_addrow.clicked.connect(lambda: self.add_new_row_to_table("Hello SO"))

def create_default_table(self, cursor, name):
    """Create the table if it doesn't exists."""
    # Set the format of the table
    fmt = QTextTableFormat()
    fmt.setCellPadding(1)
    fmt.setCellSpacing(0)
    fmt.setBorderStyle(QTextFrameFormat.BorderStyle_Solid)
    fmt.setBorder(1)
    constraints = [QTextLength(QTextLength.FixedLength, 100),
                   QTextLength(QTextLength.FixedLength, 430),
                   QTextLength(QTextLength.FixedLength, 40)]
    fmt.setColumnWidthConstraints(constraints)
    # Insert the new table
    cursor.insertTable(1, 3, fmt)
    cursor.insertText('{0}:'.format(name))
    cursor.movePosition(QTextCursor.NextCell)
    cursor.insertText("")
    cursor.movePosition(QTextCursor.NextCell)
    cursor.insertText('{0}'.format(self.label_PlayerCurrentTime.text()[:5]))

def add_new_row_to_table(self, value):
    if value == "":
        self.change_statusbar("error", "The name cannot be empty.")
    else:
        # Grab the position
        cursor = self.lineEdit.textCursor()
        try:
            if not "<table" in self.line_Edit.toHtml():
                self.create_default_table(cursor, value)
            else:
                if cursor.currentTable() is None:
                    cursor.movePosition(QTextCursor.Start)
                    while cursor.currentTable() is None:
                        cursor.movePosition(1)
                        # It fails HERE. I thought I could Iterate the cursor position until it finds it, but nope. I tried different approachs but I don't know how to do it. Suggestions are appreciated.
                    print("table found")
                    # Here I would have to find a efficient way to add the row without repeating the code on the else...
                else:
                    table = cursor.currentTable()
                    cell = table.cellAt(cursor)
                    # Insert a new row under the cursor position
                    table.insertRows(cell.row() + 1, 1)
                    cursor.movePosition(QTextCursor.NextRow)
                    cursor.insertText("{0}:".format(value))
                    cursor.movePosition(QTextCursor.NextCell)
                    cursor.insertText("")
                    cursor.movePosition(QTextCursor.NextCell)
                    cursor.insertText("{0}".format(self.label_PlayerCurrentTime.text()[:5]))
        except Exception as e:
            print(e)

这就是它的外观。

enter image description here

更新:这可以将光标放在表格的最后一个单元格上,但我不知道我对双循环的看法......

if cursor.currentTable() is None:
    # Set cursor at start
    cursor.movePosition(QTextCursor.Start)
    # Find table
    while cursor.currentTable() is None:
        cursor.movePosition(QTextCursor.NextBlock)
    # Move 1 block after table
    while cursor.currentTable() is not None:
        cursor.movePosition(QTextCursor.NextBlock)
    # Move 1 block back, so we are finally at last cell?
    cursor.movePosition(QTextCursor.PreviousBlock)
    table = cursor.currentTable()
    cell = table.cellAt(cursor)
else:
    table = cursor.currentTable()
    cell = table.cellAt(cursor)

0 个答案:

没有答案