beginAgain
beginAgain

Reputation: 211

Access QT Designer Objects Programmatically

Disclaimer: New to both python and qt designer

QT Designer 4.8.7

Python 3.4

PyCharm 5.0.3

Question - How do I add controls to the main form or a scroll area widget on the main form (created in QT Designer) programmatically?

I have created a MainWindow in qt designer and added my widgets. The following is the entire test program in PyCharm:

import sys
from PyQt4 import QtGui, QtCore,  uic

from PyQt4.QtGui import *

from PyQt4.QtCore import *

qtCreatorFile = "programLauncher.ui"

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
    QtGui.QMainWindow.__init__(self)
    Ui_MainWindow.__init__(self)
    self.setupUi(self)

    # Cannot resize or maximize
    self.setFixedSize(1045, 770)

    # Add button test
    self.dateLabel = QtGui.QLabel("Test")
    self.pushButton = QtGui.QPushButton('Test button')

    # self.scrollArea_programs.addWidget()

    grid = QtGui.QGridLayout()
    # self.scrollArea_programs.addWidget(self.pushButton)
    grid.addWidget(self.dateLabel,0,0)
    grid.addWidget(self.pushButton,0,1)
    self.setLayout(grid)

    self.pushButton_exit.clicked.connect(self.closeEvent)

def closeEvent(self):
    QtGui.QApplication.quit()


if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  window = MyApp()
  window.show()
  sys.exit(app.exec_())

As you can see I tried to add controls to a grid but nothing shows up when the program runs - I have also tried to add a control to the scroll area. Can someone help me to just add 1 control to the scroll area at run time - so then I can know the proper way to do it or "a" proper way to do this.

Thanks in advance

Upvotes: 1

Views: 2249

Answers (3)

beginAgain
beginAgain

Reputation: 211

Thanks to JCVanHamme (the programLauncher.ui hint) and also outside help I now learned most of what I need to know to access MainWindow at run time. So for anyone interested in this beginner tip:

  1. Take a blank form in QT Designer
  2. Add a control
  3. Run pyuic4 batch file
  4. Take a look at the generated .py file to learn EVERYTHING about how to add controls.

Don't let the power go to your head - cheers

Upvotes: 0

bibi
bibi

Reputation: 3765

You can use the designer to create your .ui file The you can load it in your .py using something like:

from PyQt4 import QtCore, QtGui, uic
class my_win(QtGui.QMainWindow):

    def __init__(self):
        self.ui = uic.loadUi('my_ui.ui',self)

then you can access all your widgets with something like

self.ui.actionQuit.triggered.connect(QtGui.qApp.quit)

or

self.ui.my_button.triggered.connect(self.do_someting)

Upvotes: 0

JCVanHamme
JCVanHamme

Reputation: 1050

Without having access to your programLauncher.ui and making minimal changes to your posted code, you can add your UI elements to the window like so:

from PyQt4 import QtGui
import sys


class MyApp(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        # Cannot resize or maximize
        self.setFixedSize(1045, 770)
        widget = QtGui.QWidget(self)
        self.setCentralWidget(widget)
        # Add button test
        self.dateLabel = QtGui.QLabel("Test")
        self.pushButton = QtGui.QPushButton('Test button')

        grid = QtGui.QGridLayout()
        grid.addWidget(self.dateLabel, 0, 0)
        grid.addWidget(self.pushButton, 0, 1)
        widget.setLayout(grid)

        self.pushButton.clicked.connect(self.closeEvent)

    def closeEvent(self, event):
        QtGui.QApplication.quit()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

This will get the controls on the screen, although the layout leaves a lot to be desired. You may have to make modifications to this based on what's in your .ui file. One thing that you'll want to note in this example is that the QMainWindow needs a central widget (widget in the example above). You then set the layout on that widget.

Upvotes: 1

Related Questions