-2

Want to create a custom widget with QListWidget, Qlabels and QCombo box.

In my code, contains one QListwidget,

three labels to display the number of items in QListWidget, First one for total available items in QListWidget, the second one is on filter condition (item starts) and the third one is also to display the number of items for the filter(Match Contains).

And one combo box for setting the QListWidget default view. How to make it.

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

items = ["item001","item002","item003","item004","item005","001item","002item","new001item","new003item"]

class CustomWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Cutom Widget")

        self.txtbox = QLineEdit()
        self.lstbox = QListWidget()
        self.lstbox.clicked.connect(self.select_item)
        self.lbl_Total = QLabel("Total Available Items :")
        self.lbl_start = QLabel("Item Starts with :")
        self.lbl_contain = QLabel("Items Contains With:")
        self.lbl_Total_count = QLabel("99,999")
        self.lbl_start_count = QLabel("99,999")
        self.lbl_contain_count = QLabel("99,999")
        self.combox = QComboBox()
        self.combox.addItem("Item Starts")
        self.combox.addItem("Item Contains")
        self.combox.addItem("Item Ends")
        self.lbl_combo_deatils = QLabel("Default View :")

        self.lstbox.addItems(items)
        total_item = self.lstbox.count()
        self.lbl_Total_count.setText(str(total_item))

        self.vbox = QVBoxLayout()
        self.vbox.addSpacing(4)
        self.vbox.setAlignment(Qt.AlignCenter)
        self.vbox.setContentsMargins(0,0,0,0)
        self.fbox = QFormLayout()

        self.fbox.addRow(self.lbl_Total,self.lbl_Total_count)
        self.fbox.addRow(self.lbl_start,self.lbl_start_count)
        self.fbox.addRow(self.lbl_contain,self.lbl_contain_count)
        self.fbox.addRow(self.lbl_combo_deatils,self.combox)

        self.vbox.addWidget(self.lstbox)
        self.vbox.addLayout(self.fbox)


        self.hbox = QHBoxLayout()
        self.hbox.addWidget(self.txtbox)
        self.hbox.setAlignment(Qt.AlignTop)
        self.hbox.addStretch(10)

        self.hbox.addLayout(self.vbox)
        self.setLayout(self.hbox)

    def select_item(self):
        self.txtbox.setText(self.lstbox.currentItem().text())


if __name__ =='__main__':
    app = QApplication(sys.argv)
    test = CustomWidget()
    test.show()
    sys.exit(app.exec_())
Bala
  • 648
  • 5
  • 15
  • what is the problem? – eyllanesc Jun 06 '21 at 03:05
  • want to use the same blog in so many places. so if it in a single custom widget it's easy to use. So I try to create, but I have no workable ideas.@eyllanesc – Bala Jun 06 '21 at 03:07
  • I don't understand you, 1) you point out that you want something: great, 2) you don't try anything: that's the bad, the GUI code is trivial in this case. I am pointing this out because it seems as if you were saying: I have this code that creates the GUI but I want you to implement the logic and that is not what we expect in SO. We hope the OP will investigate and show their investigation but you don't do anything about it. You seem to say: give me code. – eyllanesc Jun 06 '21 at 03:12
  • I have no ideas, so I am struck up, I don't expect code, just give some tips on how to make it, I will try, If I am failed and not able to do then, I will submit my request with code. @eyllanesc – Bala Jun 06 '21 at 03:16
  • Basic idea: let's say you have the i-th item, how do you know that it meets the requirement you indicate? So what you should do is implement the filter that you indicate and apply them to each item in a for-loop, and count how many meet that filter. – eyllanesc Jun 06 '21 at 03:20
  • Here the problem has nothing to do with PyQt5 but with verifying how many elements of a list of strings meet a certain condition: `number_of_items_by_filter = len([item for item in list_of_items if filter(item)])`, some like https://stackoverflow.com/questions/61326041/filter-a-list-according-to-condition-in-python – eyllanesc Jun 06 '21 at 03:21
  • I have achieved, all those items like filters, keypress, events,stylesheets, etc.(400 line code) Now My problem is how to make as a single custom widget. how to use the same Widgets, GUI, layouts, events, signals in various places. @eyllanesc – Bala Jun 06 '21 at 03:28
  • I use this code to filter the datas and count the total number with len function............................................ item_normal = self.listbox_dummy.findItems("*", Qt.MatchWildcard)................ item_startswith = self.listbox_dummy.findItems(search_text, t.MatchStartsWith).............. item_contains = self.listbox_dummy.findItems(search_text, Qt.MatchContains)............... item_endswith = self.listbox_dummy.findItems(search_text, t.MatchEndsWith)............... – Bala Jun 06 '21 at 03:31
  • 1) You already have the widget, what is missing is to implement a filtering logic that has nothing to do with the GUI, that is your business logic. 2) We are not interested in whether you have worked a lot on your project, we are interested in what you have worked to solve your problem, and at the moment I do not see anything of it. – eyllanesc Jun 06 '21 at 03:39
  • 3) In your last comment is the solution, what is the problem with your attempted solution? – eyllanesc Jun 06 '21 at 03:41
  • No sir, My Problem How to use this same code in various places. So My idea if I create all things as a single custom widget ( include, Qlistlistwidget, labels and combo box with events and signals ) . if I create like that, then simply I use all things with a single line code (instances) – Bala Jun 06 '21 at 03:45
  • Okay, but it would be nice if you indicate that you have already implemented (the findItems code). On the other hand, I still have a question: what is the QComboBox for? – eyllanesc Jun 06 '21 at 03:48
  • Combo box is used to set the default view at the time of filter, if my combo box selection is "Item contains" then listbox will disply iems contins property as default,(without pressing any shortcut keys). as my selection is "item Starts wtih" , then QlistWidget disply will be starts with items ...l@eyllansec – Bala Jun 06 '21 at 03:52
  • The solution is: connect a function to the textChanged signal where you put the findItems code and then add: `self.lbl_start.setNum(len(item_startswith))` – eyllanesc Jun 06 '21 at 03:55
  • @eyllanesc. attach my program picture, to my question. My problem is how to create whole things as a single custom widget and how to create instance ....,like https://stackoverflow.com/questions/57299933/creating-a-complex-custom-widget-in-pyqt5-and-adding-it-to-a-qgridlayout – Bala Jun 06 '21 at 04:06
  • My question is very clear, How to create a custom widget (using Qlistwidget,labels and Qcombo box) . any how Thankyou for your valuable time@eyllanesc – Bala Jun 06 '21 at 04:19
  • No, your question is not clear. You have the class definition, you claim you have implemented the filter logic, so creating a custom widget would be just a matter of `my_widget = CustomWidget()`. It's unclear to me what you think is still missing. – Heike Jun 07 '21 at 05:45
  • @Heike... I want to use the same layout as Gui in many places. so how to create an instance for that layout. (pls don't consider filters, events, signals, etc). Just tell how to create an instance for these total layouts – Bala Jun 07 '21 at 08:23

1 Answers1

0

I'm struggling to understand what the actual problem is in this case. You already have the custom class. If you want multiple copies of the custom widget, just create multiple instances, i.e.

if __name__ =='__main__':
    app = QApplication(sys.argv)
    main_widget = QWidget()
    layout = QGridLayout(main_widget)
    for row in range(2):
        for column in range(4):
            widget = CustomWidget()
            layout.addWidget(widget, row, column)
    main_widget.show()
    app.exec()
Heike
  • 24,102
  • 2
  • 31
  • 45