Search code examples
pythonpyqtpyqt5qtablewidgetqtablewidgetitem

How can I print values from "QTableWidget" to another "QTableWidget"?


I'm trying to code a football player selection program. The program consists of 2 windows. The program measures the players according to their criteria and shows who is the best choice.

Coming to my problem, I cannot print the names of the players entered in the first window (on the main page) where the names of the players are written in the second window (in the results window).

Likewise, the same goes for the results, I get the output as a result in the console, but I cannot print that output in my second window (result window) where the results will be written.

I used QTableWidget in both windows. There will be no action in the results window, just the results. If you have any other widget suggestions to show them, I'm also open to them.

Photo of the interface

When you click on the "Hesapla" (Calculate) button here, calculations are made in the background and the second window opens.

I want to print the "İsim" (Name) column in the first window to the "Futbolcular" (Football players) column in the second window and my results in the second column.

That my main window:

class AnaPencere(QMainWindow,QTableWidget,QTableWidgetItem):

def __init__(self):
    super().__init__()

    self.ui = Ui_AnaSayfa()
    self.ui.setupUi(self)

    self.futbolcular = [[],[]]

    self.ui.pushButton_futbolcuekle.clicked.connect(self.futbolcuekle)
    self.ui.pushButton_futbolcucikar.clicked.connect(self.futbolcucikar)
    self.ui.pushButton_hesapla.clicked.connect(self.hesapla)
    self.ui.pushButton_hesapla.clicked.connect(self.ac_sonuc_penceresi)

    self.topsis_sonuc = SonucPencere()


def futbolcuekle(self):

    satirsayimi = self.ui.tableWidget.rowCount()
    self.ui.tableWidget.insertRow(satirsayimi)
    self.topsis_sonuc.ui_snc.tableWidget_sonuc.insertRow(satirsayimi)
    self.futbolcular.append([])
    self.ui.statusBar.showMessage(" Futbolcu sayısı artırıldı.",1500)


def futbolcucikar(self):

    if self.ui.tableWidget.rowCount() > 2:
        self.ui.tableWidget.removeRow(self.ui.tableWidget.rowCount() - 1)

    if self.topsis_sonuc.ui_snc.tableWidget_sonuc.rowCount() > 2:
        self.topsis_sonuc.ui_snc.tableWidget_sonuc.removeRow(self.topsis_sonuc.ui_snc.tableWidget_sonuc.rowCount() - 1)

    if len(self.futbolcular) > 2:
        self.futbolcular.pop()
        self.ui.statusBar.showMessage(" Futbolcu sayısı azaltıldı.",1500)
    else:
        QMessageBox.information(self, "Bilgi Mesajı", "Futbolcu sayısı en az 2 olmalıdır.")


def hesapla(self):

    weights = np.array([[0.25, 0.55, 0.40, 0.30, 0.20, 0.10, 0.05]])
    criterion_type = ['max', 'max', 'max', 'max', 'max', 'max', 'max']

    satirlistesi = [i for i in (range(self.ui.tableWidget.rowCount()))]

    for i in range(self.ui.tableWidget.rowCount()):
        for j in range(1,self.ui.tableWidget.columnCount()):

            for k in satirlistesi:
                if i == k:
                    a = self.ui.tableWidget.item(i, j)
                    veri = int(a.text())
                    self.futbolcular[i].append(veri)

    #print(self.futbolcular)

    dataset = np.array(self.futbolcular)

    yakinlik_katsayilari = topsis_method(dataset, weights, criterion_type, graph=False)

    for i in range(0, yakinlik_katsayilari.shape[0]):
        print('futbolcu' + str(i + 1) + ' = ', round(yakinlik_katsayilari[i], 4))

    for i in range(self.ui.tableWidget.rowCount()):
        topcu = self.ui.tableWidget.item(i, 0)
        topcu_isim = (topcu.text())

        self.topsis_sonuc.ui_snc.tableWidget_sonuc.setItem(i, 0, topcu)
        sonuc_topcu = self.topsis_sonuc.ui_snc.tableWidget_sonuc.item(i, 0)
        sonuc_topcu.setText(topcu_isim)

        sonuc_topcu_isim = (sonuc_topcu.text())
        print(sonuc_topcu_isim)

    for i in range(self.ui.tableWidget.rowCount()):
        yks = self.topsis_sonuc.ui_snc.tableWidget_sonuc.item(i, 1)
        yks.setText(yakinlik_katsayilari)

def ac_sonuc_penceresi(self):

    self.topsis_sonuc.show()

Here I used the last two for loops to print, but it does not print inside the tables in my second window, it returns None…

It says:

    sonuc_topcu.setText(topcu_isim)
AttributeError: 'NoneType' object has no attribute 'setText'

Solution

  • Selam, It's very basic actually, first, get your data from the datatable with a loop. self.ui.tableWidget.item(row, column).text(). Basically :

    for r in range(self.ui.tableWidget.rowCount()):
        for c in range(self.ui.tableWidget.columnCount()):
            data.loc[r, c] = self.ui.tableWidget.item(r, c).text()
    

    here i add to pandas dataframe but you could add dict, list or whatever you want. Second, get data and put in another datatable

    for c in range(len(data.columns)):
        for r, row in data.iterrows():
            self.ui.tableWidget.setItem(c, r, QtWidgets.QTableWidgetItem(str(data.iloc[r, c])))