ioaniatr
ioaniatr

Reputation: 277

Put a QToolBar in a QWidget instead of QMainWindow

I am trying to put a QToolBar on a layout of a QWidget instead of QMainWindow. On QMainWindow and QWidget is working fine, but when i try to add it on a layout first, is not. Am I doing something wrong? Is it possible? Here is my code:

from PyQt4 import QtGui, QtCore
import sys


img = '../../Images/logo.png'


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


    mainWin = QtGui.QMainWindow()    

    widget = QtGui.QWidget()    
    hLayout = QtGui.QHBoxLayout()

    '''ToolBar On main Window '''    

    basicToolBar = mainWin.addToolBar('Basic')
    basicToolBar.addAction(QtGui.QAction('Test', mainWin))
#    basicToolBar.addAction(QtGui.QAction(QtGui.QIcon(img), 'Test', mainWin))

#    mainWin.show()


    '''ToolBar On Widget '''

    # Case 1: Set widget as parent
#    widgetToolBar = QtGui.QToolBar(widget)
#    widgetToolBar.addAction(QtGui.QAction('Test', widget))
#    widgetToolBar.addAction(QtGui.QAction(QtGui.QIcon(img), QtGui.QAction('Test', widget))

    # Case 2: Set toolBat on a layout
    widgetToolBar = QtGui.QToolBar()
    widgetToolBar.addAction(QtGui.QAction('Test', None))
#   widgetToolBar.addAction(QtGui.QAction(QtGui.QIcon(img), 'Test', None))    
    hLayout.addWidget(widgetToolBar)  
    widget.setLayout(hLayout)

    widget.show()

    # Run 
    sys.exit(app.exec_())

Upvotes: 0

Views: 2768

Answers (2)

eyllanesc
eyllanesc

Reputation: 244301

QToolBar can only be in a QMainWindow since the QMainWindow has a special layout.

So you can use a secondary QMainWindow without problems as I show below:

from PyQt4 import QtCore, QtGui


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.tabwidget = QtGui.QTabWidget()
        self.setCentralWidget(self.tabwidget)
        for name in ("tab1", "tab2", "tab3"):
            self.create_widgets(name)

    def create_widgets(self, name):
        w = QtGui.QMainWindow()
        self.tabwidget.addTab(w, name)
        basicToolBar = w.addToolBar('Basic')
        basicToolBar.addAction('Test')
        basicToolBar.addAction(QtGui.QIcon("home.png"), 'Test')
        tab = QtGui.QTabWidget()
        w.setCentralWidget(tab)
        for i in range(10):
            tab.addTab(QtGui.QWidget(), "tab-{}".format(i))


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

enter image description here

Upvotes: 2

Gram90
Gram90

Reputation: 1

Hmmm have you read the description of QToolBar? http://doc.qt.io/qt-5/qtoolbar.html#details I think it won't work like this if your Object isn't a child of QMainWindow. The documentation says:

When a QToolBar is not a child of a QMainWindow, it loses the ability to populate the extension pop up with widgets added to the toolbar using addWidget(). Please use widget actions created by inheriting QWidgetAction and implementing QWidgetAction::createWidget() instead.

Upvotes: 0

Related Questions