pspnash 发表于 2022-4-12 01:10:06

花了两三天没有搞出来想要的效果。求求大家帮帮小白

花了两三天时间在学习这个效果,一直搞不出来,求大神们帮帮小白。

1.我的想法;第一步:点击打开按钮,就可以导入文件夹的文件到listView中显示,同时文件夹的路径显示在line text里。
                第二步:选中listView的项目,点击删除按钮,将其删除。

2.碰到的难点:1.在第一步从文件夹中导入,不会设置过滤只导入文件进来;
                        2.第二步,我想的是选中后才可以删除,
                        现在做出来的效果,选中后单击可以删除,不选中,单击删除也可以删除。

UI代码:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainView1.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
      Form.setObjectName("Form")
      Form.resize(1172, 904)
      self.verticalLayout = QtWidgets.QVBoxLayout(Form)
      self.verticalLayout.setContentsMargins(200, 200, 200, 200)
      self.verticalLayout.setSpacing(0)
      self.verticalLayout.setObjectName("verticalLayout")
      self.gridLayout = QtWidgets.QGridLayout()
      self.gridLayout.setContentsMargins(-1, 20, -1, 50)
      self.gridLayout.setHorizontalSpacing(30)
      self.gridLayout.setVerticalSpacing(50)
      self.gridLayout.setObjectName("gridLayout")
      self.pushButton = QtWidgets.QPushButton(Form)
      self.pushButton.setObjectName("pushButton")
      self.gridLayout.addWidget(self.pushButton, 0, 1, 1, 1)
      self.lineEdit = QtWidgets.QLineEdit(Form)
      self.lineEdit.setObjectName("lineEdit")
      self.gridLayout.addWidget(self.lineEdit, 0, 0, 1, 1)
      self.listView = QtWidgets.QListView(Form)
      self.listView.setObjectName("listView")
      self.gridLayout.addWidget(self.listView, 1, 0, 1, 1)
      self.pushButton_2 = QtWidgets.QPushButton(Form)
      self.pushButton_2.setObjectName("pushButton_2")
      self.gridLayout.addWidget(self.pushButton_2, 1, 1, 1, 1, QtCore.Qt.AlignTop)
      self.gridLayout.setColumnStretch(0, 6)
      self.verticalLayout.addLayout(self.gridLayout)

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

    def retranslateUi(self, Form):
      _translate = QtCore.QCoreApplication.translate
      Form.setWindowTitle(_translate("Form", "Form"))
      self.pushButton.setText(_translate("Form", "打开"))
      self.pushButton_2.setText(_translate("Form", "删除"))


逻辑代码:
# -*- encoding:utf-8 -*-
import os,sys

from Pratics.UiView.MainView1 import Ui_Form
from PyQt5.QtWidgets import QWidget,QFileDialog,QMainWindow,QApplication
from PyQt5.QtCore import QStringListModel

class Function(QWidget,Ui_Form):
    def __init__(self):
      super(Function, self).__init__()
      self.setupUi(self)
      self.pushButton.clicked.connect(self.openfiles)
      self.listView.clicked.connect(self.currentIndex)
      self.pushButton_2.clicked.connect(self.deleteItem)
      
    def openfiles(self):
      dir_path=QFileDialog.getExistingDirectory(None,"选择文件",os.getcwd())
      self.lineEdit.setText(dir_path)         #路径写入到文本框内
      self.list =os.listdir(dir_path) #获取路径下面文件列表
      self.listmodel =QStringListModel()
      self.listmodel.setStringList(self.list)
      self.listView.setModel(self.listmodel)

    def currentIndex(self):
      self.a=self.listView.currentIndex().row()#获取当前项目行号
      print("bbb")
    def deleteItem(self):
      self.listmodel.removeRow(self.a)
if __name__=="__main__":
    app =QApplication(sys.argv)
    win =Function()
    win.show()
    sys.exit(app.exec_())

请大家给我指导一下如何实现我的两个想法。。。


页: [1]
查看完整版本: 花了两三天没有搞出来想要的效果。求求大家帮帮小白