(Python 3.7 / pyqt5) I know how to set up a toolbar based on the menu but I am trying to make the toolbar adjustable by the user which means I will have to store the layout in my database and then create it based on the query results -- so here is my test program and while it kind of works the most important part does not work as I have denoted -- this works except you will have to make an images directory and put the 3 images you want to use within it:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class CenterPanel(QWidget):
def __init__(self, MainWin):
QWidget.__init__(self)
self.MainWin = MainWin
CntrPane = QSplitter(Qt.Horizontal, self)
CntrPane.addWidget(QTextEdit())
CntrPane.addWidget(QTextEdit())
CntrPane.setSizes([75,200])
hbox = QHBoxLayout(self)
hbox.addWidget(CntrPane)
self.setLayout(hbox)
@property
def MainWin(self):
return self.__parent
@MainWin.setter
def MainWin(self, value):
self.__parent = value
class MenuToolBar(QDockWidget):
def __init__(self, MainWin):
QDockWidget.__init__(self)
self.MainWin = MainWin
self.MainMenu = MainWin.menuBar()
# ******* Create the File Menu *******
self.FileMenu = self.MainMenu.addMenu('File')
self.NewFileAct = QAction(QIcon('img/new.png'), 'New File', self)
self.NewFileAct.setShortcut("Ctrl+N")
self.NewFileAct.setStatusTip('Create a New Project File')
self.NewFileAct.triggered.connect(self.NewProjFile)
# ******* Create File Menu Items *******
self.OpnFileAct = QAction(QIcon('img/open.png'), 'Open File', self)
self.OpnFileAct.setShortcut("Ctrl+O")
self.OpnFileAct.setStatusTip('Open an Existing Project File')
self.OpnFileAct.triggered.connect(self.OpenProjFile)
self.SavFileAct = QAction(QIcon('img/save.png'), 'Save File', self)
self.SavFileAct.setShortcut("Ctrl+S")
self.SavFileAct.setStatusTip('Save Current Project File')
self.SavFileAct.triggered.connect(self.SaveProjFile)
# ******* Setup the File Menu *******
self.FileMenu.addAction(self.NewFileAct)
self.FileMenu.addSeparator()
self.FileMenu.addAction(self.OpnFileAct)
self.FileMenu.addSeparator()
self.FileMenu.addAction(self.SavFileAct)
self.InitToolBar(MainWin)
def InitToolBar(self, MainWin):
# Add Items to the Toolbar
# This needs to be dynamically based on user adjustments if any
self.mainToolBar = MainWin.addToolBar("Quick Access")
# This how I would do it normally and it works just fine
# self.mainToolBar.addAction(self.OpnFileAct)
# self.mainToolBar.addAction(self.SavFileAct)
# self.mainToolBar.addAction(self.NewFileAct)
# Here I am trying to figure out how to load the various items dynamically if stored as follows:
# It sort of works I just need to translate the Action string
#
NewToolBarLayout = {0:{'addAction': 'self.NewFileAct'},
1:{'addSeparator': ''},
2:{'addAction': 'self.OpnFileAct'},
3:{'addSeparator': ''},
4:{'addAction': 'self.SavFileAct'}}
#
for idx in NewToolBarLayout:
item = NewToolBarLayout[idx]
if 'addAction' in item.keys():
value = item['addAction']
# How to translate the above so that the stored 'self.OpnProjAct' string
# for instance becomes the same internal value it is defined as
self.mainToolBar.addAction(value)
elif 'addSeparator' in item.keys():
self.mainToolBar.addSeparator()
#
def NewProjFile(self):
print("Added New Project File")
def OpenProjFile(self):
print("Opened Existing Project File")
def SaveProjFile(self):
print("Saved Current Project File")
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.title = 'New Project'
self.LeftEdge = 100
self.TopEdge = 100
self.WinWidth = 800
self.WinHeight = 600
self.setWindowTitle(self.title)
self.setGeometry(self.LeftEdge, self.TopEdge, self.WinWidth, self.WinHeight)
self.CenterPane = CenterPanel(self)
self.setCentralWidget(self.CenterPane)
self.MenuToolBar = MenuToolBar(self)
self.setStyle(QStyleFactory.create('Cleanlooks'))
if __name__ == '__main__':
newProj = QApplication([])
GUI = Window()
GUI.show()
sys.exit(newProj.exec_())
