What object to use that sllows class access to toolbar, menu, statusbar?
-
Hi,
I have a PySide6 window that I would like to update the Menu bar, Toolbar, Statusbar, Actions for, from class files.
I have a bare window that I would like to dynamically populate upon selection of an option by the user.
This would display different configurations depending on the user's selection.
A bit like changing channels on TV.
The main application screen changes to BBC1 or 2 or ITV, but the Volume and Contrast remain the same unless those universal actions are called.
The same with my app, HELP would always display ABOUT, QT ABOUT and be accessible from and screen. FILE would always allow EXIT.
But, by the use of class files I should be able to add or remove activities from these menus add an EDIT menu etc plus the corresponding toolbar option.
If the user then wants to switch to a different config, the base configuration is retained and only the new aspects are added.
I can populate a TabPageWidget via calling a class, I can redecorate the menu with new option but cannot create actions to populate a toolbar, display status.
It appears that if I pass the QMainWindiw to the class the named MenuBar is passed into the class as is the TabWidget Panel, but not the toolbar or status bar.
So I assume I am passing the wrong object.
I have tried other objects such as app, window and self but that does not seem to work. Is there a master object that will allow me to inherit the menu, toolbar, status bar and actions from the main window.
If not what can I do?
Thank you and kind regards,
jB 😎 -
Hi,
I have a PySide6 window that I would like to update the Menu bar, Toolbar, Statusbar, Actions for, from class files.
I have a bare window that I would like to dynamically populate upon selection of an option by the user.
This would display different configurations depending on the user's selection.
A bit like changing channels on TV.
The main application screen changes to BBC1 or 2 or ITV, but the Volume and Contrast remain the same unless those universal actions are called.
The same with my app, HELP would always display ABOUT, QT ABOUT and be accessible from and screen. FILE would always allow EXIT.
But, by the use of class files I should be able to add or remove activities from these menus add an EDIT menu etc plus the corresponding toolbar option.
If the user then wants to switch to a different config, the base configuration is retained and only the new aspects are added.
I can populate a TabPageWidget via calling a class, I can redecorate the menu with new option but cannot create actions to populate a toolbar, display status.
It appears that if I pass the QMainWindiw to the class the named MenuBar is passed into the class as is the TabWidget Panel, but not the toolbar or status bar.
So I assume I am passing the wrong object.
I have tried other objects such as app, window and self but that does not seem to work. Is there a master object that will allow me to inherit the menu, toolbar, status bar and actions from the main window.
If not what can I do?
Thank you and kind regards,
jB 😎@britesc said in What object to use that sllows class access to toolbar, menu, statusbar?:
I can populate a TabPageWidget via calling a class, I can redecorate the menu with new option but cannot create actions to populate a toolbar, display status.
It appears that if I pass the QMainWindiw to the class the named MenuBar is passed into the class as is the TabWidget Panel, but not the toolbar or status bar.
So I assume I am passing the wrong object.
I have tried other objects such as app, window and self but that does not seem to work. Is there a master object that will allow me to inherit the menu, toolbar, status bar and actions from the main window.Don't understand any of this. If you are choosing to pass a
QMainWindow
instance around to other classes/code they/that can access any of its members. Maybe show some minimal code illustrating whatever your issue is. -
Hi,
Rather that moving your main window around, you should create a base widget with an API that returns the required menus, actions, etc. as well as signals that will transmit whatever information you want.
That way, when you change widget, you can simply update your main window. These child widgets should not care about that main window, it's not their job to change it.
-
@britesc said in What object to use that sllows class access to toolbar, menu, statusbar?:
I can populate a TabPageWidget via calling a class, I can redecorate the menu with new option but cannot create actions to populate a toolbar, display status.
It appears that if I pass the QMainWindiw to the class the named MenuBar is passed into the class as is the TabWidget Panel, but not the toolbar or status bar.
So I assume I am passing the wrong object.
I have tried other objects such as app, window and self but that does not seem to work. Is there a master object that will allow me to inherit the menu, toolbar, status bar and actions from the main window.Don't understand any of this. If you are choosing to pass a
QMainWindow
instance around to other classes/code they/that can access any of its members. Maybe show some minimal code illustrating whatever your issue is.@JonB Thank you for your reply.
QMainWindow is in this case constructed from UI_MainWindow where menubar, toolbar and statusbar bar are added to the window.
If you look at the attributes of a QMainWindow when passed to a class, out of the 3 menubar. toolbar and statusbar, only menubar is a passed, accessible attributes.
Therefore I can amend menubar but not natively access toolbar and statusbar.
Hence my question.
Thanks, jB 😎 -
@JonB Thank you for your reply.
QMainWindow is in this case constructed from UI_MainWindow where menubar, toolbar and statusbar bar are added to the window.
If you look at the attributes of a QMainWindow when passed to a class, out of the 3 menubar. toolbar and statusbar, only menubar is a passed, accessible attributes.
Therefore I can amend menubar but not natively access toolbar and statusbar.
Hence my question.
Thanks, jB 😎@britesc said in What object to use that sllows class access to toolbar, menu, statusbar?:
If you look at the attributes of a QMainWindow when passed to a class, out of the 3 menubar. toolbar and statusbar, only menubar is a passed, accessible attributes.
Therefore I can amend menubar but not natively access toolbar and statusbar.- PySide6.QtWidgets.QMainWindow.menuBar()
- PySide6.QtWidgets.QMainWindow.statusBar()
- If you want to find toolbars
mainWindow.findChildren(QToolBar)
should do it.
So you can access all 3. I do not know what "is a passed, accessible attributes" means.
Don't know why you'd want to do this, doesn't seem a good idea to me. Maybe @SGaist's comment helps.
-
Hi,
Here goes
I use a fairly standard main.py to call mainwindow.py.
From that I want to be able to call various classes, each class to handle an activity.- It will define a series of actions
- It will populate the menubar via the actions
- It will populate a toolbar via the actions
- It will display the appropriate messages on the status bar
- It will handle its own singles and slots.
So I have main.py.
#!/usr/bin/env python3 # coding: utf-8 import sys import traceback from PySide6.QtWidgets import ( QApplication, QMainWindow ) from mainwindow import MainWindow def main(): try: app = QApplication(sys.argv) window = MainWindow(app) window.show() except Exception as err: print("Unfortunately Class Test has encountered an error \ and is unable to continue.") print(f"Exception {err=}, {type(err)=}") traceback.print_exc() traceback.print_exception() finally: sys.exit(app.exec()) if __name__ == '__main__': main()
from PySide6.QtWidgets import ( QMainWindow ) from ui_mainwindow import Ui_MainWindow from classes import firstclass class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, app) -> None: super().__init__() self.setupUi(self) self.app = app self.setWindowTitle("Class Test - QMainWindow") # print(dir(self)) # print("\n\n MainWindow Class") # print(self.parent) # print(self.objectName) firstclass.FirstClass(WhatGoesHere)
from PySide6.QtCore import ( QSize, QObject ) from PySide6.QtGui import ( QAction, QIcon ) from PySide6.QtWidgets import ( QMainWindow, QToolBar, QPushButton, QStatusBar, QWidget ) class FirstClass(WhatGoesHere): def __init__(self, parent) -> None: super().__init__() self.parent = parent print("\n\n FirstClass Class") print(dir(self)) print(self.parent) print(self.objectName) self.setWindowTitle("Class Test - First Class") # Does Nothing ???
My question is what do I pass in the call to FirstClass(???) that allows me access to the MainWindow defined in mainwindow.py
.
Thank you to anyone who solves this riddle for me.
Kind regards,
jB -
Hi,
Here goes
I use a fairly standard main.py to call mainwindow.py.
From that I want to be able to call various classes, each class to handle an activity.- It will define a series of actions
- It will populate the menubar via the actions
- It will populate a toolbar via the actions
- It will display the appropriate messages on the status bar
- It will handle its own singles and slots.
So I have main.py.
#!/usr/bin/env python3 # coding: utf-8 import sys import traceback from PySide6.QtWidgets import ( QApplication, QMainWindow ) from mainwindow import MainWindow def main(): try: app = QApplication(sys.argv) window = MainWindow(app) window.show() except Exception as err: print("Unfortunately Class Test has encountered an error \ and is unable to continue.") print(f"Exception {err=}, {type(err)=}") traceback.print_exc() traceback.print_exception() finally: sys.exit(app.exec()) if __name__ == '__main__': main()
from PySide6.QtWidgets import ( QMainWindow ) from ui_mainwindow import Ui_MainWindow from classes import firstclass class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, app) -> None: super().__init__() self.setupUi(self) self.app = app self.setWindowTitle("Class Test - QMainWindow") # print(dir(self)) # print("\n\n MainWindow Class") # print(self.parent) # print(self.objectName) firstclass.FirstClass(WhatGoesHere)
from PySide6.QtCore import ( QSize, QObject ) from PySide6.QtGui import ( QAction, QIcon ) from PySide6.QtWidgets import ( QMainWindow, QToolBar, QPushButton, QStatusBar, QWidget ) class FirstClass(WhatGoesHere): def __init__(self, parent) -> None: super().__init__() self.parent = parent print("\n\n FirstClass Class") print(dir(self)) print(self.parent) print(self.objectName) self.setWindowTitle("Class Test - First Class") # Does Nothing ???
My question is what do I pass in the call to FirstClass(???) that allows me access to the MainWindow defined in mainwindow.py
.
Thank you to anyone who solves this riddle for me.
Kind regards,
jB@britesc
I'm afraid your ideas about classes and instances are all over the place.firstclass.FirstClass(WhatGoesHere) ... class FirstClass(WhatGoesHere):
If you think those two occurrences of
WhatGoesHere
are connected they are not, they have nothing to do with each other.-
class FirstClass(WhatGoesHere):
:WhatGoeshere
is a class which the new classFirstClass
is derived from. -
firstclass.FirstClass(WhatGoesHere)
:WhatGoesHere
must be an instance/variable, of whatever class. It arrives as a parameter toFirstClass.__init__()
-
We know nothing about
firstclass
. You havefrom classes import firstclass
, but we don't know what that might be. Whatever it is it can't be right to try to callfirstclass.FirstClass()
.
Sorry, but you really need to review Python classes & instances to get anywhere. From what you have you might want something like:
self.firstclass = FirstClass(self) ... class FirstClass(): def __init__(self, mainWindow: QMainWindow) -> None: print(mainWindow.menuBar()) print(mainWindow.statusBar()) mainWindow.setWindowTitle("Class Test - First Class") # Sets the main window's title, if that's what you're trying to do
But the whole concept is wrong. You shouldn't need to pass you
QMainWindow
instance to anything else. I don't know what yourFirstClass
class is supposed to be about, but it doesn't look like a good idea. Indeed yourI have a PySide6 window that I would like to update the Menu bar, Toolbar, Statusbar, Actions for, from class files.
doesn't sound like the right approach. If you have code not inside your
class MainWindow
which would like the main window to update, say, its status bar the Qt way is to have that external code emit signals with suitable parameters and yourMainWindow
class places slots on those signals where it uses the parameters to update its own status bar. -
Thank you very much for your patience and assistance.
Due to my near blindness I am severely limited to the time I can spend online and reading etc.
I agree I need much more knowledge about Python OOP and will endeavor to attain that.
Your help has allowed me to gently continue with my labour of love, be it good or bad coidin at this stage.
I just need to develop my project before my brain explodes. After that I will refactor for version 2.
Many thanks and kind regards,
jB -
Thank you very much for your patience and assistance.
Due to my near blindness I am severely limited to the time I can spend online and reading etc.
I agree I need much more knowledge about Python OOP and will endeavor to attain that.
Your help has allowed me to gently continue with my labour of love, be it good or bad coidin at this stage.
I just need to develop my project before my brain explodes. After that I will refactor for version 2.
Many thanks and kind regards,
jB@britesc
Quite understand. Maybe there are some youtube videos or spoken books which would allow to learn with overstressing sight.Read up on classes and instances, you will need a thorough understanding for Python/OOP no matter what you do. Remember usage works like:
class SomeClass(optionalClassItDerivesFrom): # class definition def __init__(self, optionalAnyParameters): # class constructor, called first whenever an instance is created ... # outside world classInstanceVariable = SomeClass() # create an instance of the class
A class is like
Animal
orLion
--- it defines attributes and behaviours for animals/lions, but it does not create any animal or lion.lion1 = Lion()
creates an instance of aLion()
, i.e. some individual lion withLion
attributes.