Issue
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_())
Solution
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()
Answered By - Heike
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.