0

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ioaniatr
  • 277
  • 4
  • 15
  • What does it mean that I do not work? – eyllanesc Aug 09 '18 at 12:53
  • Put on a layout is not working (Case 2) – ioaniatr Aug 09 '18 at 12:58
  • I'm curious why you do not want to use QMainWindow ?, I just checked the source code of QMainWindow and it has a custom layout. – eyllanesc Aug 09 '18 at 13:07
  • Because this is going to be on a QTabWidget, and the QTabWidget within a QMainWindow. At least this is what I am trying to make. – ioaniatr Aug 09 '18 at 13:17
  • you could put a picture of what you want to get, I think you could use a second QMainWindow but it will not be clear until you fully understand what you want. On the other hand you have a [XY Problem](http://xyproblem.info/), I find it difficult to implement a QToolBar in a QWidget – eyllanesc Aug 09 '18 at 13:21
  • So, you think that the best solution would be to put a secondary QMainWindow ? – ioaniatr Aug 09 '18 at 13:26
  • yes, but if you want a solution I would have to understand how you want the widget to be displayed. – eyllanesc Aug 09 '18 at 13:28
  • QMainWindow with a QTabWidget as a central widget. Each tab has a QWidget. Each QWidget will have a different toolbar and their own QTabWidget too. For now, i only want to put a toolbar within each tab. – ioaniatr Aug 09 '18 at 13:33
  • Do you want the following? https://imgur.com/a/IrhezmI – eyllanesc Aug 09 '18 at 13:42
  • Yes, and below the toolBar another QTabWidget. Now you have the full picture – ioaniatr Aug 09 '18 at 13:48
  • Is this what you want or something else? https://imgur.com/a/OwBveLO – eyllanesc Aug 09 '18 at 13:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177739/discussion-between-ioaniatr-and-eyllanesc). – ioaniatr Aug 09 '18 at 13:55

2 Answers2

2

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I Just tested your example, seems that is working fine. But, you say that I can NOT also use a secondary QMainWindow without problems ? What problems do you think that I would have? – ioaniatr Aug 09 '18 at 14:14
  • @ioaniatr No, no problem, I'm a little tired and I wrote it wrong, :) – eyllanesc Aug 09 '18 at 14:18
0

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.

Gram90
  • 1
  • 4
  • I haven't try to put some actions on it yet. I am trying only to make it show. Am I going to lose the ability to put actions there like 'select file from folder' ? – ioaniatr Aug 09 '18 at 13:14
  • seems like. Do I get you right? You just want to show a toolbar on a widget in your application, not on the (Main)Window of your application...??? – Gram90 Aug 09 '18 at 13:38
  • Yes, but if it is not possible, i am thinking to replace the widget with a secondary QMainWindow. Not sure what would be the best. – ioaniatr Aug 09 '18 at 13:50