viandante 0 Newbie Poster

Hello!

I am trying to create a tabbed program with Pyqt4. The idea is that I want to be able to add python scripts in a folder. Those scritps should be then be accessible from a main framework/program. Finally, when I run the python script I want the window to be displayed in a tab of the main program.

Below you can find what I have got until now. It's a simple tree structure. When I double clik the second level items, I will run my script. At the moment the script runs in it's own dialog window. I would like it to run in a tab of the framework.

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from prova import Ui_MainWindow


data = [("Hyperion related", [("Hyperion imports", [("Create tables", []), ("Create hyperion import file", [])])])]

class StartQT4(QMainWindow):

	def __init__(self, parent=None):
		QWidget.__init__(self, parent)
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)

		self.model = QStandardItemModel()
		self.addItems(self.model, data)
		self.ui.treeView.setEditTriggers(QAbstractItemView.NoEditTriggers)
		#Models
		self.ui.treeView.setModel(self.model)
		fileSelectionModel =QItemSelectionModel(self.model, self.ui.treeView)
		self.ui.treeView.setSelectionModel(fileSelectionModel)
		
		self.ui.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
		self.ui.treeView.customContextMenuRequested.connect(self.openMenu)
		self.model.setHorizontalHeaderLabels([self.tr("Object")])
		
		#self.connect(self.ui.treeView, SIGNAL("clicked(QModelIndex)"), self.runDescription)
		self.connect(self.ui.treeView, SIGNAL("doubleClicked(QModelIndex)"), self.runScript)
		self.connect(fileSelectionModel, SIGNAL("selectionChanged(const QItemSelection &, const QItemSelection &)"), self.runDescription)

	def addItems(self, parent, elements):
		for text, children in elements:
			item = QStandardItem(text)
			parent.appendRow(item)
			if children:
				self.addItems(item, children)
		
	def runScript(self,index):
		indexes = self.ui.treeView.selectedIndexes()
		if len(indexes) > 0:
			level = 0
			index = indexes[0]
			while index.parent().isValid():
				index = index.parent()
				level += 1
		if level==2:
			index = indexes[0]
			item_text = index.data().toString()
			if item_text=="Create tables":
				import Hyp as hyp
				hyp.interface()
			if item_text=="Create hyperion import file":
				import toHypExp as toyhpexp
				toyhpexp.interface()
		
	def runDescription(self, index):
		self.ui.textEdit.setText("")
		indexes = self.ui.treeView.selectedIndexes()
		if len(indexes) > 0:
			level = 0
			index = indexes[0]
			while index.parent().isValid():
				index = index.parent()
				level += 1
		if level==2:
			index = indexes[0]
			item_text = index.data().toString()
			if item_text=="Create tables":
				f=open("HypDscr.txt","r")
				file_module=f.read()
				self.ui.textEdit.setText(file_module)
			
	def openMenu(self, position):

		indexes = self.ui.treeView.selectedIndexes()
		if len(indexes) > 0:

			level = 0
			index = indexes[0]
			while index.parent().isValid():
				index = index.parent()
				level += 1

		menu = QMenu()
		if level == 0:
			entry=menu.addAction(self.tr("Edit person"))
			self.connect(entry,SIGNAL('triggered()'), lambda item="Edit person": self.doStuff())
		elif level == 1:
			menu.addAction(self.tr("Edit object/container"))
		elif level == 2:
			menu.addAction(self.tr("Edit object"))

		menu.exec_(self.ui.treeView.viewport().mapToGlobal(position))
		
	def doStuff(self):
		print "hello"

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

Here the ui file.

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

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created: Tue Mar 15 14:49:21 2011
#      by: PyQt4 UI code generator 4.5.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.treeView = QtGui.QTreeView(self.centralwidget)
        self.treeView.setGeometry(QtCore.QRect(5, 10, 261, 531))
        self.treeView.setObjectName("treeView")
        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(280, 10, 501, 491))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))