简体   繁体   English

PyQt5 删除按钮

[英]PyQt5 removing button

In the list_widget I have added a add button I also want to add a remove button which asks which item you wants to remove and remove the chosen item.在 list_widget 中,我添加了一个添加按钮,我还想添加一个删除按钮,询问您要删除哪个项目并删除所选项目。 I was trying it to do but I didn't had any idea to do so .Also, please explain the solution I am a beginner with pyqt5 or I'd like to say absolute beginner.我正在尝试这样做,但我不知道这样做。另外,请解释解决方案我是pyqt5的初学者,或者我想说绝对的初学者。

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow, 
QListWidget, QListWidgetItem
import sys

class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.x = 200
        self.y = 200
        self.width = 500
        self.length = 500
        self.setGeometry(self.x, self.y, self.width, 
        self.length)
        self.setWindowTitle("Stock managment")

        self.iniTUI()

    def iniTUI(self):
        # buttons
        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("+")
        self.b1.move(450, 100)
        self.b1.resize(50, 25)
        self.b1.clicked.connect(self.take_inputs)
        # This is the button I want to define.
        self.btn_minus = QtWidgets.QPushButton(self)
        self.btn_minus.setText("-")
        self.btn_minus.move(0, 100)
        self.btn_minus.resize(50, 25)


        # list
        self.list_widget = QListWidget(self)

        self.list_widget.setGeometry(120, 100, 250, 300)

        self.item1 = QListWidgetItem("A")
        self.item2 = QListWidgetItem("B")
        self.item3 = QListWidgetItem("C")

        self.list_widget.addItem(self.item1)
        self.list_widget.addItem(self.item2)
        self.list_widget.addItem(self.item3)

        self.list_widget.setCurrentItem(self.item2)



    def take_inputs(self):
        self.name, self.done1 = 
 QtWidgets.QInputDialog.getText(
            self, 'Add Item to List', 'Enter The Item you want 
in 
            the list:')
        self.roll, self.done2 = QtWidgets.QInputDialog.getInt(
            self, f'Quantity of {str(self.name)}', f'Enter 
            Quantity of {str(self.name)}:')

        if self.done1 and self.done2:
            self.item4 = QListWidgetItem(f"{str(self.name)}              
            Quantity{self.roll}")
            self.list_widget.addItem(self.item4)
            self.list_widget.setCurrentItem(self.item4)
               

    def clicked(self):
        self.label.setText("You clicked the button")
        self.update()

    def update(self):
        self.label.adjustSize()


def clicked():
    print("meow")
def window():
    apk = QApplication(sys.argv)
    win = MyWindow()





    win.show()
    sys.exit(apk.exec_())
window()

The core issue here is the lack of separation of the view and the data .这里的核心问题是缺乏视图数据的分离。 This makes it very hard to reason about how to work with graphical elements.这使得很难推理如何使用图形元素。 You will almost certainly want to follow the Model View Controller design paradigm https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller which offers a systematic way to handle this separation.您几乎肯定会想要遵循模型视图控制器设计范式https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller ,它提供了一种处理这种分离的系统方法。

Once you do so, it immediately becomes very straight forward how to proceed with the question: You essentially just have a list, and you either want to add a thing to this list, or remove one based on a selection.一旦你这样做了,如何继续这个问题就会变得非常简单:你本质上只有一个列表,你要么想在这个列表中添加一个东西,要么根据选择删除一个。

I include an example here which happens to use the built-in classes QStringListModel and QListView in Qt5, but it is simple to write your own more specialized widgets and models.我在这里包含一个例子,它碰巧使用了 Qt5 中的内置类 QStringListModel 和 QListView,但是编写自己的更专业的小部件和模型很简单。 They all just use a simple signal to emit to the view that it needs to refresh the active information.它们都只是使用一个简单的信号向视图发出它需要刷新活动信息的信号。

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys

class StuffViewer(QMainWindow):
    def __init__(self, model):
        super().__init__()
        self.setWindowTitle("Stock managment")

        # 1: Use layouts.
        hbox = QHBoxLayout()
        widget = QWidget()
        widget.setLayout(hbox)
        self.setCentralWidget(widget)

        # 2: Don't needlessly store things in "self"
        vbox = QVBoxLayout()
        add = QPushButton("+")
        add.clicked.connect(self.add_new_stuff)
        vbox.addWidget(add)
        sub = QPushButton("-")
        sub.clicked.connect(self.remove_selected_stuff)
        vbox.addWidget(sub)
        vbox.addStretch(1)
        hbox.addLayout(vbox)

        # 3: Separate the view of the data from the data itself. Use Model-View-Controller design to achieve this.
        self.model = model
        self.stuffview = QListView()
        self.stuffview.setModel(self.model)
        hbox.addWidget(self.stuffview)

    def add_new_stuff(self):
        new_stuff, success = QInputDialog.getText(self, 'Add stuff', 'Enter new stuff you want')
        if success:
            self.stuff.setStringList(self.stuff.stringList() + [new_stuff])

    def remove_selected_stuff(self):
        index = self.stuffview.currentIndex()
        all_stuff = self.stuff.stringList()
        del all_stuff[index.column()]
        self.stuff.setStringList(all_stuff)


def window():
    apk = QApplication(sys.argv)
    # Data is clearly separated:
    # 4: Never enumerate variables! Use lists!
    stuff = QStringListModel(["Foo", "Bar", "Baz"])
    # The graphical components is just how you interface with the data with the user!
    win = StuffViewer(stuff)
    win.show()
    sys.exit(apk.exec_())

window()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM