Results 1 to 2 of 2

Thread: Switching windows

  1. #1
    Join Date
    Jan 2016
    Posts
    3
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Switching windows

    Im making an app that has 2 menus, the first one is a "Welcome" menu, and the second one is for searching something. The problem is that i cant make the searching window show itself. It sticks in the welcome menu. When i started programming the tools for the searching window i kept them in the "Welcome" menu and worked fine until i tried to put those tools in an other menu(ergo different function); So i guess the problem is not that im not calling the self.show() because they worked fine before...

    Here's the details of the app and what im doing. Gonna delete the non important parts.
    Its kinda complex:

    First i have this:

    Qt Code:
    1. class Window(QtGui.QMainWindow):
    2. def __init__(self):
    3. super(Window, self).__init__()
    4.  
    5. self.buttons()
    6. self.toolButtons()
    7. self.searchMenu()
    To copy to clipboard, switch view to plain text mode 

    There there is the buttons() that creates all the interface of the "Welcome" menu, which was the first one.
    toolButtons() explains itself. Im using one of the toolbuttons to switch between menues.
    searchMenu() is the second Menu, which is supposed to show itself when you press one of the toolbuttons

    A little bit later i have the important functions among others:

    Qt Code:
    1. def buttons(self):
    2. # Welcome menu. Images and labels.
    3. self.show()
    4.  
    5. def toolButtons(self):
    6. # Toolbuttons. Here is the button that is supposed to load the search menu, and it does it with these lines:
    7. searchButton = QtGui.QAction(QtGui.QIcon('searchicon.png'), 'Search', self)
    8. searchButton.triggered.connect(self.loadingSearchWindow) #Appears down
    9. self.toolBar.addAction(searchButton)
    10.  
    11. def searchMenu(self):
    12. # lot of comboboxes, images, etc. Loads everything of the search menu.
    13. self.show()
    To copy to clipboard, switch view to plain text mode 

    ____________________

    Now here is the "loadingSearchWindow" that appears up in the searchButton:

    Qt Code:
    1. def loadingSearchWindow(self):
    2. search = self.searchMenu()
    3. self.setCentralWidget(search)
    To copy to clipboard, switch view to plain text mode 

    So when you click the searchButton, it loads "loadingSearchWindow" which sets the searchMenu() as the CentralWidget.

    And again, nothing happends when i click the toolButton that is supposed to load the search menu. And i dont know why, because im doing the same thing with the "buttons" function that loads the welcome menu without problems, but then it seems that if you want to add an other menu it doesn't work.

    What am I doing wrong?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Switching windows

    You seem to have a basic misunderstanding of the role of the "central widget" in a QMainWindow. All of your posts so far have asked various forms of the same questions, and you seem to persist in making the same mistake over and over.

    In most cases, you do not want to keep flipping the central widget back and forth between different other widgets. The better way to accomplish this is to make the central widget a single instance of QStackedWidget. You make the child widgets that should be changed dynamically children of the stacked widget by instantiating them and then calling QStackedWidget::addWidget(). To make one child visible while hiding the others, you call the QStackedWidget::setCurrentIndex() method.

    You do not need to create new widgets dynamically in response to button clicks or other actions. You make all of the widgets you need at startup (in __init()__) and add them to the stack. You use the button or action handling slots to make the appropriate child widget current (i.e show it and hide the others).

Similar Threads

  1. Switching between two windows
    By Prasad_ in forum Newbie
    Replies: 1
    Last Post: 15th May 2013, 09:29
  2. Qt Creator Switching between .ui and .h/.cpp (and help)
    By CoderMan in forum Qt Tools
    Replies: 3
    Last Post: 15th August 2011, 08:05
  3. Replies: 2
    Last Post: 15th December 2010, 11:53
  4. Switching between two databases
    By codeman in forum Qt Programming
    Replies: 20
    Last Post: 13th May 2009, 15:33
  5. Switching 5.2 from 5.0.2
    By baray98 in forum Qwt
    Replies: 1
    Last Post: 11th May 2009, 18:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.