FEMisMyHobby
FEMisMyHobby

Reputation: 1

In PyQt4 does QWidget with QFormLayout not work if called from QMainWindow?

Tried to search for this, but couldn't find an example where anyone is calling up a QWidget with QFormLayout from QMainWindow.

I am trying to make an object I can call from my main application to take input from the user, and I want to use QFormLayout. The problem is I can call it up fine from the command line, but if I try to call it from my main application (QMainWindow), it just doesn't show up.

I'm on Ubuntu 18.04.4 LTS and python 2.7.17 if that matters. Thanks in advance to anyone who can help me out.

This code will show what I mean, where example2 works, but example1 does not:

import sys
from PyQt4 import QtGui



class GetInput(QtGui.QWidget):

    def __init__(self,inputs,inOrder,selected):
        super(GetInput, self).__init__()
        print 'GetInput class called'

        self.x1 = 20
        self.x2 = 130
        self.y1 = 20
        self.y2 = self.y1

        self.formLayout = QtGui.QFormLayout(self)

        self.inputs = inputs
        self.inOrder = inOrder
        self.selected = {}
        for prop in inputs:
            self.selected[prop] = self.inputs[prop]

        self.return_variables = selected
        self.line_edit = {}

        self.x1 = 20
        self.x2 = 130
        self.y1 = 20
        self.y2 = self.y1
        inputNum = 0

        for prop in inOrder:
            self.line_edit[prop] = QtGui.QLineEdit()
            self.line_edit[prop].textChanged.connect(self.changeInput)
            self.formLayout.addRow(prop,self.line_edit[prop])
            self.line_edit[prop].move(self.x1, self.y1 + 2 + inputNum*40)

            self.y2 += 40
            inputNum += 1
        
        self.button_ok = QtGui.QPushButton('OK', self)
        self.button_ok.move(40, self.y2+20)
        self.button_ok.clicked.connect(self.returnInput)
        self.button_ok.clicked.connect(self.close)

        self.button_cancel = QtGui.QPushButton('Cancel', self)
        self.button_cancel.move(150, self.y2+20)
        self.button_cancel.clicked.connect(self.close)

        self.setLayout = self.formLayout
        self.setGeometry(300, 300, 290, self.y2+90)
        self.setWindowTitle("Input Properties")
        self.show()

    def changeInput(self,text):
        pass

    def returnInput(self):
        for prop in self.inOrder:
            self.return_variables[prop] = "%s" % self.line_edit[prop].text()
            print prop+':', self.return_variables[prop]



class MainWindow(QtGui.QMainWindow):
    
    def __init__(self,choices,selected,current):
        super(MainWindow, self).__init__()
        self.choices = choices
        self.selected = selected
        self.current = current
        self.initUI()
        
    def initUI(self):      
        btn1 = QtGui.QPushButton("Input", self)
        btn1.move(30, 50)
        btn2 = QtGui.QPushButton("Show", self)
        btn2.move(150, 50)
        btn1.clicked.connect(self.button1Clicked)            
        btn2.clicked.connect(self.button2Clicked)
        
        self.statusBar()
        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Input test')
        self.show()
        
    def button1Clicked(self):
        selectionWidget = GetInput(self.choices,self.selected,self.current)

    def button2Clicked(self):
        print 'properties:', inputsInOrder
        print 'inputs:', selected





if __name__ == '__main__':

    selected = {}
    inputs = {'Name': 'material', 'Elasticity': '0.', 'Poisson ratio': '0.', 'Density': '0.' }
    inputsInOrder = ['Name', 'Elasticity', 'Poisson ratio', 'Density']

    inputApp = QtGui.QApplication(sys.argv)
    example1 = MainWindow(inputs,inputsInOrder,selected)
#    example2 = GetInput(inputs,inputsInOrder,selected)
    sys.exit(inputApp.exec_())

Upvotes: 0

Views: 16

Answers (0)

Related Questions