Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:44:30+00:00 2026-06-14T21:44:30+00:00

I’m kinda new to PySide.I have a main window object which shows one widget

  • 0

I’m kinda new to PySide.I have a main window object which shows one widget at a time. I’ve been trying to change the central widget of the QMainWindow class in order to replace the visible Widget in the window when pressing a button. The problem is that the button pressed is in the Widget class, not in the main window class.

say…

class App(QtGui.QMainWindow):

    def __init__(self):
        super(App, self).__init__()

        self.initUI()

    def initUI(self):

        self.statusBar().showMessage('Listo.') #Status Bar
        self.login_screen = LoginScreen()
        self.logged_in_screen = LoggedInScreen()

        self.setCentralWidget(self.login_screen)

        self.setGeometry(300, 300, 450, 600) #Window Size
        self.setWindowTitle('PyTransactio - Client') #Window Title
        self.setWindowIcon(QtGui.QIcon('icon.png')) #App Icon
        self.show()

The pressed button is in the login_screen instance. The method called when the button is clicked is inside the LoginScreen class:

def login(self):
        """ Send login data to the server in order to log in """

        #Process

        self.setParent(None)

Setting the parent widget to None removes the widget (login_screen) from the main window. What should I do in order to get another widget (e.g. logged_in_screen) as the central widget of the main window when the loginButton (inside the login_screen widget) is pressed?

Maybe the login method should be inside the main window class? If so, how can I connect the buttons pressed in login_screen with the main window’s method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:44:31+00:00Added an answer on June 14, 2026 at 9:44 pm

    You may use a QStackedWidget as central widget and add both the log-in screen and “logged-in” screen to it.

    An example usage:

    from PyQt4 import QtCore, QtGui
    
    
    class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.central_widget = QtGui.QStackedWidget()
            self.setCentralWidget(self.central_widget)
            login_widget = LoginWidget(self)
            login_widget.button.clicked.connect(self.login)
            self.central_widget.addWidget(login_widget)
        def login(self):
            logged_in_widget = LoggedWidget(self)
            self.central_widget.addWidget(logged_in_widget)
            self.central_widget.setCurrentWidget(logged_in_widget)
    
    
    class LoginWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(LoginWidget, self).__init__(parent)
            layout = QtGui.QHBoxLayout()
            self.button = QtGui.QPushButton('Login')
            layout.addWidget(self.button)
            self.setLayout(layout)
            # you might want to do self.button.click.connect(self.parent().login) here
    
    
    class LoggedWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(LoggedWidget, self).__init__(parent)
            layout = QtGui.QHBoxLayout()
            self.label = QtGui.QLabel('logged in!')
            layout.addWidget(self.label)
            self.setLayout(layout)
    
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication([])
        window = MainWindow()
        window.show()
        app.exec_()
    

    If you do not want to use this widget, then I think you’ll have to call QMainWindow.setCentralWidget every time you change the central widget.

    As to where the login method should be, it depends. Probably you could define a simple interface for your mainwindow to add/remove/show specific central widgets, and call it from the login method of LoginScreen. In this way the LoginScreen class does not have to know about implementation details such as if the central widget is actually a QStackedWidget or this thing is done in an other way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.