Issue
Here it is my sample code, in that i want to see a particular directory of all my files in Qscrollbar widget, up to that its working fine. But after that when i select any one of the file i want to show that file data in same scroll area,but i don't know how can i do my files are clickable so please tell me how to do that my all files are clickable, Thank you in advance This is my code:
import sys
import os
import csv
from PySide import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
super( Example, self).__init__(parent)
self.btn = QtGui.QPushButton('scan')
self.lay1 = QtGui.QVBoxLayout()
self.lay1.addWidget(self.btn)
self.btn.clicked.connect(self.open_file)
self.scrollArea =QtGui. QScrollArea()
self.scrollArea.setBackgroundRole(QtGui.QPalette.Light)
self.scrollArea.setWidgetResizable(True)
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addLayout(self.lay1,0,0)
self.mainLayout.addWidget(self.scrollArea,0,1)
self.setCentralWidget(QtGui.QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self.setGeometry(100,100,800,500)
def open_file(self):
self.w = QtGui.QWidget()
self.lay =QtGui. QVBoxLayout()
self.model = QtGui.QStandardItemModel(self)
self.tableView =QtGui. QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.appendRowItems("/home/cioc/Documents/GPR/GRP")
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
def appendRowItems(self, dir):
for root, dirs, files in os.walk(dir):
if root == dir:
for file in files:
self.model.appendRow(QtGui.QStandardItem(file))
# self.model.isSelectable(True).connect(onFileClicked)
def onFileClicked(self, file):
self.w = QtGui.QWidget()
self.lay = QtGui.QVBoxLayout()
self.model = QtGui.QStandardItemModel(self)
self.tableView = QtGui.QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
with open(file.fileName(), "rb") as fileInput:
for row in csv.reader(fileInput):
items = [
QtGui.QStandardItem(field)
for field in row
]
self.model.appendRow(items)
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
eg = Example()
eg.show()
app.exec_()
Solution
I'm sorry, I have PyQt5
import sys
import os
import csv
#from PySide.QtCore import *
#from PySide.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Example(QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.dirPath = "E:/_Qt/Python-Examples/_PyQt5/Test"
self.filePath = ""
self.btn = QPushButton('scan')
self.btn.clicked.connect(self.open_file)
self.btnSee = QPushButton('See file')
self.btnSee.clicked.connect(lambda filePath=self.filePath: self.see_file(self.filePath))
self.lay1 = QVBoxLayout()
self.lay1.addWidget(self.btn)
self.lay1.addWidget(self.btnSee)
self.scrollArea = QScrollArea()
self.scrollArea.setBackgroundRole(QPalette.Light)
self.scrollArea.setWidgetResizable(True)
self.mainLayout = QGridLayout()
self.mainLayout.addLayout(self.lay1,0,0)
self.mainLayout.addWidget(self.scrollArea,0,1)
self.setCentralWidget(QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self.setGeometry(100,100,800,500)
def open_file(self):
self.w = QWidget()
self.lay = QVBoxLayout()
self.model = QStandardItemModel(self)
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.clicked[QModelIndex].connect(self.onClick)
#self.appendRowItems("/home/cioc/Documents/GPR/GRP")
self.appendRowItems(self.dirPath)
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
def appendRowItems(self, dir):
for root, dirs, files in os.walk(dir):
if root == dir:
for file in files:
self.model.appendRow(QStandardItem(file))
# self.model.isSelectable(True).connect(onFileClicked)
def onClick(self, ind):
self.filePath = self.dirPath +"/"+ self.model.item(ind.row(), ind.column()).text()
#def onFileClicked(self, file):
def see_file(self, file):
self.w = QWidget()
self.lay = QVBoxLayout()
self.model = QStandardItemModel(self)
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
try:
#with open(file.fileName(), "rb") as fileInput:
with open(file, "r") as fileInput: # !!! "r"
for row in csv.reader(fileInput):
items = [
QStandardItem(field)
for field in row
]
#self.model.appendRow(items)
if items: self.model.appendRow(items)
except Exception as err:
QMessageBox.warning(self, 'Error', str(err))
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
if __name__ == '__main__':
app = QApplication(sys.argv)
eg = Example()
eg.show()
app.exec_()
Option 3 (last)
import sys
import os
import csv
#from PySide.QtCore import *
#from PySide.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Example(QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.dirPath = "E:/_Qt/Python-Examples/_PyQt5/Test"
self.filePath = ""
self.scrollArea = QScrollArea()
self.scrollArea.setBackgroundRole(QPalette.Light)
self.scrollArea.setWidgetResizable(True)
self.mainLayout = QGridLayout()
self.mainLayout.addWidget(self.scrollArea,0,0)
self.setCentralWidget(QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self.setGeometry(100,100,800,500)
self.open_file()
def open_file(self):
self.w = QWidget()
self.lay = QVBoxLayout()
self.model = QStandardItemModel(self)
self.tableView = QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.clicked[QModelIndex].connect(self.onClick)
self.appendRowItems(self.dirPath)
self.lay.addWidget(self.tableView)
self.w.setLayout(self.lay)
self.scrollArea.setWidget(self.w)
def appendRowItems(self, dir):
for root, dirs, files in os.walk(dir):
if root == dir:
for file in files:
self.model.appendRow(QStandardItem(file))
def onClick(self, ind):
self.filePath = self.dirPath +"/"+ self.model.item(ind.row(), ind.column()).text()
self.see_file(self.filePath)
def see_file(self, file):
if self.lay.count() > 1:
self.tableView2.close()
self.lay.removeWidget(self.tableView2)
self.model2 = QStandardItemModel(self)
self.tableView2 = QTableView(self)
self.tableView2.setModel(self.model2)
self.tableView2.horizontalHeader().setStretchLastSection(True)
try:
with open(file, "r") as fileInput:
for row in csv.reader(fileInput):
items = [
QStandardItem(field)
for field in row
]
if items: self.model2.appendRow(items)
except Exception as err:
QMessageBox.warning(self, 'Error', str(err))
self.lay.addWidget(self.tableView2)
self.w.setLayout(self.lay)
if __name__ == '__main__':
app = QApplication(sys.argv)
eg = Example()
eg.show()
app.exec_()
Answered By - S. Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.