Results 1 to 2 of 2

Thread: Setting a Widget inside the MainWindow

  1. #1
    Join Date
    Aug 2017
    Location
    USA
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Setting a Widget inside the MainWindow

    Hello,

    I am currently using PyQt5 with Python3.
    I'm having trouble getting a QTabWidget to be placed inside the main screen when called from a separate class.
    Currently when the button "single" from the view menu option is selected a QTabWidget is produced but in a seperate window from my QMainWindow widget.
    I believe the issue lies in my OOP but I was hoping for some input or direction to documentation that could provide me with a solution.

    The structure is as follows:

    __main__.py

    Qt Code:
    1. mainscreen.App()
    To copy to clipboard, switch view to plain text mode 

    mainwindow.py

    Qt Code:
    1. class App(QMainWindow):
    2. def __init__(self):
    3. appl = QApplication(sys.argv)
    4.  
    5. super().__init__() #is this line necessary
    6. self.title = "PyQt EVE-Application"
    7. self.showMaximized()
    8. self.initUI()
    9.  
    10. sys.exit(appl.exec_())
    11.  
    12. def menu(self):
    13. mainMenu = self.menuBar()
    14. fileMenu = mainMenu.addMenu('File')
    15. openMenu = mainMenu.addMenu('Open')
    16. viewMenu = mainMenu.addMenu('View')
    17. helpMenu = mainMenu.addMenu('Help')
    18.  
    19. #FILE
    20. exitButton = QAction('Exit', self)
    21. exitButton.setShortcut('Ctrl + E')
    22. exitButton.setStatusTip('Exit application')
    23. exitButton.triggered.connect(self.close)
    24. fileMenu.addAction(exitButton)
    25.  
    26. #OPEN
    27.  
    28. #VIEW
    29. singleview = QAction('Single View', self)
    30. singleview.setShortcut ('Ctrl + S')
    31. singleview.setStatusTip('Change view to one widget.')
    32. singleview.triggered.connect(self.singlecall)
    33. viewMenu.addAction(singleview)
    34.  
    35.  
    36. # doubleview
    37. doubleview = QAction('Double View', self)
    38. doubleview.setShortcut('Ctrl + D')
    39. doubleview.setStatusTip('Change view to two wigets.')
    40. doubleview.triggered.connect(widgetviews.double)
    41. viewMenu.addAction(doubleview)
    42.  
    43.  
    44. # tripleview
    45. tripleview = QAction('Triple View', self)
    46. tripleview.setShortcut('Ctrl + T')
    47. tripleview.setStatusTip('Change view to three widgets.')
    48. tripleview.triggered.connect(widgetviews.triple)
    49. viewMenu.addAction(tripleview)
    50.  
    51. # quadview
    52. quadview = QAction('Quad View', self)
    53. quadview.setShortcut('Ctrl + Q')
    54. quadview.setStatusTip('Change view to four widgets.')
    55. quadview.triggered.connect(widgetviews.quad)
    56. viewMenu.addAction(quadview)
    57. #HELP
    58. def singlecall(self):
    59. self.t = widgetviews.single(self)
    60. self.t.show()
    61.  
    62. def initUI(self):
    63. self.setWindowTitle(self.title)
    64. self.menu()
    65. #default to quad view
    66. self.show()
    67. if __name__ == "__main__":
    68. pass
    To copy to clipboard, switch view to plain text mode 

    tabwidget.py

    Qt Code:
    1. class single(QWidget):
    2. def __init__(self, App):
    3. super().__init__()
    4.  
    5. App.blankwidget = QWidget()
    6. App.tab1 = QTabWidget()
    7. App.tab1.addTab(App.blankwidget, "Tab 1")
    8. App.tab2 = QTabWidget()
    9. App.tab3 = QTabWidget()
    10. App.tab4 = QTabWidget()
    11.  
    12. grid = QGridLayout()
    13. grid.addWidget(App.tab1,0,0)
    14. grid.addWidget(App.tab2,0,1)
    15. grid.addWidget(App.tab3,1,0)
    16. grid.addWidget(App.tab4,1,1)
    17. self.setLayout(grid)
    To copy to clipboard, switch view to plain text mode 

    Thank you in Advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Setting a Widget inside the MainWindow

    Fair disclosure: I am not a Python guy.
    QTabWidget is produced but in a seperate window from my QMainWindow widget.
    The simple, short answer is that you have to parent your QTabWidget to the MainWindow if you want it to be a child of your MainWindow.
    That said, your code is very poorly encapsulated.
    From what I can tell from your code, it seems you are allocating a QApplication in your QMainWindow.
    Why should a UI class be responsivle for the application object creation?
    Can't you have that in your main?

    Why should class single be allocating stuff in another class?
    That is looking for trouble.
    Let the MainWindows create its own children.
    You can have single signal the MainWindow when to do it, but leave that task to the MainWindow.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Resize widget inside mainwindow (during runtime)
    By alenn.masic in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2012, 21:50
  2. Setting areas of MainWindow that are click responsive
    By yonibr in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2012, 15:27
  3. Replies: 4
    Last Post: 10th February 2012, 08:25
  4. Replies: 0
    Last Post: 6th November 2011, 09:22
  5. QDialog problem when setting mainwindow as parent
    By superpacko in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2011, 09:35

Tags for this Thread

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.