Pyside or qt test

Reference:


In [1]:
#Global initialize
import sys
import os
#for Home: ipython notebook
os.chdir("/Users/chenchen/Documents/Git/ipynotebook/pyside/")
sys.version

#for office: ipython3-3.3 notebook
#os.chdir("/Users/chenchen/Documents/git/ipynotebook/pyside/")
#sys.version


Out[1]:
'2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Nov 11 2013, 10:49:09) \n[GCC 4.0.1 (Apple Inc. build 5493)]'

In [2]:
%qtconsole

Hello World: Your First PySide Application


In [2]:
%qtconsole

In [3]:
import sys
import os
os.chdir("/Users/chenchen/Documents/Git/ipynotebook/pyside/")

In [4]:
from PySide import QtGui, QtCore

In [5]:
app = QtGui.QApplication(sys.argv)

In [6]:
label = QtGui.QLabel("<h1><font color=red>Hello, Qt</font></h1>")

In [7]:
label.show()

sys.exit(app.exec_())


An exception has occurred, use %tb to see the full traceback.

SystemExit: 0
To exit: use 'exit', 'quit', or Ctrl-D.

Your first application using PySide and QtQuick/QML

  • Note: qml write in Qt Creator, and paste here.
  • QtQuick support upto 1.1 //2.0 not support

In [79]:
%%writefile view.qml
import QtQuick 1.1

Rectangle {
    width: 200
    height: 200
    color: "red"
    
    Text {
        //id: test
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
}


Overwriting view.qml

In [8]:
%%writefile view.qml

import QtQuick 1.1

Rectangle {
    width: 200
    height: 62
    color: "white"

    Text {
        id: test
        text: "Hello World"
        color: "blue"
        anchors.centerIn: parent
    }
}


Overwriting view.qml

In [9]:
#the main.py

import sys
from PySide import QtCore, QtGui
from PySide.QtDeclarative import QDeclarativeView

In [4]:
#Creat app
app = QtGui.QApplication(sys.argv)

In [10]:
#view
view = QDeclarativeView()

In [11]:
#Creat an URL to the QML file
url = QtCore.QUrl("view.qml")

In [12]:
#set the QML file and show
view.setSource(url)

In [13]:
#show and quit
view.show()

sys.exit(app.exec_())


An exception has occurred, use %tb to see the full traceback.

SystemExit: 0
To exit: use 'exit', 'quit', or Ctrl-D.

PySideSimplicissimus:

Prerequisites:


In [15]:
import PySide

In [16]:
PySide.__version__


Out[16]:
'1.1.2'

In [17]:
import PySide.QtCore

In [18]:
PySide.QtCore.__version__


Out[18]:
'4.7.4'

In [19]:
PySide.QtCore.qVersion()


Out[19]:
'4.7.4'
  • Hello world program

In [20]:
import sys

In [21]:
import PySide

In [22]:
from PySide.QtGui import QApplication
from PySide.QtGui import QMessageBox

In [ ]:
#Create the application opject
app = QApplication(sys.argv)

In [24]:
#message box
msgBox = QMessageBox()
msgBox.setText("Hello World " + PySide.__version__)
msgBox.show()
msgBox.exec_()


Out[24]:
1024

Close


In [30]:
import sys
import PySide.QtGui

In [31]:
from PySide.QtGui import QMainWindow, QPushButton, QApplication

In [32]:
from ui_quitter import Ui_MainWindow

In [33]:
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

In [11]:
#if __name__ =="__main__":
app = QApplication(sys.argv)


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-0c4dacc4a780> in <module>()
      1 #if __name__ =="__main__":
----> 2 app = QApplication(sys.argv)

RuntimeError: A QApplication instance already exists.

In [34]:
frame = MainWindow()

In [35]:
frame.show()
app.exec_()
sys.exit()


An exception has occurred, use %tb to see the full traceback.

SystemExit
To exit: use 'exit', 'quit', or Ctrl-D.

About Box


In [ ]:


In [ ]: