PyQtç·´ç¿
Qtã®æ
å ±ããªãå°ãªãæãã§ãã
ãªãã§ãPyQtã®ãç¹ã«PyQt4ã®ãï½
æåã®ä¸æ©ã¨ãã¦ã¯ããã®ãã¥ã¼ããªã¢ã«è¨äºãããã£ãã
http://programming.it-cafe.jp/pyqt4tutorial/pyqt001.html
C++ã®æ¹ã®Qt4æ¥æ¬èªããã¥ã¡ã³ã
http://linux-life.net/program/cc/qt/qt4doc/index.html
ã¡ã¤ã³ã¦ã£ã³ãã¦
ã¦ã£ã³ãã¦ä¸å¤®ã«ãã¼ããé
ç½®ããå ´åãªã©ã«ä½¿ããQMainWindow::setCentralWidgetsã使ãã
CentralWidgets以å¤ã«MenuBar, ToolBar, StatusBar, Dockã使ãããããã
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys from PyQt4 import QtCore, QtGui class MyWidget(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) button=QtGui.QPushButton(self.trUtf8("éãã"), self) self.connect(button, QtCore.SIGNAL("clicked()"), QtGui.qApp, QtCore.SLOT("quit()")) self.setCentralWidget(button) if __name__=="__main__": app = QtGui.QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_())
ãªã¹ã
MVCã¨ãããModel/Viewã«ãªã£ã¦ããã®ã§Modelã¯ã©ã¹ãå®ç¾©ãã¦setModelãã¦ãã
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys from PyQt4 import QtCore, QtGui class MyModel(QtCore.QAbstractListModel): def __init__(self, parent=None): QtCore.QAbstractListModel.__init__(self, parent) self.rows=[self.trUtf8("æ¥æ¬"), "USA", "UK"] def rowCount(self, parent = QtCore.QModelIndex()): return len(self.rows) def data(self, index, role): if not index.isValid(): return QtCore.QVariant() if index.row() >= len(self.rows): return QtCore.QVariant() if role == QtCore.Qt.DisplayRole: return QtCore.QVariant(self.rows[index.row()]) else: return QtCore.QVariant() class MyWidget(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) list = QtGui.QListView(self) model=MyModel(self) list.setModel(model) self.setCentralWidget(list) if __name__=="__main__": app = QtGui.QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_())
ããªã¼
QAbstractItemModelã®ä½¿ãæ¹ããã¾ãã¡ããã£ã¦ããªãã®ã§åé·ããã
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys from PyQt4 import QtCore, QtGui class Node: def __init__(self, data, parent=None): self.data=data self.parent=parent self.children=[] def addChild(self, data): node=Node(data, self) self.children.append(node) return node def row(self): if self.parent: return self.parent.children.index(self) else: return 0 class MyModel(QtCore.QAbstractItemModel): def __init__(self, parent=None): QtCore.QAbstractItemModel.__init__(self, parent) # rootãã¼ã(表示ãããæä¸ä½ã®ããã²ã¨ã¤ä¸ã®é層ã«ãªã) self.root=Node([QtCore.QVariant("Title")]) self.setupTree(self.root) def setupTree(self, node): # 1é層 current=node.addChild([self.trUtf8("æ¥æ¬")]) # 2é層 current=current.addChild([self.trUtf8("é¢æ±")]) # 3é層 current.addChild([self.trUtf8("è¨åç")]) current.addChild([self.trUtf8("æ æ¨ç")]) current.addChild([self.trUtf8("群馬ç")]) current.addChild([self.trUtf8("å¼çç")]) current.addChild([self.trUtf8("åèç")]) current.addChild([self.trUtf8("æ±äº¬é½")]) current.addChild([self.trUtf8("ç¥å¥å·ç")]) def columnCount(self, parent): if parent.isValid(): # parent.internalPointer()ã§Nodeã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ã«ã¢ã¯ã»ã¹ãã return len(parent.internalPointer().data) else: # header return len(self.root.data) def data(self, index, role): if not index.isValid(): return QtCore.QVariant() if role != QtCore.Qt.DisplayRole: return QtCore.QVariant() return QtCore.QVariant(index.internalPointer().data[index.column()]) def flags(self, index): if not index.isValid(): return QtCore.Qt.ItemIsEnabled return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable def headerData(self, section, orientation, role): if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: return self.root.data[section] return QtCore.QVariant() def index(self, row, column, parent): if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent): return QtCore.QModelIndex() if not parent.isValid(): parentItem = self.root else: parentItem = parent.internalPointer() childItem = parentItem.children[row] if childItem: return self.createIndex(row, column, childItem) else: return QtCore.QModelIndex() def parent(self, index): if not index.isValid(): return QtCore.QModelIndex() childItem = index.internalPointer() parentItem = childItem.parent if parentItem == self.root: return QtCore.QModelIndex() return self.createIndex(parentItem.row(), 0, parentItem) def rowCount(self, parent): if parent.column() > 0: return 0 if not parent.isValid(): parentItem = self.root else: parentItem = parent.internalPointer() return len(parentItem.children) class MyWidget(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) tree = QtGui.QTreeView(self) model=MyModel(self) tree.setModel(model) self.setCentralWidget(tree) if __name__=="__main__": app = QtGui.QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_())
ã¹ããªããã¦ã£ã³ãã¦
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys from PyQt4 import QtCore, QtGui class MyWidget(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) splitter = QtGui.QSplitter(self) model=QtGui.QDirModel(self) # pane1 treeview = QtGui.QTreeView() treeview.setModel(model) treeview.setRootIndex(model.index(QtCore.QDir.currentPath())) splitter.addWidget(treeview) # pane2 listview = QtGui.QListView() listview.setModel(model) listview.setRootIndex(model.index(QtCore.QDir.currentPath())) splitter.addWidget(listview) self.setCentralWidget(splitter) if __name__=="__main__": app = QtGui.QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_())
QAbstractItemModelå¨ãã調ã¹ã¦ã¿ãå¿ è¦ãããããã