简体   繁体   中英

How to add data to QTableWidget using PySide

the answers to this question is either in C++ or doesn't really answer my question. I'm learning python, I'm not professional programmer, I have learned the language but have not build live applications yet.

  • I designed user-interface using QT.
  • I imported this user interface into my main program.

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.

  • I have already imported the UI into my main program using this statement from "firstApp" import PyMainWindow

  • Do I need to create new object in my main program for QTableWidget to add data?

This is what I was trying to do but it doesn't work

Code Doesnt' Work:

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 ##

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. 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)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM