Created
February 9, 2023 20:44
-
-
Save kporangehat/ca19db0d168421597d9b2b5c2b9deb81 to your computer and use it in GitHub Desktop.
SGTK App with QScrollArea tabs example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2013 Shotgun Software Inc. | |
# | |
# CONFIDENTIAL AND PROPRIETARY | |
# | |
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit | |
# Source Code License included in this distribution package. See LICENSE. | |
# By accessing, using, copying or modifying this work you indicate your | |
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights | |
# not expressly granted therein are reserved by Shotgun Software Inc. | |
import sgtk | |
import os | |
import sys | |
import threading | |
# by importing QT from sgtk rather than directly, we ensure that | |
# the code will be compatible with both PySide and PyQt. | |
from sgtk.platform.qt import QtCore, QtGui | |
from .ui.dialog import Ui_Dialog | |
# standard toolkit logger | |
logger = sgtk.platform.get_logger(__name__) | |
def show_dialog(app_instance): | |
""" | |
Shows the main dialog window. | |
""" | |
# in order to handle UIs seamlessly, each toolkit engine has methods for launching | |
# different types of windows. By using these methods, your windows will be correctly | |
# decorated and handled in a consistent fashion by the system. | |
# we pass the dialog class to this method and leave the actual construction | |
# to be carried out by toolkit. | |
app_instance.engine.show_dialog("Starter Template App...", app_instance, AppDialog) | |
class AppDialog(QtGui.QWidget): | |
""" | |
Main application dialog window | |
""" | |
def __init__(self): | |
""" | |
Constructor | |
""" | |
# first, call the base class and let it do its thing. | |
QtGui.QWidget.__init__(self) | |
self.layout = QtGui.QVBoxLayout(self) | |
# ---------------------------------------------------------------------- | |
# create the content for tab 1 and set the scrolling area on it | |
# - Qwidget (with the scroll_tab1 set as scrollArea) | |
# - Vertical layout for the list content | |
# - QLabel widget 1 | |
# - QLabel widget 2 | |
# - QLabel widget 3 | |
# ---------------------------------------------------------------------- | |
# create a scrolling area | |
self.scroll_tab1 = QtGui.QScrollArea() | |
# set the policy for when scrollbars are visible | |
self.scroll_tab1.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) | |
self.scroll_tab1.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) | |
# create a widget to hold the contents of tab 1 | |
self.tab1 = QtGui.QWidget() | |
# apply the scrollArea to the contents of tab 1 | |
self.scroll_tab1.setWidget(self.tab1) | |
# required as our contents won't display otherwise | |
# and will be painted in a single pixel line that isn'e visible. | |
# setting this to True, allows the widget to expand for the content. | |
self.scroll_tab1.setWidgetResizable(True) | |
# create a vertical layout to hold our tab content widgets | |
self.tab1_layout = QtGui.QVBoxLayout(self.tab1) | |
# Add a bunch of QLabels to our tab layout | |
for i in range(1, 50): | |
object = QtGui.QLabel("Tab1 %d" % i) | |
self.tab1_layout.addWidget(object) | |
# duplicated for Tab 2 | |
# ---------------------------------------------------------------------- | |
# create the content for tab 2 and set the scroll on it | |
# - Qwidget (with the scroll_tab2 set as scrollArea) | |
# - Vertical layout for the list content | |
# - QLabel widget 1 | |
# - QLabel widget 2 | |
# - QLabel widget 3 | |
# ---------------------------------------------------------------------- | |
self.scroll_tab2 = QtGui.QScrollArea() | |
self.scroll_tab2.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) | |
self.scroll_tab2.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) | |
self.tab2 = QtGui.QWidget() | |
self.scroll_tab2.setWidget(self.tab2) | |
self.scroll_tab2.setWidgetResizable(True) | |
self.tab2_layout = QtGui.QVBoxLayout(self.tab2) | |
for i in range(1, 50): | |
object = QtGui.QLabel("Tab2 %d" % i) | |
self.tab2_layout.addWidget(object) | |
# We have the content for both of our tabs now, but don't have a tab | |
# widget yet. So we create the it here. | |
self.tabwidget = QtGui.QTabWidget() | |
# add the content to a new tab. | |
# NOTE: We're adding the scroll widgets self.scroll_tab1 and self.scroll_tab2, | |
# NOT the original content widgets self.tab1 and self.tab2! If we did that, | |
# the scroll would be lost. | |
self.tabwidget.addTab(self.scroll_tab1, "Tab 1") | |
self.tabwidget.addTab(self.scroll_tab2, "Tab 2") | |
# Now finally we can add our tab widget to the main layout | |
self.layout.addWidget(self.tabwidget) | |
# And we set the parent widget's layout to our main layout. | |
self.setLayout(self.layout) | |
# ---------------------------------------------------------------------- | |
# NOTE: Some items are commented out since this example is manually | |
# building the UI instead of relying on a UI file generated by Designer. | |
# ---------------------------------------------------------------------- | |
# now load in the UI that was created in the UI designer | |
# self.ui = Ui_Dialog() | |
# self.ui.setupUi(self) | |
# most of the useful accessors are available through the Application class instance | |
# it is often handy to keep a reference to this. You can get it via the following method: | |
self._app = sgtk.platform.current_bundle() | |
# logging happens via a standard toolkit logger | |
logger.info("Launching Starter Application...") | |
# via the self._app handle we can for example access: | |
# - The engine, via self._app.engine | |
# - A Shotgun API instance, via self._app.shotgun | |
# - An Sgtk API instance, via self._app.sgtk | |
# lastly, set up our very basic UI | |
# self.ui.context.setText("Current Context: %s" % self._app.context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment