I have the following test script.
from PyQt5.QtWidgets import *
import sys
class MW(QMainWindow):
def __init__(self):
super(MW, self).__init__()
widget = QWidget()
widget.setStyleSheet('QWidget {background-color:#000000; color:#FFFFFF}'
"QPushButton:hover {background-color:#202020}")
self.setCentralWidget(widget)
box = QVBoxLayout(widget)
but1_box = QHBoxLayout()
but1 = QPushButton("Button 1")
lab1 = QLabel("Label 1")
but1_box.addWidget(but1)
but1_box.addWidget(lab1)
box.addLayout(but1_box)
but2_box = QHBoxLayout()
but2 = QPushButton("Button 2")
lab2 = QLabel("Label 2")
but2_box.addWidget(but2)
but2_box.addWidget(lab2)
box.addLayout(but2_box)
self.show()
app = QApplication(sys.argv)
mw = MW()
sys.exit(app.exec_())
If I hover over the PushButton it changes its color to a gray and I want that the associated Label changes its color while hovering over the button.
It is not possible to implement directly using the pseudo-states of the Qt Style Sheet but you will have to use an eventFilter that detects the changes of the hover (enter and leave) and that modifies the stylesheet of the other elements
from PyQt5.QtCore import pyqtSignal, QEvent, QObject
from PyQt5.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
import sys
class HoverListener(QObject):
entered = pyqtSignal()
leaved = pyqtSignal()
def __init__(self, widget):
super(HoverListener, self).__init__(widget)
self._widget = widget
self.widget.installEventFilter(self)
#property
def widget(self):
return self._widget
def eventFilter(self, obj, event):
if obj == self.widget:
if event.type() == QEvent.Enter:
self.entered.emit()
elif event.type() == QEvent.Leave:
self.leaved.emit()
return super(HoverListener, self).eventFilter(obj, event)
class MW(QMainWindow):
def __init__(self):
super(MW, self).__init__()
widget = QWidget()
widget.setStyleSheet(
"QWidget {background-color:#000000; color:#FFFFFF}"
"QPushButton:hover {background-color:#202020}"
)
self.setCentralWidget(widget)
but1 = QPushButton("Button 1")
lab1 = QLabel("Label 1")
hover_listener1 = HoverListener(but1)
hover_listener1.entered.connect(
lambda label=lab1: label.setStyleSheet("background-color:#202020")
)
hover_listener1.leaved.connect(lambda label=lab1: label.setStyleSheet(""))
but2 = QPushButton("Button 2")
lab2 = QLabel("Label 2")
hover_listener2 = HoverListener(but2)
hover_listener2.entered.connect(
lambda label=lab2: label.setStyleSheet("background-color:#202020")
)
hover_listener2.leaved.connect(lambda label=lab2: label.setStyleSheet(""))
box = QVBoxLayout(widget)
but1_box = QHBoxLayout()
but1_box.addWidget(but1)
but1_box.addWidget(lab1)
box.addLayout(but1_box)
but2_box = QHBoxLayout()
but2_box.addWidget(but2)
but2_box.addWidget(lab2)
box.addLayout(but2_box)
self.show()
app = QApplication(sys.argv)
mw = MW()
sys.exit(app.exec_())
Related
I wrote a QMainWindow, two QDialog, and one QMessageBox. The code is as follows:
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QPushButton, QMessageBox, QApplication, QAction, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
action = QAction("Open DocumentDialog", self)
action.triggered.connect(self.show_main_dialog)
menu_bar = self.menuBar()
file = menu_bar.addMenu("File")
file.addAction(action)
self.setWindowTitle("MainWindow")
def show_main_dialog(self):
main_dialog = DocumentDialog(self)
main_dialog.show()
class DocumentDialog(QDialog):
def __init__(self, parent):
super().__init__(parent)
vbox = QVBoxLayout()
btn = QPushButton("Open DocumentDetailDialog")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
self.setWindowTitle("DocumentDialog")
def btn_clicked(self):
detail_dialog = DetailDialog(self)
detail_dialog.show()
class DetailDialog(QDialog):
def __init__(self, parent: QDialog):
super().__init__(parent)
vbox = QVBoxLayout()
btn = QPushButton("Open delete message box")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
self.setWindowTitle("DetailDialog")
def btn_clicked(self):
msg_box = QMessageBox(self)
msg_box.setIcon(QMessageBox.Warning)
msg_box.setText("Are you sure?")
msg_box.setWindowTitle("Delete")
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg_box.buttonClicked.connect(self.delete_handler)
msg_box.show()
def delete_handler(self, btn):
self.close()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec())
The process is as follows:
Start the app, then MainWindow shows itself.
Click on the menu item to show DocumentDialog.
Click on the button on DocumentDialog to show DetailDialog.
Click on the button on DetailDialog to show a QMessageBox.
Click on the button on QMessageBox to close it along with DetailDialog.
The QMessageBox is closed, as expected. the weird thing is, DocumentDialog is over MainWindow when it shows up. But after QMessageBox is closed, DocumentDialog hides behind MainWindow, why? How not to let DocumentDialog change it position?
I record a GIF to illustrate the process:
You should set Qt.Popup Flag for your Dialog :
This makes your Dialog always on top of others.
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QPushButton, QMessageBox, QApplication, QAction, QMainWindow
from PyQt5.QtCore import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
action = QAction("Open DocumentDialog", self)
action.triggered.connect(self.show_main_dialog)
menu_bar = self.menuBar()
file = menu_bar.addMenu("File")
file.addAction(action)
self.setWindowTitle("MainWindow")
def show_main_dialog(self):
main_dialog = DocumentDialog(self)
main_dialog.show()
class DocumentDialog(QDialog):
def __init__(self, parent):
super().__init__(parent)
vbox = QVBoxLayout()
btn = QPushButton("Open DocumentDetailDialog")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
self.setWindowTitle("DocumentDialog")
def btn_clicked(self):
detail_dialog = DetailDialog(self)
detail_dialog.show()
class DetailDialog(QDialog):
def __init__(self, parent: QDialog):
super().__init__(parent)
vbox = QVBoxLayout()
btn = QPushButton("Open delete message box")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
self.setWindowTitle("DetailDialog")
self.setWindowFlags(self.windowFlags() |Qt.Popup)
def btn_clicked(self):
msg_box = QMessageBox(self)
msg_box.setIcon(QMessageBox.Warning)
msg_box.setText("Are you sure?")
msg_box.setWindowTitle("Delete")
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg_box.buttonClicked.connect(self.delete_handler)
msg_box.show()
def delete_handler(self, btn):
self.close()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec())
Result:
Edited:
From Dialog modal
This property holds whether show() should pop up the dialog as modal
or modeless
By default, this property is false and show() pops up the dialog as
modeless. Setting this property to true is equivalent to setting
QWidget::windowModality to Qt::ApplicationModal.
exec() ignores the value of this property and always pops up the
dialog as modal.
Then as #musicamante said you should change show to exec :
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
action = QAction("Open DocumentDialog", self)
action.triggered.connect(self.show_main_dialog)
menu_bar = self.menuBar()
file = menu_bar.addMenu("File")
file.addAction(action)
self.setWindowTitle("MainWindow")
def show_main_dialog(self):
main_dialog = DocumentDialog(self)
main_dialog.exec()
class DocumentDialog(QDialog):
def __init__(self, parent):
super().__init__(parent)
vbox = QVBoxLayout()
btn = QPushButton("Open DocumentDetailDialog")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
self.setWindowTitle("DocumentDialog")
def btn_clicked(self):
detail_dialog = DetailDialog(self)
detail_dialog.exec()
class DetailDialog(QDialog):
def __init__(self, parent: QDialog):
super().__init__(parent)
vbox = QVBoxLayout()
btn = QPushButton("Open delete message box")
btn.clicked.connect(self.btn_clicked)
vbox.addWidget(btn)
self.setLayout(vbox)
self.setWindowTitle("DetailDialog")
def btn_clicked(self):
msg_box = QMessageBox(self)
msg_box.setIcon(QMessageBox.Warning)
msg_box.setText("Are you sure?")
msg_box.setWindowTitle("Delete")
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg_box.buttonClicked.connect(self.delete_handler)
msg_box.exec()
def delete_handler(self, btn):
self.close()
I've seen a number of replies on SO regarding this matter but not specifically to QMenu and QToolButton. Would appreciate some pointers on aligning the popup widget to the right side of the button. Here's a basic code I'm working off..
import sys
from PyQt5.QtWidgets import *
class test(QWidget):
def __init__(self):
super().__init__()
self.resize(200, 100)
layout = QHBoxLayout(self)
label = QLabel('Testing QToolButton Popup')
toolbutton = QToolButton()
toolbutton.setPopupMode(QToolButton.InstantPopup)
widget = QWidget()
widgetLayout = QHBoxLayout(widget)
widgetLabel = QLabel('Popup Text')
widgetSpinbox = QSpinBox()
widgetLayout.addWidget(widgetLabel)
widgetLayout.addWidget(widgetSpinbox)
widgetAction = QWidgetAction(toolbutton)
widgetAction.setDefaultWidget(widget)
widgetMenu = QMenu(toolbutton)
widgetMenu.addAction(widgetAction)
toolbutton.setMenu(widgetMenu)
layout.addWidget(label)
layout.addWidget(toolbutton)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = test()
win.show()
sys.exit(app.exec_())
The outcome looks like this:
The Qt developer thought the default position was correct, so if you want to modify the alignment you must move the QMenu as I show below:
import sys
from PyQt5.QtCore import QPoint
from PyQt5.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QMenu,
QSpinBox,
QToolButton,
QWidgetAction,
QWidget,
)
class Menu(QMenu):
def showEvent(self, event):
if self.isVisible():
button = self.parentWidget()
if button is not None:
pos = button.mapToGlobal(button.rect().bottomRight())
self.move(pos - self.rect().topRight())
super().showEvent(event)
class Test(QWidget):
def __init__(self):
super().__init__()
self.resize(200, 100)
layout = QHBoxLayout(self)
label = QLabel("Testing QToolButton Popup")
toolbutton = QToolButton(popupMode=QToolButton.InstantPopup)
widgetLabel = QLabel("Popup Text")
widgetSpinbox = QSpinBox()
widget = QWidget()
widgetLayout = QHBoxLayout(widget)
widgetLayout.addWidget(widgetLabel)
widgetLayout.addWidget(widgetSpinbox)
widgetAction = QWidgetAction(toolbutton)
widgetAction.setDefaultWidget(widget)
widgetMenu = Menu(toolbutton)
widgetMenu.addAction(widgetAction)
toolbutton.setMenu(widgetMenu)
layout.addWidget(label)
layout.addWidget(toolbutton)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Test()
win.show()
sys.exit(app.exec_())
Is it possible to run a custom method on clicking the ClearButton in a QLineEdit?
For instance i have a ClearButton inside a QCombobox and i want to set a default-index on clicking the ClearButton in this ComboBox.
I have already tried to subclass the 'clear()' slot of the QLineEdit, but without success.
qtawesome
import sys
from PyQt5.QtWidgets import (QLineEdit, QApplication, QWidget, QVBoxLayout)
import qtawesome as qt
class Widget(QWidget):
def __init__(self, parent=None):
super(QWidget, self).__init__(parent)
self.flag = 0
self.layout = QVBoxLayout()
self.line_edit = QLineEdit()
self.line_edit.setClearButtonEnabled(False)
self.line_edit.textChanged.connect(self._on_line_edit_text_changed)
self.clear_icon = qt.icon('mdi.delete-circle-outline', color='gray', color_active='black')
self.clear_action = None
self.layout.addWidget(self.line_edit)
self.setLayout(self.layout)
def _on_line_edit_text_changed(self):
if self.line_edit and self.line_edit.text():
if not self.clear_action:
self.clear_action = self.line_edit.addAction(self.clear_icon, QLineEdit.TrailingPosition)
self.clear_action.triggered.connect(self._on_clear_clicked)
elif self.clear_action and self.line_edit and not self.line_edit.text():
self.line_edit.removeAction(self.clear_action)
self.clear_action = None
def _on_clear_clicked(self):
self.line_edit.clear()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
I am trying to figure out a way to customize the scrollbars for QListWidget to have the scrollbars above and below the QListWidget instead of the normal vertical and horizontal scrollbars.
Please check out my example below if you don't understand what I mean.
In the example below I use QPushButtons with QTimers controlling the scrolling in place of the scrollbars but what I am looking for are scrollbars like the ones in QMenu when menu scrolling is enabled.
If that is not an option, I am wondering if there is a scrollbar signal or something that I could try to use to know when the scrollbars are normally activated? That way I can show/hide the buttons as needed. Thanks.
import sys
from PyQt5.QtCore import pyqtSignal, QTimer, Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QWidget, QPushButton, QVBoxLayout, \
QApplication, QStyle, QListWidget, QStyleOptionButton, QListWidgetItem
class UpBtn(QPushButton):
mouseHover = pyqtSignal()
def __init__(self):
QPushButton.__init__(self)
self.setMouseTracking(True)
self.timer = QTimer()
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
opt = QStyleOptionButton()
self.initStyleOption(opt)
self.style().drawControl(QStyle.CE_ScrollBarSubLine, opt, painter, self)
painter.end()
def startScroll(self):
self.mouseHover.emit()
def enterEvent(self, event):
self.timer.timeout.connect(self.startScroll)
self.timer.start(120)
def leaveEvent(self, event):
self.timer.stop()
class DwnBtn(QPushButton):
mouseHover = pyqtSignal()
def __init__(self):
QPushButton.__init__(self)
self.setMouseTracking(True)
self.timer = QTimer()
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
opt = QStyleOptionButton()
self.initStyleOption(opt)
self.style().drawControl(QStyle.CE_ScrollBarAddLine, opt, painter, self)
painter.end()
def startScroll(self):
self.mouseHover.emit()
def enterEvent(self, event):
self.timer.timeout.connect(self.startScroll)
self.timer.start(120)
def leaveEvent(self, event):
self.timer.stop()
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.layout = QVBoxLayout()
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
self.upBtn = UpBtn()
self.upBtn.setFixedWidth(230)
self.layout.addWidget(self.upBtn)
self.listWidget = QListWidget()
self.listWidget.setFixedWidth(230)
self.listWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.listWidget.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.layout.addWidget(self.listWidget)
self.downBtn = DwnBtn()
self.downBtn.setFixedWidth(230)
self.layout.addWidget(self.downBtn)
self.setLayout(self.layout)
self.upBtn.clicked.connect(self.upBtnClicked)
self.upBtn.mouseHover.connect(self.upBtnClicked)
self.downBtn.clicked.connect(self.downBtnClicked)
self.downBtn.mouseHover.connect(self.downBtnClicked)
for i in range(100):
item = QListWidgetItem()
item.setText("list item " + str(i))
self.listWidget.addItem(item)
def upBtnClicked(self):
cur = self.listWidget.currentRow()
self.listWidget.setCurrentRow(cur - 1)
def downBtnClicked(self):
cur = self.listWidget.currentRow()
self.listWidget.setCurrentRow(cur + 1)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
EDIT:
Here is an example image for what I am talking about. This is a scrollable QMenu.
EDIT:
Scrollable QMenu code.
Uncomment the commented parts to get a fixed size like in the image. Normally Qmenu scrolling only works when the menu items exceed the screen height. I am just looking for the top and bottom hover style scrolling but to be used in QListWidget.
import sys
from PyQt5.QtCore import QPoint, QEvent
from PyQt5.QtWidgets import QWidget, QPushButton, QVBoxLayout, \
QApplication, QAction, QMenu, QProxyStyle, QStyle
class MyMenu(QMenu):
def event(self, event):
if event.type() == QEvent.Show:
self.move(self.parent().mapToGlobal(QPoint(-108, 0)))
return super(MyMenu, self).event(event)
# class CustomStyle(QProxyStyle):
# def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None):
# if QStyle_PixelMetric == QStyle.PM_MenuScrollerHeight:
# return 15
# if QStyle_PixelMetric == QStyle.PM_MenuDesktopFrameWidth:
# return 290
# else:
# return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.layout = QVBoxLayout()
self.btn = QPushButton("Button")
self.btn.setFixedHeight(30)
self.btn.setFixedWidth(100)
self.myMenu = MyMenu("Menu", self.btn)
self.btn.setMenu(self.myMenu)
self.layout.addWidget(self.btn)
self.setLayout(self.layout)
menus = []
for _ in range(5):
myMenus = QMenu("Menu"+str(_+1), self.btn)
# myMenus.setFixedHeight(120)
myMenus.setStyleSheet("QMenu{menu-scrollable: 1; }")
menus.append(myMenus)
for i in menus:
self.btn.menu().addMenu(i)
for item in range(100):
action = QAction("item" + str(item), self)
i.addAction(action)
if __name__ == '__main__':
app = QApplication(sys.argv)
# app.setStyle(CustomStyle())
w = MainWindow()
w.show()
app.exec_()
The idea is to obtain the row of the upper and lower element that will decide whether the buttons are hidden or not, for that we use the method itemAt () that returns the item given the geometrical coordinates. On the other hand I have improved this calculation has to do every time they change the number of items in the QListView for that we use the signals of the internal model.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Button(QtWidgets.QPushButton):
moveSignal = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
super(Button, self).__init__(*args, **kwargs)
self.m_timer = QtCore.QTimer(self, interval=120)
self.m_timer.timeout.connect(self.moveSignal)
self.setMouseTracking(True)
self.setFixedHeight(20)
def mouseReleaseEvent(self, e):
super(Button, self).mousePressEvent(e)
self.setDown(True)
def enterEvent(self, e):
self.setDown(True)
self.m_timer.start()
super(Button, self).enterEvent(e)
def leaveEvent(self, e):
self.setDown(False)
self.m_timer.stop()
super(Button, self).leaveEvent(e)
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.setFixedWidth(230)
icon = self.style().standardIcon(QtWidgets.QStyle.SP_ArrowUp)
self.upBtn = Button(icon=icon)
self.upBtn.moveSignal.connect(self.moveUp)
icon = self.style().standardIcon(QtWidgets.QStyle.SP_ArrowDown)
self.downBtn = Button(icon=icon)
self.downBtn.moveSignal.connect(self.moveDown)
self.listWidget = QtWidgets.QListWidget()
self.listWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.listWidget.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.addWidget(self.upBtn)
layout.addWidget(self.listWidget)
layout.addWidget(self.downBtn)
self.adjust_buttons()
self.create_connections()
def create_connections(self):
self.listWidget.currentItemChanged.connect(self.adjust_buttons)
model = self.listWidget.model()
model.rowsInserted.connect(self.adjust_buttons)
model.rowsRemoved.connect(self.adjust_buttons)
model.rowsMoved.connect(self.adjust_buttons)
model.modelReset.connect(self.adjust_buttons)
model.layoutChanged.connect(self.adjust_buttons)
#QtCore.pyqtSlot()
def adjust_buttons(self):
first = self.listWidget.itemAt(QtCore.QPoint())
r = self.listWidget.row(first)
self.upBtn.setVisible(r != 0 and r!= -1)
last = self.listWidget.itemAt(self.listWidget.viewport().rect().bottomRight())
r = self.listWidget.row(last)
self.downBtn.setVisible( r != (self.listWidget.count() -1) and r != -1)
#QtCore.pyqtSlot()
def moveUp(self):
ix = self.listWidget.moveCursor(QtWidgets.QAbstractItemView.MoveUp, QtCore.Qt.NoModifier)
self.listWidget.setCurrentIndex(ix)
#QtCore.pyqtSlot()
def moveDown(self):
ix = self.listWidget.moveCursor(QtWidgets.QAbstractItemView.MoveDown, QtCore.Qt.NoModifier)
self.listWidget.setCurrentIndex(ix)
#QtCore.pyqtSlot(str)
def add_item(self, text):
item = QtWidgets.QListWidgetItem(text)
self.listWidget.addItem(item)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
for i in range(100):
window.add_item("item {}".format(i))
window.show()
sys.exit(app.exec_())
I just need to know if I can make a slot or signal between a pushbutton and specific page in tabwidget or no !?
If yes then how?
I divide a tab widget into 4 pages. My ask is about if I can make a slot or signal between pushbutton in first page and second page or not.
Try it:
from PyQt5.QtWidgets import (QWidget, QApplication, QTabWidget, QLabel, QPushButton,
QVBoxLayout, QMenuBar, QAction)
class MainWidget(QWidget):
def __init__(self, textLabel, *args, **kwargs):
super(MainWidget, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
self.label = QLabel(textLabel, self)
self.btn = QPushButton('Next', self)
layout.addWidget(self.label)
layout.addWidget(self.btn)
class Window(QWidget):
def __init__(self):
super().__init__()
bar = QMenuBar(self)
menu = bar.addMenu('File')
action = QAction('Close activ tab', self)
menu.addAction(action)
action.triggered.connect(self.closeActivTab)
self.tabwdg = QTabWidget()
self.tabwdg.setTabsClosable(True)
self.tabWidget = MainWidget('this is the first page')
self.tabwdg.addTab(self.tabWidget, 'first')
self.tabWidget.btn.clicked.connect(self.numTab)
self.tabWidget = MainWidget('this is the second page')
self.tabwdg.addTab(self.tabWidget, 'second')
self.tabWidget.btn.clicked.connect(self.numTab)
self.tabWidget = MainWidget('this is the third page')
self.tabwdg.addTab(self.tabWidget, 'third')
self.tabWidget.btn.clicked.connect(self.numTab)
self.tabWidget = MainWidget('this is the fourth page')
self.tabwdg.addTab(self.tabWidget, 'fourth')
self.tabWidget.btn.clicked.connect(self.numTab)
self.tabwdg.tabCloseRequested.connect(self.closeTab)
box = QVBoxLayout()
box.addWidget(bar)
box.addWidget(self.tabwdg)
self.setLayout(box)
def numTab(self):
nextTab = self.tabwdg.currentIndex()+1
if nextTab == self.tabwdg.count():
nextTab = 0
self.tabwdg.setCurrentIndex(nextTab)
self.tabwdg.setCurrentWidget(self.tabwdg.currentWidget())
def closeActivTab(self):
activ_tab_ind = self.tabwdg.currentIndex()
self.closeTab(activ_tab_ind)
def closeTab(self, ind):
self.tabwdg.removeTab(ind)
if __name__ == '__main__':
import sys
app = QApplication([''])
w = Window()
w.resize(400, 300)
w.show()
sys.exit(app.exec_())