TransWikia.com

How to visualize my QtWidgets.QToolButton

Stack Overflow Asked on December 2, 2020

Hello I have this code using python and pyqt5 which allows to display a graphical interface :

import sys
from PyQt5 import QtCore, QtWidgets

class TabPage(QtWidgets.QWidget):
    def __init__(self, parent=None):
     super().__init__(parent)
     group = QtWidgets.QGroupBox('Monty Python')
     layout = QtWidgets.QVBoxLayout(self)
     layout.addWidget(group)
     grid = QtWidgets.QGridLayout(group)
     grid.addWidget(QtWidgets.QLabel('Enter a name:'), 0, 0)
     grid.addWidget(QtWidgets.QLabel('Choose a number:'), 0, 1)
     grid.addWidget(QtWidgets.QLineEdit(), 1, 0)
     grid.addWidget(QtWidgets.QComboBox(), 1, 1)
     grid.addWidget(QtWidgets.QPushButton('Click Me!'), 1, 2)
     grid.addWidget(QtWidgets.QSpinBox(), 2, 0)
     grid.addWidget(QtWidgets.QPushButton('Clear Text'), 2, 2)
     grid.addWidget(QtWidgets.QTextEdit(), 3, 0, 1, 3)

class Window(QtWidgets.QWidget):
    def __init__(self):
     super().__init__()
     self.tabs = QtWidgets.QTabWidget()
     layout = QtWidgets.QVBoxLayout(self)
     layout.addWidget(self.tabs)

     button = QtWidgets.QToolButton()
     button.setToolTip('Add New Tab')
     button.clicked.connect(self.addNewTab)
     button.setIcon(self.style().standardIcon(
  QtWidgets.QStyle.SP_DialogYesButton))
     self.tabs.setCornerWidget(button, QtCore.Qt.TopRightCorner)

     button1 = QtWidgets.QToolButton()
     button1.setToolTip('Remove')
     button1.clicked.connect(self.addNewTab)
     button1.setIcon(self.style().standardIcon(
     QtWidgets.QStyle.SP_BrowserStop))
     self.tabs.setCornerWidget(button1, QtCore.Qt.TopRightCorner)

     self.addNewTab()

    def addNewTab(self):
     text = 'Tab %d' % (self.tabs.count() + 1)
     self.tabs.addTab(TabPage(self.tabs), text)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())

When I execute my code I get this :

enter image description here

whereas I would like to get something like this :

enter image description here

How can I do to do this ?

Thank you a lot !

2 Answers

Since the other answer does not explain the cause of the problem and its code is confusing, I will explain the error in detail.

The error is that there can only be one cornerWidget, if you set a second cornerWidget it will replace the previous one, therefore only one QToolButton is observed. If you want to show several widgets then you have to use a container like a QWidget and place the other widgets there.

# ...
layout.addWidget(self.tabs)

button = QtWidgets.QToolButton()
button.setToolTip("Add New Tab")
button.clicked.connect(self.addNewTab)
button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogYesButton))

button1 = QtWidgets.QToolButton()
button1.setToolTip("Remove")
button1.clicked.connect(self.addNewTab)
button1.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_BrowserStop))

container = QtWidgets.QWidget()
container.setContentsMargins(0, 0, 0, 0)
lay = QtWidgets.QHBoxLayout(container)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(button)
lay.addWidget(button1)

self.tabs.setCornerWidget(container, QtCore.Qt.TopRightCorner)

self.addNewTab()

Answered by eyllanesc on December 2, 2020

QTabWidget::setCornerWidget(QWidget *widget, Qt::Corner corner = Qt::TopRightCorner)

Any previously set corner widget is hidden. https://doc.qt.io/qt-5/qtabwidget.html#setCornerWidget

Try it:

import sys
from PyQt5 import QtCore, QtWidgets

class TabPage(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        group = QtWidgets.QGroupBox('Monty Python')
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(group)
        grid = QtWidgets.QGridLayout(group)
        grid.addWidget(QtWidgets.QLabel('Enter a name:'), 0, 0)
        grid.addWidget(QtWidgets.QLabel('Choose a number:'), 0, 1)
        grid.addWidget(QtWidgets.QLineEdit(), 1, 0)
        grid.addWidget(QtWidgets.QComboBox(), 1, 1)
        grid.addWidget(QtWidgets.QPushButton('Click Me!'), 1, 2)
        grid.addWidget(QtWidgets.QSpinBox(), 2, 0)
        grid.addWidget(QtWidgets.QPushButton('Clear Text'), 2, 2)
        grid.addWidget(QtWidgets.QTextEdit(), 3, 0, 1, 3)

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.tabs = QtWidgets.QTabWidget()      
        
        self.tabs.setTabsClosable(True)                                         # +
        self.tabs.tabCloseRequested.connect(self.qtabwidget_tabcloserequested)  # +
        
        self.tabs.currentChanged.connect(lambda: print(f'currentIndex->{self.tabs.currentIndex()}'))  #+
        
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.tabs)

        button = QtWidgets.QToolButton()
        button.setFixedSize(20, 20)                                             # +
        button.setToolTip('Add New Tab')
        button.clicked.connect(self.addNewTab)
        button.setIcon(self.style().standardIcon(
             QtWidgets.QStyle.SP_DialogYesButton))
#        self.tabs.setCornerWidget(button, QtCore.Qt.TopRightCorner)
        
        button1 = QtWidgets.QToolButton()
        button1.setFixedSize(20, 20)                                            # +
        button1.setToolTip('Remove')
        button1.clicked.connect(self.removeTab)                                 # removeTab
        button1.setIcon(self.style().standardIcon(
            QtWidgets.QStyle.SP_BrowserStop))
            
        # Any previously set corner widget is hidden.  
#        self.tabs.setCornerWidget(button1, QtCore.Qt.TopRightCorner)      #

# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        self.widget = QtWidgets.QWidget()
        h_layout = QtWidgets.QHBoxLayout(self.widget)
        h_layout.setContentsMargins(0, 0, 0, 0)
        h_layout.addWidget(button)
        h_layout.addWidget(button1)
        self.tabs.setCornerWidget(self.widget, QtCore.Qt.TopRightCorner)   
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        
           
        self.addNewTab()

    def addNewTab(self):
        text = 'Tab %d' % (self.tabs.count() + 1)
        self.tabs.addTab(TabPage(self.tabs), text)
        
    @QtCore.pyqtSlot(int)
    def qtabwidget_tabcloserequested(self, index):
        # gets the widget
        widget = self.tabs.widget(index)
        # if the widget exists
        if widget:
            # removes the widget
            widget.deleteLater()
        # removes the tab of the QTabWidget
        self.tabs.removeTab(index)
        
    def removeTab(self):
        print('def removeTab(self): print')
        

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 200)
    window.show()
    sys.exit(app.exec_())

enter image description here

Answered by S. Nick on December 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP