[PyKDE] Question about Qt syntax between C++ and python

Michael Sullivan michael at espersunited.com
Thu Jun 29 19:09:05 BST 2006


On Thu, 2006-06-29 at 18:44 +0100, Phil Thompson wrote:
> On Thursday 29 June 2006 6:30 pm, Michael Sullivan wrote:
> > If I've got a symbolic constant, such as "Qt::AlignHCenter", how would I
> > write that in python?
> 
> Qt.AlignHCenter
> 
> Phil

Here is my program:

#!/usr/bin/env python

import sys
import qt

class PartyFormation(qt.QWidget):
   def __init__(self, parent=None, name=None):
      qt.QWidget.__init__(self, parent, name)

      self.setMinimumSize(800, 600)
      self.setMaximumSize(800, 600)
      self.setCaption("pyFantasy - Party Formation")

      self.space = qt.QWidget(self)
      self.blank = []

      for i in range(2):
	 self.blank.append(qt.QLabel(self))

      self.blank[0].setText("\n\n\nPlease create a party...");
      self.blank[0].setAlignment(Qt.AlignHCenter);
#      self.blank[0].setFont(QFont("OldEnglish", 16,Qt::Bold, true));


      self.edit = []
      self.combo = []
      self.label = []

      self.vbox = []
      self.vbox.append(qt.QVBoxLayout())
      self.vbox.append(qt.QVBoxLayout())
      self.vbox.append(qt.QVBoxLayout())
      self.vbox.append(qt.QVBoxLayout())

      self.hbox = []
      self.hbox.append(qt.QHBoxLayout())
      self.hbox.append(qt.QHBoxLayout())

      self.mainvbox = qt.QVBoxLayout(self)
      

      for i in range(4):
	 self.edit.append(qt.QLineEdit(self))
	 self.edit[i].setFixedWidth(150)
	 

	 self.combo.append(qt.QComboBox(self))
	 self.combo[i].insertItem("Fighter")
	 self.combo[i].insertItem("Thief")
	 self.combo[i].insertItem("Black Belt")
	 self.combo[i].insertItem("Red Mage")
	 self.combo[i].insertItem("White Mage")
	 self.combo[i].insertItem("Black Mage")

	 
	 self.label.append(qt.QLabel(self))

	 if i < 2:
	    self.vbox[i].addWidget(self.edit[i])
	    self.vbox[i].addWidget(self.combo[i])
	    self.vbox[i].addWidget(self.label[i])
	 else:
	    self.vbox[i].addWidget(self.label[i])
	    self.vbox[i].addWidget(self.combo[i])
	    self.vbox[i].addWidget(self.edit[i])
	    

      self.hbox[0].addLayout(self.vbox[0])
      self.hbox[0].addLayout(self.vbox[1])
      self.hbox[1].addLayout(self.vbox[2])
      self.hbox[1].addLayout(self.vbox[3])

      self.mainvbox.addWidget(self.blank[0])
      self.mainvbox.addLayout(self.hbox[0])
      self.mainvbox.addWidget(self.blank[1])
      self.mainvbox.addLayout(self.hbox[1])
      self.mainvbox.addWidget(self.space)

      for i in range(4):
	 self.edit[i].show()
	 self.combo[i].show()
	 self.label[i].show()

	       

	 



a = qt.QApplication(sys.argv)

myPartyFormation = PartyFormation(None)

a.setMainWidget(myPartyFormation)
myPartyFormation.show()
sys.exit(a.exec_loop())


I tried the solution suggested, writing "Qt::AlignHCenter" as
"Qt.AlignHCenter" and saved an ran the script.  Here's the output:

michael at camille ~ $ ./myFantasy.py
Traceback (most recent call last):
  File "./myFantasy.py", line 92, in ?
    myPartyFormation = PartyFormation(None)
  File "./myFantasy.py", line 21, in __init__
    self.blank[0].setAlignment(Qt.AlignHCenter);
NameError: global name 'Qt' is not defined


Is there something more to how you use symbolic constants, and if not,
what am I doing wrong here?




More information about the PyQt mailing list