Python PyQt - QTableWidget、JSON 和 emitSignal 导致空白单元格 [英] Python PyQt - QTableWidget, JSON, and emitSignal causing blank cells

查看:34
本文介绍了Python PyQt - QTableWidget、JSON 和 emitSignal 导致空白单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 PyQt 用于一个简单的应用程序,该应用程序从带有 JSON 格式字符串的日志文件中读取,并在表中很好地输出它们.

I am using PyQt for a simple application that reads from a log file with JSON formatted strings, and outputs them nicely in a table.

一切都按预期工作,除非我尝试从加载"函数发出信号.该信号由主窗口在一个插槽中接收,该插槽旨在使用新信息对表格进行排序.

Everything is working as expected except when I try to emit a signal from a 'load' function. This signal is picked up by the main window, in a slot designed to resort the table with new information.

在没有发出信号的情况下,表格完全正确地填充:

Without the signal emitted, the table populates fully and properly:

通过取消注释 self.emit 以便发出信号,表格最终不完整:

By uncommenting the self.emit so that the signal IS emitted, the table ends up being incomplete:

正如您在第一张图片中看到的,表格未排序,但所有字段都已填充.在第二张图中,表格已排序,但有些字段为空!

As you can see in the first image, the table is NOT sorted, but all fields are populated. In the second image, the table is sorted, but some fields are blank!

填充表格并发送信号的代码:

The code that populates the table and sends the signal:

#openLog function does stuff, then populates the table as follows

self.ui.tableWidget.setRowCount(len(entries))
self.ui.tableWidget.verticalHeader().setVisible(False)

for i, row in enumerate(entries):
    for j, col in enumerate(row):
        item = QtGui.QTableWidgetItem(col)
        self.ui.tableWidget.setItem(i, j, item)

#When this is uncommented, the table ends up having a lot of blank cells.
#self.emit(QtCore.SIGNAL("updateSignal"))

接收信号并执行的代码:

The code for receiving the signal, and acting:

#main window class
    #__init__
        self.ui.tableWidget.connect(self,QtCore.SIGNAL("updateSignal"),self.updateTable)

    def updateTable(self):
        self.ui.tableWidget.sortItems(0,QtCore.Qt.DescendingOrder)

程序流程称为:program_init->register_signal.用户打开日志的操作 -> 填充表/发出信号的 openLog 函数 -> 接收信号/度假村表

The program flow is called as : program_init->register_signal. User action to open log ->openLog function that populates table/emit signal->signal received/ resort table

对于这种方法,我使用了信号和槽,好像我没有使用一样,QT/Python 会抛出一堆关于从函数重绘 GUI/Pixmap 不安全的警告.

For this method, I am using signals and slots, as if I do not, QT/Python throws a bunch of warnings about it not being safe to redraw the GUI/Pixmap from the function.

问题:如何在我想要的列上进行 QTableWidget 排序,同时确保表格已完全填充?

Question: How can I make the QTableWidget sort on the column I desire, while also ensuring the table is fully populated?

推荐答案

我认为解决方案是通过调用 QTableWidget.setSortingEnabled(False),然后恢复排序.

I think solution is to disable sorting while populating table by calling QTableWidget.setSortingEnabled(False), and then restore sorting.

示例代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QWidget):
    updateSignal = QtCore.pyqtSignal()
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.table_widget = QtGui.QTableWidget()
        self.button = QtGui.QPushButton('Populate')
        self.button.clicked.connect(self.populate)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.table_widget)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.updateSignal.connect(self.update_table)
        self.populate()

    def populate(self):
        nrows, ncols = 5, 2
        self.table_widget.setSortingEnabled(False)
        self.table_widget.setRowCount(nrows)
        self.table_widget.setColumnCount(ncols)
        for i in range(nrows):
            for j in range(ncols):
                item = QtGui.QTableWidgetItem('%s%s' % (i, j))
                self.table_widget.setItem(i, j, item)
        self.updateSignal.emit()
        self.table_widget.setSortingEnabled(True)

    def update_table(self):
        self.table_widget.sortItems(0,QtCore.Qt.DescendingOrder)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    wnd = MainWindow()
    wnd.resize(640, 480)
    wnd.show()
    sys.exit(app.exec_())

这篇关于Python PyQt - QTableWidget、JSON 和 emitSignal 导致空白单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆