im new in Qt and im using python . I made a .ui file with qtdesigner with only a simple Tree View object . I converted the .ui file to .py with pyui.bat and I promoted the Tree view.
here is my ui code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.treeView = Main(self.centralwidget)
self.treeView.setGeometry(QtCore.QRect(300, 180, 256, 192))
self.treeView.setObjectName(_fromUtf8("treeView"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
from QTree import Main
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
and this is my tree View code:
from PyQt4 import QtGui
from PyQt4 import QtCore
class Main(QtGui.QTreeView):
def __init__(self):
QtGui.QTreeView.__init__(self)
model = QtGui.QFileSystemModel()
model.setRootPath( QtCore.QDir.currentPath() )
self.setModel(model)
QtCore.QObject.connect(self.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test)
@QtCore.pyqtSlot("QItemSelection, QItemSelection")
def test(self, selected, deselected):
print("hello!")
print(selected)
print(deselected)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
But when I try to run converted ui file I get error :
E:\Python27\python.exe C:/Users/Amirmohammad/Desktop/proje/testagain.py
Traceback (most recent call last):
File "C:/Users/Amirmohammad/Desktop/proje/testagain.py", line 56, in <module>
ui.setupUi(MainWindow)
File "C:/Users/Amirmohammad/Desktop/proje/testagain.py", line 31, in setupUi
self.treeView = Main(self.centralwidget)
TypeError: __init__() takes exactly 1 argument (2 given)
and when I try to add QWidget_parent=None to my Main class init, after running the app I get a blank window!
in one sentence I want to make a file explorer and I want to use TreeView (the second code) in a nice ui that is created with qtdesigner.
There are two problems:
parent
argument to Main.__init__
, and, ideally, pass this argument on to QTreeView.__init__
.