Search code examples
pythonpyqtpyqt5qpixmap

I want to set path_to_photo in QPixmap, when i press the button in another QWidget


I am developping an application with PyQt based on a QWidget. I have 2 QWidget classes. When i press "Choose" button from ToolWindow class i run script and get path_to_photo, also i switch to other QWidget class. I want to set a new path to QPixmap, but i only get it when i back to main and return to this window.

class UIWindow(QWidget):
    def __init__(self, parent=None):
        super(UIWindow, self).__init__(parent)
        self.vbox = QVBoxLayout(self)
        global path_to_photo
        self.ToolsBTN = QPushButton('text', self)
        self.label = QLabel(self)
        self.pixmap = QPixmap(path_to_photo)
        self.label.setPixmap(self.pixmap)
        self.resize(self.pixmap.width(),self.pixmap.height())
        self.vbox.addWidget(self.ToolsBTN)
        self.vbox.addWidget(self.label)
        self.vbox.addStretch(5)
        self.setLayout(self.vbox)

This is mine UIWindow with photo that i want to show

class UIToolTab(QWidget):
    def __init__(self, parent=None):
        super(UIToolTab, self).__init__(parent)
        self.vbox = QVBoxLayout(self)
        self.CPSBTN = QPushButton("Choose", self)
        self.vbox.addWidget(self.CPSBTN)
        self.vbox.addStretch(5)
        self.setLayout(self.vbox)

This is mine UIToolTab with "Choose" button

class MainWindow(QMainWindow):
...
   def startUIToolTab(self):
        self.ToolTab = UIToolTab(self)
        self.Window = UIWindow(self)
        self.setWindowTitle("UIToolTab")
        self.setCentralWidget(self.ToolTab)
        self.ToolTab.CPSBTN.clicked.connect(self.startUIWindow)
        self.show()

   def startUIWindow(self):
        global path_to_photo
        self.Window = UIWindow(self)
        file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
        print(file)
        path = Path(file)
        glob_path = path.glob('*')
        path_to_photo = brisquequality.answer(glob_path)
        self.setWindowTitle("UIWindow")
        self.setCentralWidget(self.Window)
        self.Window.ToolsBTN.clicked.connect(self.startUIToolTab)
        self.show()

Switch functions

How to get QPixmap updated, when i rewrite path_to_photo?


Solution

  • The solution takes into account the following:

    • Do not abuse the global variables, use them when necessary and in this case it is not.
    • If you want to show several widgets in the same space then use a QStackedWidget.
    • Each class must be independent so it must offer methods to update the information, in this case it implements a method that allows updating the pixmap shown by the QLabel.
    class UIWindow(QWidget):
        def __init__(self, parent=None):
            super(UIWindow, self).__init__(parent)
            self.tools_btn = QPushButton("text", self)
            self.label = QLabel(self)
    
            vbox = QVBoxLayout(self)
            vbox.addWidget(self.tools_btn)
            vbox.addWidget(self.label)
            vbox.addStretch()
    
        def set_photo(self, photo):
            pixmap = QPixmap(photo)
            self.label.setPixmap(pixmap)
    
    class UIToolTab(QWidget):
        def __init__(self, parent=None):
            super(UIToolTab, self).__init__(parent)
    
            self.cps_btn = QPushButton("Choose", self)
    
            vbox = QVBoxLayout(self)
            vbox.addWidget(self.cps_btn)
            vbox.addStretch()
    
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            self.stacked_widget = QStackedWidget()
            self.setCentralWidget(self.stacked_widget)
    
            self.ui_tool = UIToolTab()
            self.ui_window = UIWindow()
    
            self.stacked_widget.addWidget(self.ui_tool)
            self.stacked_widget.addWidget(self.ui_window)
    
            self.ui_tool.cps_btn.clicked.connect(self.on_choose_clicked)
            self.ui_window.tools_btn.clicked.connect(self.open_tools_window)
    
        @pyqtSlot()
        def on_choose_clicked(self):
            dirname = QFileDialog.getExistingDirectory(self, "Select Directory")
            if dirname:
                path = Path(dirname)
                glob_path = path.glob("*")
                path_to_photo = brisquequality.answer(glob_path)
                print(path_to_photo)
                self.ui_window.set_photo(path_to_photo)
                self.stacked_widget.setCurrentIndex(1)
    
        @pyqtSlot()
        def open_tools_window(self):
            self.stacked_widget.setCurrentIndex(0)