Search code examples
pythonpyqtpyqt5qtabbar

how to pair creating new tabs with editing


My problem start when i try some options to pair this two scripts but it only ends with TypeError: init() missing 1 required positional argument: 'parent' Even my friend try to help but it end with similar errors becouse he have skills in C++

import sys
from PyQt5.QtWidgets import QTabWidget, QWidget, QToolButton, QTabBar, QApplication


class Project_Tabs(QTabWidget):

    def __init__(self):
        QTabWidget.__init__(self)
        self.tabCloseRequested.connect(lambda index: tabs.removeTab(index))
        self.setTabsClosable(True)
        self._build_tabs()


    def _build_tabs(self):

        self.insertTab(0, QWidget(), "Project 0" )

        # create the "new tab" tab with button
        self.insertTab(1, QWidget(),'')
        nb = self.new_btn = QToolButton()
        nb.setText('+') # you could set an icon instead of text
        nb.setAutoRaise(True)
        nb.clicked.connect(self.new_tab)
        self.tabBar().setTabButton(1, QTabBar.RightSide, nb)


    def new_tab(self):
        index = self.count() - 1
        self.insertTab(index, QWidget(), "Project %d" % index)
        self.setCurrentIndex(index)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    tabs = Project_Tabs()
    tabs.show()

    app.exec_()

and

from PyQt5.QtCore import Qt, QEvent
from PyQt5.QtWidgets import QTabBar, QTabWidget, QApplication, QLineEdit, QWidget

class EditableTabBar(QTabBar):
    def __init__(self, parent):
        QTabBar.__init__(self, parent)
        self._editor = QLineEdit(self)
        self._editor.setWindowFlags(Qt.Popup)
        self._editor.setFocusProxy(self)
        self._editor.editingFinished.connect(self.handleEditingFinished)
        self._editor.installEventFilter(self)

    def eventFilter(self, widget, event):
        if ((event.type() == QEvent.MouseButtonPress and not self._editor.geometry().contains(event.globalPos())) or (event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape)):
            self._editor.hide()
            return True
        return QTabBar.eventFilter(self, widget, event)

    def mouseDoubleClickEvent(self, event):
        index = self.tabAt(event.pos())
        if index >= 0:
            self.editTab(index)

    def editTab(self, index):
        rect = self.tabRect(index)
        self._editor.setFixedSize(rect.size())
        self._editor.move(self.parent().mapToGlobal(rect.topLeft()))
        self._editor.setText(self.tabText(index))
        if not self._editor.isVisible():
            self._editor.show()

    def handleEditingFinished(self):
        index = self.currentIndex()
        if index >= 0:
            self._editor.hide()
            self.setTabText(index, self._editor.text())

class Window(QTabWidget):
    def __init__(self):
        QTabWidget.__init__(self)
        self.setTabBar(EditableTabBar(self))
        self.addTab(QWidget(self), 'Tab One')
        self.addTab(QWidget(self), 'Tab Two')

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

All i try end with error: TypeError: init() missing 1 required positional argument: 'parent'


Solution

  • As not to indicate the complete error message or the code that produces it, it is impossible to objectively indicate the cause of the error but I presume that you think that joining functionality is only joining code but unfortunately it is not true.

    In the following code I show the union of both functionalities:

    from PyQt5.QtCore import Qt, QEvent
    from PyQt5.QtWidgets import (
        QTabBar,
        QTabWidget,
        QApplication,
        QLineEdit,
        QWidget,
        QToolButton,
    )
    
    
    class EditableTabBar(QTabBar):
        def __init__(self, parent):
            QTabBar.__init__(self, parent)
            self._editor = QLineEdit(self)
            self._editor.setWindowFlags(Qt.Popup)
            self._editor.setFocusProxy(self)
            self._editor.editingFinished.connect(self.handleEditingFinished)
            self._editor.installEventFilter(self)
    
        def eventFilter(self, widget, event):
            if (
                event.type() == QEvent.MouseButtonPress
                and not self._editor.geometry().contains(event.globalPos())
            ) or (event.type() == QEvent.KeyPress and event.key() == Qt.Key_Escape):
                self._editor.hide()
                return True
            return QTabBar.eventFilter(self, widget, event)
    
        def mouseDoubleClickEvent(self, event):
            index = self.tabAt(event.pos())
            if index >= 0:
                self.editTab(index)
    
        def editTab(self, index):
            rect = self.tabRect(index)
            self._editor.setFixedSize(rect.size())
            self._editor.move(self.parent().mapToGlobal(rect.topLeft()))
            self._editor.setText(self.tabText(index))
            if not self._editor.isVisible():
                self._editor.show()
    
        def handleEditingFinished(self):
            index = self.currentIndex()
            if index >= 0:
                self._editor.hide()
                self.setTabText(index, self._editor.text())
    
    
    class Window(QTabWidget):
        def __init__(self):
            QTabWidget.__init__(self)
            self.setTabBar(EditableTabBar(self))
            self.setTabsClosable(True)
            self.tabCloseRequested.connect(self.removeTab)
    
            self.addTab(QWidget(self), "Tab One")
            self.addTab(QWidget(self), "Tab Two")
    
            count = self.count()
    
            nb = QToolButton(text="+", autoRaise=True)
            nb.clicked.connect(self.new_tab)
            self.insertTab(count, QWidget(), "")
            self.tabBar().setTabButton(count, QTabBar.RightSide, nb)
    
        def new_tab(self):
            index = self.count() - 1
            self.insertTab(index, QWidget(), "Tab %d" % index)
            self.setCurrentIndex(index)
    
    
    if __name__ == "__main__":
    
        import sys
    
        app = QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())