1

我正在尝试将控件(根据用户输入确定的数量)添加到使用 Qt Creator 创建的基本表单中。不幸的是,GridLayout 没有扩展,因此除了 4 次“for”循环迭代之外,所有小部件都被压扁了。任何关于在哪里寻找解决方案的想法都将不胜感激。谢谢期待...

import sys 
from CoordinatesUI import Ui_Coordinates
from BuildChord import Ui_BuildChord
from PyQt4 import QtGui, QtCore


class Main(QtGui.QMainWindow):
   def __init__(self):
    self.mvtNum  = None
    self.blockNum = None
    self.progNum = None
    self.chordNum = None
    self.numOfPitches = None


    QtGui.QMainWindow.__init__(self)
    self.ui = Ui_Coordinates()
    self.ui1 = Ui_BuildChord()
    self.ui.setupUi(self)

    self.setUpsignals()

def commitCoords(self):
    self.mvtNum = self.ui.txtMvt.text()
    self.blockNum = self.ui.txtBlock.text()
    self.progNum = self.ui.txtProg.text()
    self.chordNum = self.ui.txtChord.text()
    print self.mvtNum, self.blockNum, self.progNum, self.chordNum
    self.ui1 = Ui_BuildChord()
    self.ui1.setupUi(self)
    self.setUpSignals2()
    window.show()

def genPitches(self):
    self.numOfPitches = int(self.ui1.txtNumOfPitches.text())
    sui = self.ui1.gridLayout
    BtnC = QtGui.QPushButton()
    self.showMaximized()
    for i in range(3, self.numOfPitches + 3):
            leP = QtGui.QLineEdit()
            leR = QtGui.QLineEdit()
            leC = QtGui.QCheckBox()
            leP.setObjectName("p" + str(i - 3))
            leR.setObjectName("r" + str(i - 3))
            leC.setObjectName("c" + str(i - 3))
            sui.addWidget(leP,i,3)
            sui.addWidget(leR,i,5)
            sui.addWidget(leC,i,6, QtCore.Qt.AlignCenter)

    BtnC.setText("Commit")
    sui.gridLayout.addWidget(BtnC, self.numOfPitches + 4, 7)

    print "Create clicked", self.numOfPitches

def setUpsignals(self): 
    self.ui.btnCommit.clicked.connect(self.commitCoords)

def setUpSignals2(self): 
    self.ui1.btnCreate.clicked.connect(self.genPitches)


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

这是QT生成的代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'BuildC.ui'
#
# Created: Sun May  3 22:31:44 2015
#      by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
    return s

 try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
    def _translate(context, text, disambig):
    return QtGui.QApplication.translate(context, text, disambig)

 class Ui_BuildChord(object):
 def setupUi(self, mainWindow):
    mainWindow.setObjectName(_fromUtf8("mainWindow"))
    mainWindow.resize(439, 381)
    self.centralWidget = QtGui.QWidget(mainWindow)
    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
    self.gridLayoutWidget = QtGui.QWidget(self.centralWidget)
    self.gridLayoutWidget.setGeometry(QtCore.QRect(-10, 40, 432, 176))
    self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
    self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
    self.gridLayout.setMargin(0)
    self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
    self.label = QtGui.QLabel(self.gridLayoutWidget)
    self.label.setObjectName(_fromUtf8("label"))
    self.gridLayout.addWidget(self.label, 1, 2, 1, 1)
    spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum,            QtGui.QSizePolicy.Expanding)
    self.gridLayout.addItem(spacerItem, 4, 2, 1, 1)
    self.label_2 = QtGui.QLabel(self.gridLayoutWidget)
    self.label_2.setObjectName(_fromUtf8("label_2"))
    self.gridLayout.addWidget(self.label_2, 2, 3, 1, 1)
    spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding,     QtGui.QSizePolicy.Minimum)
    self.gridLayout.addItem(spacerItem1, 1, 4, 1, 1)
    self.label_3 = QtGui.QLabel(self.gridLayoutWidget)
    self.label_3.setObjectName(_fromUtf8("label_3"))
    self.gridLayout.addWidget(self.label_3, 2, 5, 1, 1)
    spacerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
    self.gridLayout.addItem(spacerItem2, 2, 0, 1, 1)
    spacerItem3 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
    self.gridLayout.addItem(spacerItem3, 1, 0, 1, 1)
    self.label_4 = QtGui.QLabel(self.gridLayoutWidget)
    self.label_4.setObjectName(_fromUtf8("label_4"))
    self.gridLayout.addWidget(self.label_4, 2, 6, 1, 1)
    self.txtNumOfPitches = QtGui.QLineEdit(self.gridLayoutWidget)
    self.txtNumOfPitches.setObjectName(_fromUtf8("txtNumOfPitches"))
    self.gridLayout.addWidget(self.txtNumOfPitches, 1, 3, 1, 1)
    self.btnCreate = QtGui.QPushButton(self.gridLayoutWidget)
    self.btnCreate.setDefault(False)
    self.btnCreate.setFlat(False)
    self.btnCreate.setObjectName(_fromUtf8("btnCreate"))
    self.gridLayout.addWidget(self.btnCreate, 1, 5, 1, 1)
    spacerItem4 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
    self.gridLayout.addItem(spacerItem4, 2, 1, 1, 1)
    mainWindow.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(mainWindow)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 439, 22))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    mainWindow.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(mainWindow)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    mainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(mainWindow)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    mainWindow.setStatusBar(self.statusBar)

    self.retranslateUi(mainWindow)
    QtCore.QMetaObject.connectSlotsByName(mainWindow)

def retranslateUi(self, mainWindow):
    mainWindow.setWindowTitle(_translate("mainWindow", "Harmonic Engine", None))
    self.label.setText(_translate("mainWindow", "Enter Number of Pitches:", None))
    self.label_2.setText(_translate("mainWindow", "Pitch Class:", None))
    self.label_3.setText(_translate("mainWindow", "Registration:", None))
    self.label_4.setText(_translate("mainWindow", "Sustain:", None))
    self.btnCreate.setText(_translate("mainWindow", "Create", None))


if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
mainWindow = QtGui.QMainWindow()
ui = Ui_BuildChord()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
4

0 回答 0