in PySide6 I am using this module def maximize_on_screen(self):
screen = QGuiApplication.primaryScreen()
screen_geometry = screen.availableGeometry() # Excludes taskbar
self.setGeometry(screen_geometry). This is the main program which calling number of child programs. Can I store the screen geometry as a global variable so that it is accessible for all child form. The logic is - for the current session by a user the screen geometry is not going to change and the same variable can be used as reference for plotting other child programs
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Yes, you can store the screen geometry as a global variable to make it accessible across all child forms in your PySide6 application. Since the screen geometry is unlikely to change during a single user session, this approach is efficient and avoids redundant calculations.
Here’s how you can implement this:
Define a global variable to store the screen geometry.
Initialize the global variable in your main program.
Access the global variable in child forms as needed.
Here’s an example implementation:
python
from PySide6.QtGui import QGuiApplication
from PySide6.QtCore import QRect
# Global variable to store screen geometry
SCREEN_GEOMETRY = None
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initialize_screen_geometry()
self.maximize_on_screen()
def initialize_screen_geometry(self):
global SCREEN_GEOMETRY
screen = QGuiApplication.primaryScreen()
SCREEN_GEOMETRY = screen.availableGeometry() # Store as global variable
def maximize_on_screen(self):
global SCREEN_GEOMETRY
self.setGeometry(SCREEN_GEOMETRY) # Use the stored geometry
class ChildForm(QWidget):
def __init__(self):
super().__init__()
self.use_screen_geometry()
def use_screen_geometry(self):
global SCREEN_GEOMETRY
# Example usage: set the child form's geometry based on the screen
self.setGeometry(SCREEN_GEOMETRY.x(), SCREEN_GEOMETRY.y(),
SCREEN_GEOMETRY.width() // 2, SCREEN_GEOMETRY.height() // 2)
if __name__ == "__main__":
app = QApplication([])
main_window = MainWindow()
main_window.show()
# Example: Opening a child form
child_form = ChildForm()
child_form.show()
app.exec()
Explanation:
Global Variable (SCREEN_GEOMETRY): This variable is defined outside any class or function, making it accessible globally.
Initialization: In the MainWindow's __init__ method, the initialize_screen_geometry method is called to fetch and store the screen geometry in the global variable.
Usage in Child Forms: The ChildForm class accesses the global SCREEN_GEOMETRY variable to use the screen dimensions as needed.
Notes:
Thread Safety: If your application is multi-threaded, ensure proper synchronization when accessing or modifying the global variable.
Immutability: Since QRect is immutable, you don’t need to worry about accidental modifications to the stored geometry.
Session Changes: If the screen geometry could change (e.g., due to docking/undocking a laptop), you might want to recompute it dynamically or listen for screen change events using QScreen.geometryChanged.
This approach ensures that the screen geometry is computed once and reused across all child forms, improving efficiency and maintaining consistency.
User
I have used the assistant A answer as below - def tbl_requ_header(self):
column_defs = [
("requ_hdr_id", "Requ Hdr ID"),
("requ_dt", "Dated"),
("from_store_id", "Origin"),
("requ_hdr_status", "Status"),
("requ_by", "By")
]
self.requ_hdr_browser = TableBrowser(
self.rspl_conn,
"requ_header",
column_defs,
#on_edit_callback=self.open_requ_hdr_editor,
parent=self
)
self.requ_hdr_browser.setGeometry(SCREEN_GEOMETRY.x(300),SCREEN_GEOMETRY.y(300),SCREEN_GEOMETRY.width(800),SCREEN_GEOMETRY.height(800))
self.requ_hdr_browser.show()
code
def close_form(self):
self.rspl_conn.disconnect()
self.close(). However the doesnot show up at all
Assistant
The issue with your code is likely due to the incorrect usage of SCREEN_GEOMETRY when setting the geometry of the TableBrowser widget. You're calling SCREEN_GEOMETRY.x(300), SCREEN_GEOMETRY.y(300), etc., which is not the correct way to use QRect attributes.
Instead, you should directly access the x, y, width, and height attributes of the SCREEN_GEOMETRY rect, and then adjust them as needed.
Here’s the corrected version of your code:
python
def tbl_requ_header(self):
column_defs = [
("requ_hdr_id", "Requ Hdr ID"),
("requ_dt", "Dated"),
("from_store_id", "Origin"),
("requ_hdr_status", "Status"),
("requ_by", "By")
]
self.requ_hdr_browser = TableBrowser(
self.rspl_conn,
"requ_header",
column_defs,
#on_edit_callback=self.open_requ_hdr_editor,
parent=self
)
# Correctly use SCREEN_GEOMETRY to set the widget's position and size
x = SCREEN_GEOMETRY.x() + 300 # Adjust x position by 300 pixels
y = SCREEN_GEOMETRY.y() + 300 # Adjust y position by 300 pixels
width = min(800, SCREEN_GEOMETRY.width() - x) # Ensure width doesn't exceed screen bounds
height = min(800, SCREEN_GEOMETRY.height() - y) # Ensure height doesn't exceed screen bounds
self.requ_hdr_browser.setGeometry(x, y, width, height)
self.requ_hdr_browser.show()
def close_form(self):
self.rspl_conn.disconnect()
self.close()
Key Fixes:
Correct Geometry Calculation:
x and y are calculated by adding an offset (300 pixels) to the screen's top-left corner.
width and height are set to 800 but are constrained to not exceed the available screen space.
Avoid Method Calls on Attributes:
Directly use SCREEN_GEOMETRY.x, SCREEN_GEOMETRY.y, SCREEN_GEOMETRY.width(), and SCREEN_GEOMETRY.height() instead of calling them as methods.
Additional Checks:
Widget Visibility: Ensure TableBrowser is properly initialized and doesn't have any hidden or invisible parent widgets.
Screen Geometry Validity: Verify that SCREEN_GEOMETRY is correctly initialized and contains valid dimensions.
Error Handling: Add debugging prints or checks to ensure the widget's geometry is set as expected:
python
print(f"Setting geometry to: x={x}, y={y}, width={width}, height={height}")
If the widget still doesn't show up, check for any overlapping widgets or layout issues in your main window. Ensure the TableBrowser widget is not being obscured by other elements.