PyQt4-如何从QListWidgetItem中读取QLineEdit文本

时间:2019-04-01 17:21:04

标签: python-3.x pyqt4

我创建了一个QListWidget ... QListWidgetItem由QHBoxLayout中对齐的QPushButton和QLineEdit组成。

创建了另一个按钮,该按钮具有在QListWidgetItem上循环并读取每个QLineEdit条目中的文本并进行打印的功能。

打印结果什么都没有显示,我尝试了QListWidget方法的许多不同组合,但没有希望!

enter image description here


import sys
from PyQt4 import QtGui, QtCore

def Add_OtherItem():
    ItemOther = CustomItem()
    ItemOther.SetupItem(OthersCommandsWidget)

def ReadText_fn():
    for index in range(0,OthersCommandsWidget.count()):
        TargetItem = (OthersCommandsWidget.item(index)).text()
        print(TargetItem)

app = QtGui.QApplication(sys.argv)

class CustomItem(object):

    def SetupItem(self, OthersCommandList):

        self.Item = QtGui.QListWidgetItem()
        self.Item.setStatusTip("TItem")

        self.MainWidget = QtGui.QWidget()

        self.CommandLine = QtGui.QLineEdit("")

        self.DeleteButton = QtGui.QPushButton()
        self.DeleteButton.setFixedSize(22, 22)

        self.ItemLayoutBox = QtGui.QHBoxLayout()

        self.ItemLayoutBox.addWidget(self.CommandLine)
        self.ItemLayoutBox.addWidget(self.DeleteButton)

        self.MainWidget.setLayout(self.ItemLayoutBox)

        self.Item.setSizeHint(self.MainWidget.sizeHint())

        OthersCommandList.addItem(self.Item)
        OthersCommandList.setItemWidget(self.Item, self.MainWidget)

AppWindow = QtGui.QMainWindow()
AppWindow.setWindowTitle("PoC ListWidget")
AppWindow.setFixedSize(550, 550)

TabWindow = QtGui.QTabWidget(AppWindow)
TabWindow.setGeometry(8, 30, 535, 505)

WorkTAB = QtGui.QWidget()
TabWindow.addTab(WorkTAB, 'Tab.01')

OthersCommandsWidget = QtGui.QListWidget(WorkTAB)
OthersCommandsWidget.setGeometry(QtCore.QRect(8, 40, 515, 430))

AddButton = QtGui.QPushButton(WorkTAB)
AddButton.setText("Add Item")
AddButton.setGeometry(QtCore.QRect(8, 8, 0, 0))
AddButton.setFixedSize(70, 22)

AddButton.clicked.connect(Add_OtherItem)

ReadButton = QtGui.QPushButton(WorkTAB)
ReadButton.setText("Read Text")
ReadButton.setGeometry(QtCore.QRect(100, 8, 0, 0))
ReadButton.setFixedSize(70, 22)

ReadButton.clicked.connect(ReadText_fn)

AppWindow.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

您正在尝试访问QLineEdit()的文本字段。为此,必须调用其text()属性。但是首先,您必须获取QLineEdit()对象。

OthersCommandsWidget类型为QListWidget(),其类型为QListWidgetItem(),该QWidget()已连接到QWidget()。您可以使用itemWidget()来抓取QWidget()对象。 QHBoxLayout()具有三个嵌入式对象:QLineEdit()QPushButton()children(),可以根据添加的顺序使用TargetItem = (OthersCommandsWidget.item(index)).text() 进行访问。

更改

TargetItem = OthersCommandsWidget.itemWidget(OthersCommandsWidget.item(index)).children()[1].text()

收件人:

SELECT c.id, SUM(a.view_count), dt
FROM project_content c
JOIN project_content_analytic a ON a.content_id = c.id
RIGHT JOIN (SELECT c.id, DATE_FORMAT(a.created_at, '%d-%m-%Y') dt, MAX(a.created_at) max_date
            FROM project_content c
            JOIN project_content_analytic a ON a.content_id = c.id
            GROUP BY dt, c.id) x 
ON x.max_date = a.created_at and x.id = c.id
GROUP BY dt  
ORDER BY `x`.`dt` ASC