[英]How to add data to QTableWidget using PySide
the answers to this question is either in C++ or doesn't really answer my question. 这个问题的答案要么是用C ++,要么是不能回答我的问题。 I'm learning python, I'm not professional programmer, I have learned the language but have not build live applications yet.
我正在学习python,我不是专业程序员,我学过这门语言但还没有构建实时应用程序。
User-Interface [Screenshot Attached] * Trying to Achieve: * 用户界面 [附加截图] * 试图实现: *
I want to add data into QLineEdit and when I hit "Add Row" Button, it should enter the data into QTableWidget. 我想将数据添加到QLineEdit中,当我点击“添加行”按钮时,它应该将数据输入到QTableWidget中。
I have already imported the UI into my main program using this statement from "firstApp" import PyMainWindow 我已经使用“firstApp”导入PyMainWindow中的语句将UI导入到我的主程序中
Do I need to create new object in my main program for QTableWidget to add data? 我是否需要在主程序中为QTableWidget创建新对象以添加数据?
This is what I was trying to do but it doesn't work 这是我试图做的,但它不起作用
Code Doesnt' Work: 代码不'工作:
self.addData.clicked.connect(self.addDataClicked)
self.addData.clicked.connect(self.addDataClicked)
def addDataClicked(self):
username = self.userName.text()
print username ## for testing if signal is working ##
self.item.setItem(0,0,username)
## Where 0 is row and 0 is column, username is the data i want to add ## ##其中0是行,0是列,username是我要添加的数据##
the error I'm getting is item doesn't exist globally . 我得到的错误是项目全局不存在 。 I understand this but I was thinking because I have already imported the UI into my main app, python would know I'm talking about that Item in UI file.
我理解这一点,但我在想,因为我已经将UI导入我的主应用程序,python会知道我在UI文件中讨论该项目。 which is written like this.
这是这样写的。
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(5, item)
self.tableWidget.horizontalHeader().setVisible(True)
please help. 请帮忙。
The data you input are supposed to be your table headers ? 您输入的数据应该是您的表格标题? Seems rather unusual.
似乎很不寻常。
From the documentation : 从文档 :
Items are constructed outside the table before being added to the table at the required location:
在将表添加到所需位置的表之前,将在表外构建项:
QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(pow(row, column+1)));
tableWidget->setItem(row, column, newItem);
Try something like this: 尝试这样的事情:
def addDataClicked(self):
username = self.userName.text()
self.myTable.insertRow(0)
item = QtGui.QTableWidgetItem(username)
self.myTable.setItem(0, 0, item)
[EDIT] The thing is, the new item is created as a new independent object you will then add to your container. [编辑]问题是,新项目是作为新的独立对象创建的,然后您将添加到容器中。 It works this way for all item view widget in Qt (but some of them have constructors that allows you to do so at creation)
它以这种方式对Qt中的所有项目视图小部件起作用(但是其中一些具有允许您在创建时执行此操作的构造函数)
From the page linked earlier: 从前面链接的页面:
A list widget is constructed in the same way as any other widget:
列表小部件的构造方式与任何其他小部件相同:
QListWidget *listWidget = new QListWidget(this);
List items can be added directly to the list widget when they are constructed:
列表项可以在构造时直接添加到列表小部件:
new QListWidgetItem(tr("Sycamore"), listWidget);
new QListWidgetItem(tr("Chestnut"), listWidget);
new QListWidgetItem(tr("Mahogany"), listWidget);
They can also be constructed without a parent list widget and added to a list at some later time:
它们也可以在没有父列表小部件的情况下构建,并在以后添加到列表中:
QListWidgetItem *newItem = new QListWidgetItem;
newItem->setText(itemText);
listWidget->insertItem(row, newItem);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.