aTei
aTei

Reputation: 1844

PyQt show one widget by clicking button on another

I'm new in PyQt and using PyQt4. Have two independent widgets. First of them showFullScreen() and second show(). I want after hiding second by hide() show it by clicking button on first. Tried something and googled - nothing. Full code:

from PyQt4 import QtCore, QtGui


class FileExplorer(QtGui.QWidget):
    def __init__(self, parent=None):
        super(FileExplorer, self).__init__(parent)

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        showButton = QtGui.QPushButton('Show widget', self)
        showButton.clicked.connect(FileExplor.show)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(showButton, 3, 1)

        self.setLayout(mainLayout)
#               self.setGeometry(300, 300, 250, 150)
#        self.sizeHint()
        self.setWindowTitle("File Explorer")




class FileExplor(QtGui.QWidget):
    def __init__(self, parent=None):
        super(FileExplor, self).__init__(parent)

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        quitButton = QtGui.QPushButton('Quit', self)
        quitButton.clicked.connect(self.hide)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(quitButton, 3, 1)

        self.setLayout(mainLayout)
        #self.setGeometry(300, 300, 250, 150)
        self.sizeHint()
        self.setWindowTitle("File Explorer")


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    fileExplorer = FileExplorer()
    fileExplorer.showFullScreen()
#    fileExplorer.show()
#
    fileExplor = FileExplor()
    fileExplor.show()

    sys.exit(app.exec_())

Logic what i want make in the end:

Upvotes: 1

Views: 12981

Answers (2)

ekhumoro
ekhumoro

Reputation: 120608

It sounds like what you want is a modeless dialog.

In the code you posted, change the FileExplor class to a QDialog:

class FileExplor(QtGui.QDialog):

Then add a signal handler to the main FileExplorer class:

def handleShowDialog(self):
    if not hasattr(self, 'dialog'):
        self.dialog = FileExplor(self)
    self.dialog.show()

And finally connect the button to the handler:

showButton.clicked.connect(self.handleShowDialog)

Upvotes: 2

aayoubi
aayoubi

Reputation: 12069

I don't have PyQt4 installed on this machine so i can't test this. But here is your problem:

showButton.clicked.connect(FileExplor.show)

You are not referencing the widget object created below, you are referencing the class object FileExplor.

    fileExplorer = FileExplorer()
    fileExplorer.showFullScreen()

    fileExplor = FileExplor()
    fileExplor.show()

Can you try making FileExplor as a argument of FileExplorer? Also, try naming FileExplor something else, like DependentFileExplorer (learn about naming conventions here) and do this:

from PyQt4 import QtCore, QtGui


class FileExplorer(QtGui.QWidget):
    def __init__(self, dependent, parent=None):
        super(FileExplorer, self).__init__(parent)
        self.dependent = dependent

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        showButton = QtGui.QPushButton('Show widget', self)
        showButton.clicked.connect(self.dependent.show)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(showButton, 3, 1)

        self.setLayout(mainLayout)
        self.setWindowTitle("File Explorer")




class DependentFileExplorer(QtGui.QWidget):
    def __init__(self, parent=None):
        super(DependentFileExplorer, self).__init__(parent)

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        quitButton = QtGui.QPushButton('Quit', self)
        quitButton.clicked.connect(self.hide)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(quitButton, 3, 1)

        self.setLayout(mainLayout)
        #self.setGeometry(300, 300, 250, 150)
        self.sizeHint()
        self.setWindowTitle("File Explorer")


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    dependent = DependentFileExplorer()
    fileExplorer = FileExplorer(dependent)

    fileExplorer.showFullScreen()
    dependent.show()

    sys.exit(app.exec_())

Now FileExplorer takes DependentFileExplorer as an argument.
You must create the DependentFileExplorer before FileExplorer.

Upvotes: 0

Related Questions