Results 1 to 6 of 6

Thread: Working with widgets created bt Qt Designer

  1. #1
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Working with widgets created bt Qt Designer

    I'm trying to break away from having all my widgets and my main window in one user interface file. So in the example below I've created a frame that I want to show up in the main window. Later on I will show and hide it along with other frames etc. It pops up but in its own window and only briefly. What am I doing wrong? How do I get the QFrame UI file I created in Qt Designer to behave like what I would expect? That is to say a frame and not frame inside a window? I've excluded the boilerplate code that pyuic4 generates.

    myApp.py:

    Qt Code:
    1. import sys
    2. from PyQt4 import QtCore, QtGui
    3. from main_window import Ui_MainWindow
    4. from frame2 import Frame
    5.  
    6. class StartQt4(QtGui.QMainWindow, Ui_MainWindow):
    7. def __init__(self):
    8. super(self.__class__, self).__init__()
    9. self.setupUi(self)
    10. myFrame = Frame()
    11. myFrame.show()
    12.  
    13. if __name__ == "__main__":
    14. app = QtGui.QApplication(sys.argv)
    15. mainWin = StartQt4()
    16. mainWin.show()
    17. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    main_window.py:
    Qt Code:
    1. class Ui_MainWindow(object):
    2. def setupUi(self, MainWindow):
    3. MainWindow.setObjectName(_fromUtf8("MainWindow"))
    4. MainWindow.resize(640, 480)
    5. self.centralwidget = QtGui.QWidget(MainWindow)
    6. self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    7. self.label = QtGui.QLabel(self.centralwidget)
    8. self.label.setGeometry(QtCore.QRect(155, 45, 146, 81))
    9. self.label.setObjectName(_fromUtf8("label"))
    10. MainWindow.setCentralWidget(self.centralwidget)
    11. self.statusbar = QtGui.QStatusBar(MainWindow)
    12. self.statusbar.setObjectName(_fromUtf8("statusbar"))
    13. MainWindow.setStatusBar(self.statusbar)
    14.  
    15. self.retranslateUi(MainWindow)
    16. QtCore.QMetaObject.connectSlotsByName(MainWindow)
    17.  
    18. def retranslateUi(self, MainWindow):
    19. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    20. self.label.setText(_translate("MainWindow", "Main Window without menu bar", None))
    To copy to clipboard, switch view to plain text mode 
    frame2.py:

    Qt Code:
    1. class Ui_Frame(object):
    2. def setupUi(self, Frame):
    3. Frame.setObjectName(_fromUtf8("Frame"))
    4. Frame.resize(320, 240)
    5. Frame.setFrameShape(QtGui.QFrame.Box)
    6. Frame.setFrameShadow(QtGui.QFrame.Plain)
    7. Frame.setLineWidth(8)
    8. self.label = QtGui.QLabel(Frame)
    9. self.label.setGeometry(QtCore.QRect(100, 60, 141, 76))
    10. self.label.setObjectName(_fromUtf8("label"))
    11.  
    12. self.retranslateUi(Frame)
    13. QtCore.QMetaObject.connectSlotsByName(Frame)
    14.  
    15. def retranslateUi(self, Frame):
    16. Frame.setWindowTitle(_translate("Frame", "Frame", None))
    17. self.label.setText(_translate("Frame", "This is the frame", None))
    18.  
    19. class Frame(QtGui.QFrame, Ui_Frame):
    20. def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
    21. QtGui.QFrame.__init__(self, parent, f)
    22.  
    23. self.setupUi(self)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Working with widgets created bt Qt Designer

    It shows up in a separte window because a widget becomes a window if it doesn't have any parent widget.

    You probably want your main window's central widget as the frame's parent and also add the frame to the central widget's layout.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Working with widgets created bt Qt Designer

    Thanks. All I had to do was what you said:

    Qt Code:
    1. myFrame = Frame(self)
    To copy to clipboard, switch view to plain text mode 

    self references the mainWin parent not itself (myFrame). Check autoFillBackground to get the stuff behind frame hidden. I just put that in for other newbies who might read this in the future.

    Not sure about the central widget stuff though. In reality my main window will actually have its own child buttons designed in through Qt Designer and clicked events that will bring up the frame(s) at a specific place on the screen, then when another button is pressed (child object of the frame itself) the frame will close. In other words different buttons in the main window will show different frames, but each frame will have its own child button to hide itself. Am I programming doing this the correct way to achieve what I want?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Working with widgets created bt Qt Designer

    A QMainWindow always works with a central widget, this is why the code you got generated from your designer file has one as well.

    The layout of a main window is complex (menu bar, toolbar, etc), so the application develops put their UI into the central widget area.

    That widget can be anything, often jsut empty and filled with custom widgets after the main window's setpUi() call.

    Cheers,
    _

  5. #5
    Join Date
    Aug 2016
    Posts
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Working with widgets created bt Qt Designer

    Okay I grok the central widget thing. But I'm creating an application without menu bars, tool bars etc for a touchscreen system. The main window (self) will have a few buttons etc that when clicked show the other frames I've made. The other frames will have their own buttons that when clicked will close themselves. What advantage is there to add all my frames to the central widget of the main window in this case?


    Qt Code:
    1. myFrame = Form(self)
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. myFrame = Form(self.centralwidget)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Working with widgets created bt Qt Designer

    Quote Originally Posted by dpetican View Post
    Okay I grok the central widget thing. But I'm creating an application without menu bars, tool bars etc for a touchscreen system.
    Hmm, ok.
    Which of the QMainWindow features are you using? I.e. what is your reason to use it instead of, say, a plain QWidget?

    Quote Originally Posted by dpetican View Post
    What advantage is there to add all my frames to the central widget of the main window in this case?
    QMainWindow has a complex layout, you don't want to mess with that.

    The central widget is the window's content area, under control of the application developer.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 13th April 2016, 03:29
  2. Replies: 4
    Last Post: 30th December 2012, 22:20
  3. Replies: 4
    Last Post: 25th October 2012, 18:29
  4. Replies: 2
    Last Post: 30th March 2011, 20:20
  5. Subclassing a widget created in Qt Designer
    By dmginc in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2011, 06:30

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.