pyqt5 QTableWidget 不会显示来自 PyEve 数据集的 id 键值

Posted

技术标签:

【中文标题】pyqt5 QTableWidget 不会显示来自 PyEve 数据集的 id 键值【英文标题】:pyqt5 QTableWidget won't display id key value from PyEve dataset 【发布时间】:2017-11-17 07:26:18 【问题描述】:

我正在尝试构建一个 pyqt5 小部件来显示来自 PyEve 的数据集,但我无法将 id 键值显示到表行标题中。这是生成 QTableWidget 的部分代码

http = "http://127.0.0.1:5000/people"
con = requests.get(http)

self.tview = QTableWidget()
self.tview.setRowCount(len(con.json()['_items']))
self.tview.setColumnCount(2)

self.tview.setHorizontalHeaderItem(0, QTableWidgetItem("Firstname"))
self.tview.setHorizontalHeaderItem(1, QTableWidgetItem("Lastname"))

row = 0
for item in con.json()['_items']:
    self.tview.setVerticalHeaderItem(row, QTableWidgetItem(item["id"]))
    self.tview.setItem(row, 0, QTableWidgetItem(item['firstname']))
    self.tview.setItem(row, 1, QTableWidgetItem(item['lastname']))
    row += 1

结果: QTableWidget

这是来自 shell 中 API 调用的数据集

    $ http localhost:5000/people

    
    "_items": [
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "34418931281956ee9f0d73f5015a325276dce0a8",
            "_links": 
                "self": 
                    "href": "people/1",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "George",
            "fullname": "George Washington",
            "id": 1,
            "lastname": "Washington"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "8efc3ef35256de6e9bf530e54188ba5c3526cf0b",
            "_links": 
                "self": 
                    "href": "people/2",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "John",
            "fullname": "John Adams",
            "id": 2,
            "lastname": "Adams"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "b1e81fb3519dda0d723043203c06c2df235c9450",
            "_links": 
                "self": 
                    "href": "people/3",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Thomas",
            "fullname": "Thomas Jefferson",
            "id": 3,
            "lastname": "Jefferson"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "5d3bc841cefdb56710445b2cf9d89c3a0c899551",
            "_links": 
                "self": 
                    "href": "people/4",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "George",
            "fullname": "George Clinton",
            "id": 4,
            "lastname": "Clinton"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "a83d11293f00374f91f66ae9da5373dc6c924bca",
            "_links": 
                "self": 
                    "href": "people/5",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "James",
            "fullname": "James Madison",
            "id": 5,
            "lastname": "Madison"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "e6abc69c3f2cb05676b0f2e6d76bde51b3ee467c",
            "_links": 
                "self": 
                    "href": "people/6",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Elbridge",
            "fullname": "Elbridge Gerry",
            "id": 6,
            "lastname": "Gerry"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "5621734381fe51fc4bb6d05bdc94ffe2cfe91c75",
            "_links": 
                "self": 
                    "href": "people/7",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "James",
            "fullname": "James Monroe",
            "id": 7,
            "lastname": "Monroe"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "a03e9261c3a06526893ba074f62b939d9f7798ed",
            "_links": 
                "self": 
                    "href": "people/8",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "John",
            "fullname": "John Adams",
            "id": 8,
            "lastname": "Adams"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "1b7c8e7674a75e876547567839f0496f4e2f893e",
            "_links": 
                "self": 
                    "href": "people/9",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Andrew",
            "fullname": "Andrew Jackson",
            "id": 9,
            "lastname": "Jackson"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "975dd0527d63b8bb10fe63c6d648df8eaafc1a4d",
            "_links": 
                "self": 
                    "href": "people/10",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Martin",
            "fullname": "Martin Van Buren",
            "id": 10,
            "lastname": "Van Buren"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "d2aed37174724a07dd4f3262b703490319c9aa9a",
            "_links": 
                "self": 
                    "href": "people/11",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "William",
            "fullname": "William Harrison",
            "id": 11,
            "lastname": "Harrison"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "60938a8ea70dc4be7082dc12510c19bd2d802623",
            "_links": 
                "self": 
                    "href": "people/12",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "John",
            "fullname": "John Tyler",
            "id": 12,
            "lastname": "Tyler"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "f158fb234f0be3da9701779bcc953311bcaf9e89",
            "_links": 
                "self": 
                    "href": "people/13",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "James",
            "fullname": "James Polk",
            "id": 13,
            "lastname": "Polk"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "32cd09027db237a97c1d26a365403385e7eb9e40",
            "_links": 
                "self": 
                    "href": "people/14",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Zachary",
            "fullname": "Zachary Taylor",
            "id": 14,
            "lastname": "Taylor"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "9c211e916acb0327c2cc02f71a894e1f445d21ba",
            "_links": 
                "self": 
                    "href": "people/15",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Millard",
            "fullname": "Millard Fillmore",
            "id": 15,
            "lastname": "Fillmore"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "f8d4dc8f5d8ffed19cdd5304c66a5f930e085fcb",
            "_links": 
                "self": 
                    "href": "people/16",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Franklin",
            "fullname": "Franklin Pierce",
            "id": 16,
            "lastname": "Pierce"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "1b64a097904bb043e9d52f85c0620d5743fe41f7",
            "_links": 
                "self": 
                    "href": "people/17",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "James",
            "fullname": "James Buchanan",
            "id": 17,
            "lastname": "Buchanan"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "dbc84c7bffdcaa6268f64556db373f96a308025c",
            "_links": 
                "self": 
                    "href": "people/18",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Abraham",
            "fullname": "Abraham Lincoln",
            "id": 18,
            "lastname": "Lincoln"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "c9e29f8cc5b81bc406336780fbdd211490af33c6",
            "_links": 
                "self": 
                    "href": "people/19",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Andrew",
            "fullname": "Andrew Johnson",
            "id": 19,
            "lastname": "Johnson"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "b3ebe09856c4e5e6e04a5e5fb88c087f5cf93165",
            "_links": 
                "self": 
                    "href": "people/20",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Ulysses",
            "fullname": "Ulysses Grant",
            "id": 20,
            "lastname": "Grant"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "e874ccb8f1ff91efb4f25d0d86ac0d1c15ba9260",
            "_links": 
                "self": 
                    "href": "people/21",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Henry",
            "fullname": "Henry Wilson",
            "id": 21,
            "lastname": "Wilson"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "a62dd991fd36fdfc427c6823ecd1baf9b9a2a55f",
            "_links": 
                "self": 
                    "href": "people/22",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Rutherford",
            "fullname": "Rutherford Hayes",
            "id": 22,
            "lastname": "Hayes"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "21071baad27a9fed4ed01be7b32b7962fabd9414",
            "_links": 
                "self": 
                    "href": "people/23",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "James",
            "fullname": "James Garfield",
            "id": 23,
            "lastname": "Garfield"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "cdbd9450d58cd7258ce977cbf89a13efc3bd05df",
            "_links": 
                "self": 
                    "href": "people/24",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Chester",
            "fullname": "Chester Arthur",
            "id": 24,
            "lastname": "Arthur"
        ,
        
            "_created": "Tue, 14 Nov 2017 13:33:50 GMT",
            "_etag": "f6d202e5f0ef01ccc1bc73a215e93378935bac0b",
            "_links": 
                "self": 
                    "href": "people/25",
                    "title": "person"
                
            ,
            "_updated": "Tue, 14 Nov 2017 13:33:50 GMT",
            "firstname": "Grover",
            "fullname": "Grover Cleveland",
            "id": 25,
            "lastname": "Cleveland"
        
    ],
    "_links": 
        "last": 
            "href": "people?page=3",
            "title": "last page"
        ,
        "next": 
            "href": "people?page=2",
            "title": "next page"
        ,
        "parent": 
            "href": "/",
            "title": "home"
        ,
        "self": 
            "href": "people",
            "title": "people"
        
    ,
    "_meta": 
        "max_results": 25,
        "page": 1,
        "total": 55
    

这是我从 python 控制台打印“id”值的输出

import requests 
r = requests.get('http://127.0.0.1:5000/people') 
for i in r.json()["_items"]: print(i["id"])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

我也尝试在verticalHeader之外显示ID号,但无济于事

任何见解都会非常有帮助,在此先感谢

【问题讨论】:

更改为self.tview.setVerticalHeaderItem(row, QTableWidgetItem(str(item["id"]))) 【参考方案1】:

正如@eyllanesc 所说.. 将其更改为

self.tview.setVerticalHeaderItem(row, QTableWidgetItem(str(item["id"])))

成功了。谢谢

【讨论】:

以上是关于pyqt5 QTableWidget 不会显示来自 PyEve 数据集的 id 键值的主要内容,如果未能解决你的问题,请参考以下文章

在 QTableWidget PyQt5 中突出显示单元格

PyQt5 QTableWidget 仅显示第一行和第一列

PyQt5 - 从 sqlite3 复制到 QTableWidget 的 SQL 行不显示

QTableWidget 上的 PYQT5 setCellWidget() 会减慢 UI

PyQt5 QTableWidget:右键选择列,并在上下文菜单中显示删除条目

得到选择行pyqt5 qtablewidget