User:Axiom: Difference between revisions

From KDE TechBase
No edit summary
Line 56: Line 56:


You will need [[User:Axiom/demo.ui|this file]] too.
You will need [[User:Axiom/demo.ui|this file]] too.
==Getting user input into a variable==
Once you have it in a variable, you can throw the whole power of python at it.
I have tried to show how to do this in the simplest way possible.
''Note: This code shamelessly stolen from [http://www.apaku.de/linux/xpathevaluator/index.php xpathevaluator].  Lots of good pyqt4 sample code in there.  Also might be helpful if you want to evaluate an xpath.  But I'm not sure why you would want to do that.
===readinput.py===
<code python>
import sys
  #Load the widget
from PyQt4.QtGui import QWidget  #This is a widget, not a window, and that makes a difference
from PyQt4.uic import loadUiType  #We need this to dynamically load the .ui file we created in qt designer
from PyQt4.QtCore import pyqtSignature, Qt #Not sure what this signature thing is, but apparently it is great for buttons (signals/slots?)
import sys
(form, formbase) = loadUiType('readinput.ui') #this is the .ui file we want to show
class readInputWidget(QWidget, form):  #in this class, we load the window, and assign actions to the buttons
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setAttribute(Qt.WA_DeleteOnClose) #This doesn't seem to change anything if you delete it, but may be usefull for housekeeping
        self.setupUi(self)
     
    @pyqtSignature("on_exitButton_clicked()")
    def on_exitButton_clicked(self):
        self.close()
   
    @pyqtSignature("on_evalButton_clicked()")
    def on_evalButton_clicked(self):
        print 'This should echo our text:'
        userinput =  self.inputBox1.text() #This is the fantastic line of code we were waiting for!  Make sure you have an input box by that name, and specify that it is text
        print userinput
def main():
  from PyQt4.QtGui import QApplication
  from PyQt4.QtCore import Qt
  app = QApplication(sys.argv)
  widget = readInputWidget()
  widget.show()
  return app.exec_()
if __name__ == "__main__":
  sys.exit(main())       
</code>
You will also need the [[User:Axiom/readinput.ui|ui file]] that goes along with that.

Revision as of 18:17, 28 March 2007

Axiom's sandbox

I am just putting some stuff here right now, because you have such nice color coding in your wiki. Maybe I'll turn it into a tutorial if I figure out what I am doing.

Dynamically Loading a .ui file

I found this snippet here (french). I don't know if this is the right way to do it, but it seems to work.

loadui.py

import sys from PyQt4 import QtGui, uic

app = QtGui.QApplication(sys.argv) widget = uic.loadUi("demo.ui") widget.show()

  1. La boucle principale

app.exec_() Of course you will need to make a ui file called demo.ui for this to work.

Connecting Buttons From Dynamically loaded .ui File

This code really works.

callMainWindow.py

import sys from PyQt4 import QtGui from PyQt4 import uic from PyQt4 import QtCore

  1. the class has to have the same base class as your UI

class DemoImpl(QtGui.QMainWindow):

   def __init__(self, *args):
       QtGui.QMainWindow.__init__(self, *args)

# just pass self uic.loadUi("mainWindow.ui", self)

   #this line makes sure that we are only looking for a string
   #if we want a bool as well we can add 	@QtCore.pyqtSignature("bool")
   @QtCore.pyqtSignature("")   
   def on_button1_clicked(self):
       print "You Clicked the button!"
   @QtCore.pyqtSignature("")
   def on_button2_clicked(self):
       print "You Clicked the button2!"	


app = QtGui.QApplication(sys.argv) widget = DemoImpl() widget.show() app.exec_()


You will need this file too.



Getting user input into a variable

Once you have it in a variable, you can throw the whole power of python at it. I have tried to show how to do this in the simplest way possible. Note: This code shamelessly stolen from xpathevaluator. Lots of good pyqt4 sample code in there. Also might be helpful if you want to evaluate an xpath. But I'm not sure why you would want to do that.


readinput.py

import sys

 #Load the widget

from PyQt4.QtGui import QWidget #This is a widget, not a window, and that makes a difference from PyQt4.uic import loadUiType #We need this to dynamically load the .ui file we created in qt designer from PyQt4.QtCore import pyqtSignature, Qt #Not sure what this signature thing is, but apparently it is great for buttons (signals/slots?) import sys

(form, formbase) = loadUiType('readinput.ui') #this is the .ui file we want to show

class readInputWidget(QWidget, form): #in this class, we load the window, and assign actions to the buttons

   def __init__(self, parent=None):
       QWidget.__init__(self, parent)
       self.setAttribute(Qt.WA_DeleteOnClose) #This doesn't seem to change anything if you delete it, but may be usefull for housekeeping
       self.setupUi(self)
      
   @pyqtSignature("on_exitButton_clicked()")
   def on_exitButton_clicked(self):
       self.close()
   
   @pyqtSignature("on_evalButton_clicked()")
   def on_evalButton_clicked(self):
       print 'This should echo our text:'
       userinput =  self.inputBox1.text() #This is the fantastic line of code we were waiting for!  Make sure you have an input box by that name, and specify that it is text
       print userinput

def main():

 from PyQt4.QtGui import QApplication
 from PyQt4.QtCore import Qt
 app = QApplication(sys.argv)
 widget = readInputWidget()
 widget.show()
 return app.exec_()

if __name__ == "__main__":

 sys.exit(main())        

You will also need the ui file that goes along with that.