这是我的代码,我使用表格视图创建表格格式。该表格由4列组成,我想添加标题名称,如文件名,大小和修改日期,但我不想添加我的buttons.so的任何名称,所以请您指导我如何为表视图中仅修改的文件,日期和日期添加标题名称 鉴于下面是我的代码:
import sys
import os
import csv
import datetime
from PySide import QtGui, QtCore
class ViewWidget(QtGui.QWidget):
dirPath = "C:\raghava\main project"
def __init__(self, x, index, parent=None):
super(ViewWidget, self).__init__(parent)
self.p_index = QtCore.QPersistentModelIndex(index)
self.content_button = QtGui.QWidget(self)
lay = QtGui.QHBoxLayout(self.content_button)
lay.setContentsMargins(0, 0, 0,0)
self.label = QtGui.QLineEdit()
self.label.setText(datetime.datetime.today().strftime('%d-%m-%Y'))
self.label.setReadOnly(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label1 = QtGui.QLineEdit()
self.label1.setText(self.dirPath)
self.label1.setReadOnly(True)
self.label1.resize(250, 100)
self.label1.setAlignment(QtCore.Qt.AlignRight)
lay.addWidget(self.label)
lay.addWidget(self.label1)
self.content_button.move(x, 0)
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.dirPath = ViewWidget.dirPath
self.open_file()
self.file_data=""
def open_file(self):
self.setCentralWidget(QtGui.QWidget())
layout = QtGui. QGridLayout(self.centralWidget())
self.model =QtGui.QStandardItemModel(self)
self.tableView = QtGui.QTableView()
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(True)
self.tableView.clicked.connect(self.onClick)
self.tableView.verticalHeader().hide()
self.tableView.horizontalHeader().hide()
self.appendRowItems(self.dirPath)
layout.addWidget(self.tableView)
self.resize(1300,500)
def appendRowItems(self, dir_path):
for root, dirs, files in os.walk(dir_path):
if root == dir_path:
for file in files:
item = QtGui.QStandardItem(file)
item.setData(os.path.join(root, file))
self.model.appendRow(item)
ix = self.model.indexFromItem(item)
self.tableView.setIndexWidget(ix, ViewWidget(1270, ix))
@QtCore.Slot(QtCore.QModelIndex)
def onClick(self, ix):
pass
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
eg = Example()
vw= ViewWidget(1270,QtGui.QStandardItemModel().indexFromItem(QtGui.QStandardItem("./adding_buttons.py")))
eg.show()
sys.exit(app.exec_())
答案 0 :(得分:3)
在这种情况下,最好有几列并在最后一列中设置按钮:
import sys
from PySide import QtGui, QtCore
class ViewWidget(QtGui.QWidget):
def __init__(self, index, parent=None):
super(ViewWidget, self).__init__(parent)
self.p_index = QtCore.QPersistentModelIndex(index)
self.content_button = QtGui.QWidget(self)
self.view_btn = QtGui.QPushButton("View")
self.share_btn = QtGui.QPushButton("Share", clicked=self.share_clicked)
self.delete_btn = QtGui.QPushButton("Delete", clicked=self.delete_clicked)
lay = QtGui.QHBoxLayout(self.content_button)
lay.setContentsMargins(0, 0, 0,0)
lay.addWidget(self.view_btn)
lay.addWidget(self.share_btn)
lay.addWidget(self.delete_btn)
@QtCore.Slot()
def delete_clicked(self):
model = self.p_index.model()
model.removeRow(self.p_index.row())
@QtCore.Slot()
def share_clicked(self):
model = self.p_index.model()
name = model.index(self.p_index.row(), 0).data()
print(name)
class DirView(QtGui.QTableView):
lengthChanged = QtCore.Signal(int)
def __init__(self, directory="", parent=None):
super(DirView, self).__init__(parent)
self.setShowGrid(False)
self._model = QtGui.QStandardItemModel(self)
self._model.setColumnCount(4)
labels = ("filename", "size", "date", "")
self._model.setHorizontalHeaderLabels(labels)
self.setModel(self._model)
self.verticalHeader().hide()
self.horizontalHeader().setStretchLastSection(True)
if directory:
self.load(directory)
@QtCore.Slot(str)
@QtCore.Slot(QtCore.QDir)
def load(self, path):
directory = QtCore.QDir(path)
for i, finfo in enumerate(directory.entryInfoList(QtCore.QDir.Files)):
filename = finfo.fileName()
size = finfo.size()
lastModified = finfo.lastModified().toString()
it_name = QtGui.QStandardItem(finfo.fileName())
it_size = QtGui.QStandardItem(str(finfo.size()))
it_last_modified = QtGui.QStandardItem(finfo.lastModified().toString())
it_buttons = QtGui.QStandardItem()
for j, it in enumerate((it_name, it_size, it_last_modified, it_buttons)):
self._model.setItem(i, j, it)
ix = self._model.indexFromItem(it_buttons)
buttons = ViewWidget(ix)
self.setIndexWidget(ix, buttons)
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.open_file()
def open_file(self):
self.setCentralWidget(QtGui.QWidget())
view = DirView()
dirPath = r"C:\raghava\main project"
view.load(dirPath)
layout = QtGui. QGridLayout(self.centralWidget())
layout.addWidget(view)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
eg = Example()
eg.show()
sys.exit(app.exec_())