Brady
Brady

Reputation: 15

Edit tab name from child class

I am trying to have a function change the text listed on a tab, but I am having trouble figuring out how to properly reference the tab.

The below code works, but does nothing since the one line is commented out. I know the issue is with the "self" in that line (un-commenting the line causes the error when the button is clicked), but I do not know what is supposed to go there to make it work. I am guessing that my issue is with the tab being created by a different class, but I want to be able to open multiple tabs with the same widgets in each one (as this code does). Will I have to restructure the code to make this work?

from PyQt5.QtWidgets import (QMainWindow, QApplication, QAction, qApp,
    QPushButton, QWidget, QMenu, QVBoxLayout, QTabWidget, QLineEdit,
    QLabel, QHBoxLayout)
import sys

class MainWin(QMainWindow):

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

        self.initUI()

    def initUI(self):

        exitAct = QAction('&Exit', self)
        exitAct.triggered.connect(qApp.quit)

        newAct = QAction('&New Tab', self)
        newAct.triggered.connect(self.newTab)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')

        fileMenu.addAction(newAct)
        fileMenu.addAction(exitAct)

        self.layout = QVBoxLayout()

        self.tabbed = QTabWidget(self)
        self.layout.addWidget(self.tabbed)
        self.setCentralWidget(self.tabbed)

        self.show()

    def newTab(self):
        self.tab1 = tabbedFrame(self.tabbed)
        self.tabbed.addTab(self.tab1, self.tab1.tabName)


class tabbedFrame(QWidget):
    def __init__(self, parent):
        super(tabbedFrame, self).__init__(parent)
        layout = QVBoxLayout()
        self.updateButton = QPushButton("Update")
        self.updateButton.clicked.connect(self.updateTab)
        layout.addWidget(QLabel("Tab Name"))
        self.nameLineEdit = QLineEdit("New Tab")
        self.tabName = self.nameLineEdit.text()
        layout.addWidget(self.nameLineEdit)
        layout.addWidget(self.updateButton)
        self.setLayout(layout)

    def updateTab(self):
        tabindex = win.tabbed.currentIndex
        #win.tabbed.setTabText(self, tabindex, self.tabName)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWin()
    sys.exit(app.exec_())

Upvotes: 1

Views: 435

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

You want to change the text and in your attempt you are using the variable tabName, but that variable takes the initial value of the QLineEdit, and after you do not update it, then how do you want the text to change?

One solution is to create a signal that transports the new title that must be updated, and must be emited at the moment that is clicked through the clicked signal.

Then to establish the new name you must use the setTabText() method where you must pass the index of the tab and the new name. For this we can use functools.partial() in the connection:

import sys
from functools import partial
from PyQt5 import QtCore, QtWidgets


class MainWin(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        exitAct = QtWidgets.QAction('&Exit', self)
        exitAct.triggered.connect(QtWidgets.qApp.quit)
        newAct = QtWidgets.QAction('&New Tab', self)
        newAct.triggered.connect(self.newTab)
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')

        fileMenu.addAction(newAct)
        fileMenu.addAction(exitAct)
        self.tabbed = QtWidgets.QTabWidget()
        self.setCentralWidget(self.tabbed)

        self.show()

    def newTab(self):
        tab = TabbedFrame()
        ix = self.tabbed.addTab(tab, "")
        tab.nameTabChanged.connect(partial(self.tabbed.setTabText, ix))
        tab.updateTab()


class TabbedFrame(QtWidgets.QWidget):
    nameTabChanged = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super(TabbedFrame, self).__init__(parent)

        updateButton = QtWidgets.QPushButton("Update")
        updateButton.clicked.connect(self.updateTab)
        self.nameLineEdit = QtWidgets.QLineEdit("New Tab")

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(QtWidgets.QLabel("Tab Name"))
        layout.addWidget(self.nameLineEdit)
        layout.addWidget(updateButton)
        layout.addStretch()

    def updateTab(self):
        new_name = self.nameLineEdit.text()
        self.nameTabChanged.emit(new_name)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    win = MainWin()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions