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'
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_())
Related
I am writing a slot method for the signal of scrolling down a scrollbar in QPlainTextEdit.
I only found this signalQPlainTextEdit.verticalScrollBar().valueChanged.
I tested this signal and it returned the position number when scrolls to a new position.
My purpose is that when the scrollbar move down and trigger the slot method. But in that signal when move up it also triggeres the slot.
I read the document but I couldn't find other signals.
A possible solution is to save the previous position and compare with the new position using sliderPosition property:
from PyQt5.QtWidgets import QApplication, QPlainTextEdit
class PlainTextEdit(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.last_position = self.verticalScrollBar().sliderPosition()
self.verticalScrollBar().sliderMoved.connect(self.handle_value_changed)
def handle_value_changed(self, position):
if position > self.last_position:
print("down")
else:
print("up")
self.last_position = position
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = PlainTextEdit()
w.show()
sys.exit(app.exec_())
Another possible option is to implement a use of the mousePressEvent and mouseMoveEvent events of the QScrollBar:
from PyQt5.QtCore import QPoint, Qt
from PyQt5.QtWidgets import QApplication, QPlainTextEdit, QScrollBar
class ScrollBar(QScrollBar):
last_pos = QPoint()
def mousePressEvent(self, event):
self.last_pos = event.pos()
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
if event.pos().y() > self.last_pos.y():
print("down")
else:
print("up")
self.last_pos = event.pos()
class PlainTextEdit(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.vertical_scrollbar = ScrollBar(Qt.Vertical)
self.setVerticalScrollBar(self.vertical_scrollbar)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = PlainTextEdit()
w.show()
sys.exit(app.exec_())
OR:
from PyQt5.QtCore import QEvent, QPoint
from PyQt5.QtWidgets import QApplication, QPlainTextEdit
class PlainTextEdit(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.last_pos = QPoint()
self.verticalScrollBar().installEventFilter(self)
def eventFilter(self, obj, event):
if obj is self.verticalScrollBar():
if event.type() == QEvent.MouseButtonPress:
self.last_pos = event.pos()
elif event.type() == QEvent.MouseMove:
if event.pos().y() > self.last_pos.y():
print("down")
else:
print("up")
self.last_pos = event.pos()
return super().eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = PlainTextEdit()
w.show()
sys.exit(app.exec_())
My aim to deactivate the checkbox, if my textbox is empty. So I try with the following code if I clear my entire textbox by the way of "select all characters" using Ctrl+A and delete it. But in my code, the first part only worked, that is Ctrl+A, If I press the Delete key that is the second part of my code is not worked, How to rectify?
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_A :
print("I am inside of Cntrl+A")
if event.key() == Qt.Key_Delete:
print("i Am inside of key_delete")
self.checkbox.setChecked(False)
self.checkstatus = 0
return True
Minimal reproducible Code
import os
from PyQt5.QtWidgets import QCheckBox,QLineEdit,QWidget,QApplication,QVBoxLayout
from PyQt5.QtCore import QEvent,Qt
class CtrlaDel(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Key Press Event")
self.le = QLineEdit()
self.cb = QCheckBox()
self.cb.setChecked(True)
self.vbox = QVBoxLayout(self)
self.vbox.addWidget(self.le)
self.vbox.addWidget(self.cb)
self.le.installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QEvent.KeyPress and source is self.le:
print("ddddsdsdsdsdsdsds")
if event.key() == Qt.Key_Backspace or event.key() == Qt.Key_Delete and source is self.le:
print("ddd")
if len(self.le.text()) <= 1:
self.cb.setChecked(False)
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_A:
print("I am inside of Ctrl+A")
if event.key() == Qt.Key_Delete:
print("I am Inside of Delete")
self.cb.setChecked(False)
self.checkstatus = 0
return True
return super(CtrlaDel, self).eventFilter(source, event)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mainwindow = CtrlaDel()
mainwindow.show()
sys.exit(app.exec_())
I do not see the need to use an eventFilter, the solution is that every time the text is changed verify that if it is an empty text then set the checkbox to false and for this the textChanged signal can be used
from PyQt5.QtWidgets import QCheckBox, QLineEdit, QWidget, QApplication, QVBoxLayout
class CtrlaDel(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Key Press Event")
self.le = QLineEdit()
self.cb = QCheckBox()
self.cb.setChecked(True)
vbox = QVBoxLayout(self)
vbox.addWidget(self.le)
vbox.addWidget(self.cb)
self.le.textChanged.connect(self.handle_text_changed)
self.handle_text_changed()
def handle_text_changed(self):
if not self.le.text():
self.cb.setChecked(False)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mainwindow = CtrlaDel()
mainwindow.show()
sys.exit(app.exec_())
I have this snippet that simulate the closing of a window calling a custom QDialog:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QVBoxLayout, QHBoxLayout, QLabel
from PyQt5.QtCore import Qt
class ExitDialog(QDialog):
"""TODO"""
def __init__(self):
super().__init__()
self.buttonSi = QPushButton("Yes")
self.buttonSi.clicked.connect(self.si_clicked)
self.buttonNo = QPushButton("No")
self.buttonNo.clicked.connect(self.no_clicked)
self.buttonNonUscire = QPushButton("Do not exit")
self.buttonNonUscire.clicked.connect(self.non_uscire_clicked)
self.text = QLabel("Do you want to save changes before exit?")
self.text.setAlignment(Qt.AlignCenter)
hbox1 = QHBoxLayout()
hbox1.addWidget(self.text)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.buttonSi)
hbox2.addWidget(self.buttonNo)
hbox2.addWidget(self.buttonNonUscire)
self.layout = QVBoxLayout()
self.layout.addLayout(hbox1)
self.layout.addLayout(hbox2)
self.setLayout(self.layout)
self.value_choosed = None
def keyPressEvent(self, event):
if event.key == Qt.Key_Escape:
event.ignore()
def get_choosed_value(self):
return self.value_choosed
def si_clicked(self):
self.value_choosed = 0
self.close()
def no_clicked(self):
self.value_choosed = 1
self.close()
def non_uscire_clicked(self):
self.value_choosed = 2
self.close()
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(400,300,400,200)
vbox = QVBoxLayout()
btn = QPushButton("Exit")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
def btn_clicked(self):
self.close()
def closeEvent(self, event):
dialog = ExitDialog()
dialog.exec_()
choice = dialog.get_choosed_value()
if choice == 0:
event.accept()
elif choice == 1:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
a = QApplication(["TODO"])
w = Window()
w.show()
sys.exit(a.exec_())
I notice that when I use the 'x' button of the main window or the key combination ALT+F4 for activate the ExitDialog, it's position is on different screen coordinates with respect of using the btn Exit.I'm on Ubuntu 18.04.5, window manager: GNOME Shell.
How is this possible?
Well I actually solved by adding a QTimer at the end of __init__.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QVBoxLayout, QHBoxLayout, QLabel, QDesktopWidget
from PyQt5.QtCore import Qt, QTimer
class ExitDialog(QDialog):
"""TODO"""
def __init__(self):
super().__init__()
self.buttonSi = QPushButton("Yes")
self.buttonSi.clicked.connect(self.si_clicked)
self.buttonNo = QPushButton("No")
self.buttonNo.clicked.connect(self.no_clicked)
self.buttonNonUscire = QPushButton("Do not exit")
self.buttonNonUscire.clicked.connect(self.non_uscire_clicked)
self.text = QLabel("Do you want to save changes before exit?")
self.text.setAlignment(Qt.AlignCenter)
hbox1 = QHBoxLayout()
hbox1.addWidget(self.text)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.buttonSi)
hbox2.addWidget(self.buttonNo)
hbox2.addWidget(self.buttonNonUscire)
self.layout = QVBoxLayout()
self.layout.addLayout(hbox1)
self.layout.addLayout(hbox2)
self.setLayout(self.layout)
self.value_choosed = None
self.timer = QTimer()
self.timer.timeout.connect(self.setPos)
self.timer.start(1)
def setPos(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) // 2, (screen.height() - size.height()) // 2)
self.timer.stop()
def keyPressEvent(self, event):
if event.key == Qt.Key_Escape:
event.ignore()
def get_choosed_value(self):
return self.value_choosed
def si_clicked(self):
self.value_choosed = 0
self.close()
def no_clicked(self):
self.value_choosed = 1
self.close()
def non_uscire_clicked(self):
self.value_choosed = 2
self.close()
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(400,300,400,200)
vbox = QVBoxLayout()
btn = QPushButton("Exit")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
def btn_clicked(self):
self.close()
def closeEvent(self, event):
dialog = ExitDialog()
dialog.exec_()
choice = dialog.get_choosed_value()
if choice == 0:
event.accept()
elif choice == 1:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
a = QApplication(["TODO"])
w = Window()
w.show()
sys.exit(a.exec_())
Don't know if there is way to directly interact with the system's window manager.
I'm making a custom QTreeView with QFileSystem model, and I have a MouseMoveEvent set up to print the path of the item that is hovered over.
I'm way down the rabbit hole and doing all kinds of weird things to make this work.
Here is the latest minimal reproducible code:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.resize(500, 300)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.myList = CustomTreeWidget()
self.myList.model.setRootPath("/Volumes/Home/User/Desktop/testsrc")
self.myList.setObjectName("/Volumes/Home/User/Desktop/testsrc")
self.layout.addWidget(self.myList)
class CustomTreeWidget(QTreeView):
def __init__(self):
super().__init__()
self.model = QFileSystemModel()
self.model.setFilter(QDir.NoDotAndDotDot | QDir.Files)
self.setModel(self.model)
self.setAlternatingRowColors(True)
self.setDragDropMode(QAbstractItemView.DragDrop)
self.setIndentation(0)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.setMouseTracking(True)
self.model.directoryLoaded.connect(self._runwhenloaded)
def _runwhenloaded(self):
self.setRootIndex(self.model.index(self.objectName()))
self.model.setRootPath(self.objectName())
def mouseMoveEvent(self, event):
prev = ""
if self.selectedIndexes():
prev = self.selectedIndexes()[0]
x = event.x()
y = event.y()
self.setSelection(QRect(x, y, 1, 1), QItemSelectionModel.ClearAndSelect)
self.setCurrentIndex(self.selectedIndexes()[0])
print(self.model.filePath(self.currentIndex()))
if prev:
self.setCurrentIndex(prev)
# pos = QCursor.pos()
# indexat = self.indexAt(pos).row() # why -1?
# print(indexat) # why -1?
# print(self.indexAt(pos).row())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Obviously this example is not proper at all, as it destroys multiple selections and scrolls to the previously selected item whenever the mouse moves, and just a hack in general.
I've gone through many iterations and read everything I could put my hands on but I can't figure it out.
The closest answer seems to be HERE, but it's in C and I don't understand it.
So the question is: How to print the file path when the mouse hovers over an item in this QTreeView?
A possible solution is to create an event filter that tracks the hover events and, according to that information, emits a signal that has the QModelIndex:
import sys
from PyQt5.QtCore import (
pyqtSignal,
pyqtSlot,
Qt,
QDir,
QEvent,
QModelIndex,
QObject,
QPersistentModelIndex,
QStandardPaths,
)
from PyQt5.QtWidgets import (
QAbstractItemView,
QApplication,
QFileSystemModel,
QMainWindow,
QTreeView,
)
from PyQt5 import sip
class HoverViewHelper(QObject):
hovered = pyqtSignal(QModelIndex)
def __init__(self, view):
super().__init__(view)
self._current_index = QPersistentModelIndex()
if not isinstance(view, QAbstractItemView):
raise TypeError(f"The {view} must be of type QAbstractItemView")
self._view = view
self.view.viewport().setAttribute(Qt.WA_Hover)
self.view.viewport().installEventFilter(self)
#property
def view(self):
return self._view
def eventFilter(self, obj, event):
if sip.isdeleted(self.view):
return True
if self.view.viewport() is obj:
if event.type() in (QEvent.HoverMove, QEvent.HoverEnter):
p = event.pos()
index = self.view.indexAt(p)
self._update_index(index)
elif event.type() == QEvent.HoverLeave:
if self._current_index.isValid():
self._update_index(QModelIndex())
return super().eventFilter(obj, event)
def _update_index(self, index):
pindex = QPersistentModelIndex(index)
if pindex != self._current_index:
self._current_index = pindex
self.hovered.emit(QModelIndex(self._current_index))
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.model = QFileSystemModel()
self.model.setRootPath(QDir.rootPath())
self.view = QTreeView()
self.view.setModel(self.model)
path = QStandardPaths.writableLocation(QStandardPaths.DocumentsLocation)
self.view.setRootIndex(self.model.index(path))
self.setCentralWidget(self.view)
helper = HoverViewHelper(self.view)
helper.hovered.connect(self.handle_hovered)
#pyqtSlot(QModelIndex)
def handle_hovered(self, index):
if not index.isValid():
return
path = self.model.filePath(index)
print(f"path: {path}")
def main():
app = QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
app.exec_()
if __name__ == "__main__":
main()
I can rename tab label programmatically.
With QInputDialog I can get new label text and set tab widget label.
But, I hope for a more user-friendly solution like double-clicking on label and get editing on the tab itself.
A QListWidgetItem with editable flag can show me the way, but I can't find the solution for tab label.
There are no built-in methods for achieving this. However, you could use a simple popup line-edit and position it over the tab. Here's a basic demo script:
PyQt5:
import sys
from PyQt5 import QtCore, QtWidgets
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent):
super().__init__(parent)
self._editor = QtWidgets.QLineEdit(self)
self._editor.setWindowFlags(QtCore.Qt.Popup)
self._editor.setFocusProxy(self)
self._editor.editingFinished.connect(self.handleEditingFinished)
self._editor.installEventFilter(self)
def eventFilter(self, widget, event):
if ((event.type() == QtCore.QEvent.MouseButtonPress and
not self._editor.geometry().contains(event.globalPos())) or
(event.type() == QtCore.QEvent.KeyPress and
event.key() == QtCore.Qt.Key_Escape)):
self._editor.hide()
return True
return super().eventFilter(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(QtWidgets.QTabWidget):
def __init__(self):
super().__init__()
self.setTabBar(TabBar(self))
self.addTab(QtWidgets.QWidget(self), 'Tab One')
self.addTab(QtWidgets.QWidget(self), 'Tab Two')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('Test')
window.setGeometry(600, 100, 200, 100)
window.show()
sys.exit(app.exec_())
PyQt4:
from PyQt4 import QtGui, QtCore
class TabBar(QtGui.QTabBar):
def __init__(self, parent):
QtGui.QTabBar.__init__(self, parent)
self._editor = QtGui.QLineEdit(self)
self._editor.setWindowFlags(QtCore.Qt.Popup)
self._editor.setFocusProxy(self)
self._editor.editingFinished.connect(self.handleEditingFinished)
self._editor.installEventFilter(self)
def eventFilter(self, widget, event):
if ((event.type() == QtCore.QEvent.MouseButtonPress and
not self._editor.geometry().contains(event.globalPos())) or
(event.type() == QtCore.QEvent.KeyPress and
event.key() == QtCore.Qt.Key_Escape)):
self._editor.hide()
return True
return QtGui.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(QtGui.QTabWidget):
def __init__(self):
QtGui.QTabWidget.__init__(self)
self.setTabBar(TabBar(self))
self.addTab(QtGui.QWidget(self), 'Tab One')
self.addTab(QtGui.QWidget(self), 'Tab Two')
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setWindowTitle('Test')
window.setGeometry(600, 100, 200, 100)
window.show()
sys.exit(app.exec_())
Update to the answer left by ekhumoro :
In PyQt5, the code now looks like this :
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_())