Add custom items to QListWidget - python

How can I add customized items to a QListWidget with a background color that I choose, and add a bottom border to each item, like this draft example in the picture below.
This is the code that I wrote:
from PyQt5 import QtWidgets, QtGui
import sys
class CustomListHead(QtWidgets.QWidget):
def __init__(self):
super(CustomListHead, self).__init__()
self.project_title = QtWidgets.QLabel("Today")
self.set_ui()
def set_ui(self):
grid_box = QtWidgets.QGridLayout()
grid_box.addWidget(self.project_title, 0, 0)
self.setLayout(grid_box)
self.show()
class CustomListItem(QtWidgets.QWidget):
def __init__(self):
super(CustomListItem, self).__init__()
self.project_title = QtWidgets.QLabel("Learn Python")
self.task_title = QtWidgets.QLabel("Learn more about forms, models and include")
self.set_ui()
def set_ui(self):
grid_box = QtWidgets.QGridLayout()
grid_box.addWidget(self.project_title, 0, 0)
grid_box.addWidget(self.task_title, 1, 0)
self.setLayout(grid_box)
self.show()
class MainWindowUI(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindowUI, self).__init__()
self.list_widget = QtWidgets.QListWidget()
self.set_ui()
def set_ui(self):
custom_head_item = CustomListHead()
item = QtWidgets.QListWidgetItem(self.list_widget)
item.setSizeHint(custom_head_item.sizeHint())
self.list_widget.setItemWidget(item, custom_head_item)
self.list_widget.addItem(item)
custom_item = CustomListItem()
item = QtWidgets.QListWidgetItem(self.list_widget)
item.setSizeHint(custom_item.sizeHint())
self.list_widget.addItem(item)
self.list_widget.setItemWidget(item, custom_item)
vertical_layout = QtWidgets.QVBoxLayout()
vertical_layout.addWidget(self.list_widget)
widget = QtWidgets.QWidget()
widget.setLayout(vertical_layout)
self.setCentralWidget(widget)
self.show()
app = QtWidgets.QApplication(sys.argv)
ui = MainWindowUI()
sys.exit(app.exec_())

I see you have QListWidgetItem with you.
From documentation you can customize each widget item, customize it and add to your listwidget:
The appearance of the text can be customized with setFont(), setForeground(), and setBackground(). Text in list items can be aligned using the setTextAlignment() function. Tooltips, status tips and "What's This?" help can be added to list items with setToolTip(), setStatusTip(), an
d setWhatsThis().
http://doc.qt.io/qt-5/qlistwidgetitem.html#details

Related

How to create a Drawer instance and attach it to MainWindow

I am struggling to add a side menu to my application.
I have a QMainWindow instance to which I was hoping to add a QDrawer object and achieve an effect similar to this sample.
Unfortunately, it seems that PySide2 only provides QMenu, QTooltip and QDialog widgets which inherit from the Popup class, and QDrawer is nowhere to be found. However, using a Drawer tag in a QML file works just fine. Shouldn't it be possible to also create an instance of QDrawer programmatically?
As another try, I tried to load a Drawer instance from a QML file and attach it to my QMainWindow. Unfortunately I can't quite understand what should I specify as parent, what should I wrap it in, what parameters should I use etc. - any advice would be appreciated (although I would much rather create and configure it programatically).
My goal is to create a QMainWindow with a toolbar, central widget and a QDrawer instance as a side navigation menu (such as in this sample). Can you please share some examples or explain what to do?
One possible solution is to implement a Drawer using Qt Widgets, the main feature is to animate the change of width for example using a QXAnimation, the other task is to set the anchors so that it occupies the necessary height. A simple example is the one shown in the following code:
import os
from PySide2 import QtCore, QtGui, QtWidgets
class Drawer(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedWidth(0)
self.setContentsMargins(0, 0, 0, 0)
# self.setFixedWidth(0)
self._maximum_width = 0
self._animation = QtCore.QPropertyAnimation(self, b"width")
self._animation.setStartValue(0)
self._animation.setDuration(1000)
self._animation.valueChanged.connect(self.setFixedWidth)
self.hide()
#property
def maximum_width(self):
return self._maximum_width
#maximum_width.setter
def maximum_width(self, w):
self._maximum_width = w
self._animation.setEndValue(self.maximum_width)
def open(self):
self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
self._animation.start()
self.show()
def close(self):
self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
self._animation.start()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
self.tool_button = QtWidgets.QToolButton(
checkable=True, iconSize=QtCore.QSize(36, 36)
)
content_widget = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
content_widget.setText("Content")
content_widget.setStyleSheet("background-color: green")
lay = QtWidgets.QVBoxLayout(central_widget)
lay.setSpacing(0)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(self.tool_button)
lay.addWidget(content_widget)
self.resize(640, 480)
self.drawer = Drawer(self)
self.drawer.move(0, self.tool_button.sizeHint().height())
self.drawer.maximum_width = 200
self.drawer.raise_()
content_lay = QtWidgets.QVBoxLayout()
content_lay.setContentsMargins(0, 0, 0, 0)
label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
label.setText("Content\nDrawer")
label.setStyleSheet("background-color: red")
content_lay.addWidget(label)
self.drawer.setLayout(content_lay)
self.tool_button.toggled.connect(self.onToggled)
self.onToggled(self.tool_button.isChecked())
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.onCustomContextMenuRequested)
#QtCore.Slot()
def onCustomContextMenuRequested(self):
menu = QtWidgets.QMenu()
quit_action = menu.addAction(self.tr("Close"))
action = menu.exec_(QtGui.QCursor.pos())
if action == quit_action:
self.close()
#QtCore.Slot(bool)
def onToggled(self, checked):
if checked:
self.tool_button.setIcon(
self.style().standardIcon(QtWidgets.QStyle.SP_MediaStop)
)
self.drawer.open()
else:
self.tool_button.setIcon(
self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay)
)
self.drawer.close()
def resizeEvent(self, event):
self.drawer.setFixedHeight(self.height() - self.drawer.pos().y())
super().resizeEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

How to add widgets dynamically upon selecting an option from QComboBox in Pyqt5

I want to add widgets in GUI when a user selects a particular item from QComboBox.
With the different options in combo-box Pip config, I want GUI to look like as in the following images. In the right image, there are extra widgets present for an item Multi pip. Also I want the location of the extra widgets as shown in the right image.
How to add these widgets dynamically ? Please find the code below.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt, QRect
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
vbox = QVBoxLayout()
CpsLabel = QLabel()
CpsLabel.setText("<font size = 12>Cps</font>")
CpsLabel.setAlignment(Qt.AlignCenter)
CpsLabel.setTextFormat(Qt.RichText)
CpsPipConfigLabel = QLabel('Pip config: ')
CpsPipConfigComboBox = QComboBox()
CpsPipConfigComboBox.addItems(['Single pip', 'Dual pip', 'Multi pip'])
CpsPipConfigComboBox.setCurrentIndex(2)
CpsChannel = QLabel('Cps channel: ')
CpsChannelComboBox = QComboBox()
CpsChannelComboBox.addItems(['A', 'B', 'C', 'D'])
CpsChannelComboBox.setCurrentIndex(0)
CpsTotalTeethLabel = QLabel('Total teeth: ')
CpsTotalTeethEdit = QLineEdit()
CpsTotalTeethEdit.setFixedWidth(50)
CpsTotalTeethEdit.setPlaceholderText('18')
CpsTotalTeethEdit.setValidator(QIntValidator())
CpsMissingTeethLabel = QLabel('Missing teeth: ')
CpsMissingTeethEdit = QLineEdit()
CpsMissingTeethEdit.setFixedWidth(50)
CpsMissingTeethEdit.setPlaceholderText('1')
CpsMissingTeethEdit.setValidator(QIntValidator())
vbox.addWidget(CpsLabel)
vbox.addStretch()
CpsQHBox1 = QHBoxLayout()
CpsQHBox1.setSpacing(0)
CpsQHBox1.addStretch()
CpsQHBox1.addWidget(CpsPipConfigLabel)
CpsQHBox1.addWidget(CpsPipConfigComboBox)
CpsQHBox1.addStretch()
vbox.addLayout(CpsQHBox1)
vbox.addStretch()
CpsQHBox2 = QHBoxLayout()
CpsQHBox2.setSpacing(0)
CpsQHBox2.addStretch()
CpsQHBox2.addSpacing(20)
CpsQHBox2.addWidget(CpsTotalTeethLabel)
CpsQHBox2.addWidget(CpsTotalTeethEdit)
CpsQHBox2.addStretch()
CpsQHBox2.addWidget(CpsMissingTeethLabel)
CpsQHBox2.addWidget(CpsMissingTeethEdit)
CpsQHBox2.addStretch()
vbox.addLayout(CpsQHBox2)
vbox.addStretch()
CpsQHBox3 = QHBoxLayout()
CpsQHBox3.setSpacing(0)
CpsQHBox3.addStretch()
CpsQHBox3.addWidget(CpsChannel)
CpsQHBox3.addWidget(CpsChannelComboBox)
CpsQHBox3.addStretch()
vbox.addLayout(CpsQHBox3)
vbox.addStretch()
self.setLayout(vbox)
self.setGeometry(200, 100, 300, 300)
self.setWindowTitle('Steady state data processing')
self.setWindowIcon(QIcon('duty_vs_suction_map_sum.png'))
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), QColor(255,250,100))
# p.setColor(self.backgroundRole(), Qt.blue)
self.setPalette(p)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I suggest you set the widgets up and place them at the beginning like you have them, but set them invisible. Then make a method that sets the appropriate widgets visible based on the qcombobox's current text and connect it to the qcombobox's activated signal.
You will also need to add self in front of almost every object so that it can be referred to from other methods.
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# setup code here...
self.CpsTotalTeethEdit.setVisible(False)
self.CpsTotalTeethLabel.setVisible(False)
self.CpsPipConfigComboBox.activated.connect(self.setup_total_teeth)
self.show()
def setup_widgets(self):
if self.CpsPipConfigComboBox.currentText() == "Multi pip":
self.CpsTotalTeethLabel.setVisible(True)
self.CpsTotalTeethEdit.setVisible(True)
By setting the items invisible instead of adding them with this method, you can also set them to be not visible when the cobobox's position is not for them.

Pyqt4 Qcombobox signal is not firing upon user input but it does when done with .setCurrentIndex

The QComboBox currentIndexChanged Signal is not firing when a new item is selected from user. But it does fire when .setCurrentIndex is used within the code. (line 91 and 92).
I have a QTabWidget. In tab1 I have a Qvbox into which three Qhboxes are added. Each Qhbox is from the class Kaskade and contains two widgets, a QCombobox and a QstackedWidget. Depending of the current Index of the QCombobox the QstackWidget will either show a QLCD number or a Qspinbox.
If the user changes the QCombobox index in the GUI the currentIndexChanged Signal is not emitted, although the QCombobox shows the new item.
What am I missing? Any kind of help is appreciated.
This is my test code
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
class Kaskade(QtGui.QWidget):
def __init__(self,sp,sp_min,sp_max, parent = None):
super(Kaskade, self).__init__(parent)
self._sp=sp
self._sp_min=sp_min
self._sp_max=sp_max
self.sp()
self.hbox_gen()
def mode_changed(self,i):
print "Mode has changed to", i
self.sp_stack.setCurrentIndex(i)
def sp(self):
self.sp_stack=QtGui.QStackedWidget(self)
self.sp1 = QtGui.QWidget()
self.sp2 = QtGui.QWidget()
self.sp1UI()
self.sp2UI()
self.sp_stack.addWidget(self.sp1)
self.sp_stack.addWidget(self.sp2)
def sp1UI(self):
self.sp1_layout=QtGui.QHBoxLayout()
self.sp1_lcd=QtGui.QLCDNumber(self)
self.sp1_layout.addWidget(self.sp1_lcd)
#self.sp1.connect(lcd_pv.display)
self.sp1.setLayout(self.sp1_layout)
def sp2UI(self):
self.sp2_layout=QtGui.QHBoxLayout()
self.sp2_spinBox=QtGui.QSpinBox()
self.sp2_spinBox.setRange(self._sp_min,self._sp_max)
self.sp2_spinBox.setValue(self._sp)
self.sp2_layout.addWidget(self.sp2_spinBox)
self.sp2.setLayout(self.sp2_layout)
def hbox_gen(self):
self.mode=QtGui.QComboBox(self)
self.mode.addItem("OFF")
self.mode.addItem("ON")
self.mode.currentIndexChanged.connect(self.mode_changed)
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.mode)
self.hbox.addWidget(self.sp_stack)
class tabdemo(QtGui.QTabWidget):
def __init__(self, parent = None):
super(tabdemo, self).__init__(parent)
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.tab3 = QtGui.QWidget()
self.addTab(self.tab1,"Tab 1")
self.addTab(self.tab2,"Tab 2")
self.addTab(self.tab3,"Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("Heizung")
def tab1UI(self):
K1=Kaskade(28,5,40)
K2=Kaskade(30,5,40)
K3=Kaskade(35,5,40)
K1.mode.setCurrentIndex(1)
K3.mode.setCurrentIndex(1)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(K1.hbox)
vbox.addLayout(K2.hbox)
vbox.addLayout(K3.hbox)
self.tab1.setLayout(vbox)
self.setTabText(1,"Tab1")
def tab2UI(self):
self.setTabText(1,"Tab2")
def tab3UI(self):
self.setTabText(2,"Tab3")
def main():
app = QtGui.QApplication(sys.argv)
ex = tabdemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You must add Kaskade to vbox in Tab 1, not the layout. In addition we must do self.hbox layout of Kaskade:
class Kaskade(QtGui.QWidget):
[...]
def hbox_gen(self):
[...]
self.hbox = QtGui.QHBoxLayout(self)
[...]
class tabdemo(QtGui.QTabWidget):
[...]
def tab1UI(self):
[...]
vbox = QtGui.QVBoxLayout()
vbox.addWidget(K1)
vbox.addWidget(K2)
vbox.addWidget(K3)
self.tab1.setLayout(vbox)
[...]

Cannot get ListItems with custom widgets to display

I'm having issues getting items with custom widgets to show up in a list widget. The items show up blank in the example below...
from PySide2 import QtWidgets
class ItemWidget(QtWidgets.QWidget):
def __init__(self,parent = None):
super(ItemWidget, self).__init__(parent)
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
self.checkBox = QtWidgets.QCheckBox()
self.label = QtWidgets.QLabel('test')
layout.addWidget(self.checkBox)
layout.addWidget(self.label)
class ListWidget(QtWidgets.QListWidget):
def __init__(self,parent = None):
super(ListWidget,self).__init__(parent)
self.initUI()
def initUI(self):
for i in range(10):
item = QtWidgets.QListWidgetItem()
self.addItem(item)
widget = ItemWidget(self)
self.setItemWidget(item,widget)
self.show()
lister = ListWidget()
It looks like QlistWidget won't do what you want, so you'll need to approach it from a lower level.
PySide.QtGui.QListWidget.setItemWidget(item, widget)
This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use PySide.QtGui.QListView and subclass PySide.QtGui.QItemDelegate instead.

What happens with QMainWindow

I want to create the GUI with this code. When i click Add New Object Button, it will show the pop up (I use QMainWindown) but i want to put the QLabel in here, it can not work
I dont know why.i hope everyone can give me more some advices. Thanks you
This is my code :
from PySide import QtCore, QtGui
import sys
app = QtGui.QApplication.instance()
if not app:
app = QtGui.QApplication([])
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addWidget(self.First(), 0, 0, 2, 0)
self.setLayout(self.mainLayout)
self.setWindowTitle("Library")
self.resize(700,660)
#----------------------------------------FIRST COLUMN-------------------------------------
def First(self):
FirstFrame = QtGui.QFrame()
FirstFrame.setFixedSize(230,700)
# LABEL
renderer_lb = QtGui.QLabel("Renderer :")
folders_lb = QtGui.QLabel("Folder :")
#COMBOBOX
self.renderer_cbx = QtGui.QComboBox()
self.renderer_cbx.addItem("Vray")
self.renderer_cbx.addItem("Octane")
# LIST VIEW FOLDER
self.folders_lv = QtGui.QListView()
# BUTTON
addnewobject_btn = QtGui.QPushButton("Add New Objects")
newset_btn = QtGui.QPushButton("New Set")
# DEFINE THE FUNCTION FOR FIRST FRAME
Firstbox = QtGui.QGridLayout()
Firstbox.addWidget(renderer_lb,0,0)
Firstbox.addWidget(folders_lb,2,0,1,4)
Firstbox.addWidget(self.renderer_cbx,0,1,1,3)
Firstbox.addWidget(self.folders_lv,3,0,1,4)
Firstbox.addWidget(addnewobject_btn,4,0,1,2)
Firstbox.addWidget(newset_btn,4,3)
Firstbox.setColumnStretch(1, 1)
FirstFrame.setLayout(Firstbox)
addnewobject_btn.clicked.connect(self.addnewobject)
return FirstFrame
def addnewobject(self):
window = QtGui.QMainWindow(self)
window.setWindowTitle('Select folder of new objects')
window.setFixedSize(450,90)
window.show()
folder_lb = QtGui.QLabel("Folder : ")
browser = QtGui.QGridLayout()
browser.addWidget(folder_lb,0,0)
window.setLayout(browser)
if __name__ == '__main__':
window = Window()
sys.exit(window.exec_())
Just as you did in the First() function, you could create an homemade widget using QFrame. Then you can set a central widget for your new window.
from PySide import QtCore, QtGui
import sys
app = QtGui.QApplication.instance()
if not app:
app = QtGui.QApplication([])
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.mainLayout = QtGui.QGridLayout()
self.mainLayout.addWidget(self.First(), 0, 0, 2, 0)
self.setLayout(self.mainLayout)
self.setWindowTitle("Library")
self.resize(700,660)
self.show()
#----------------------------------------FIRST COLUMN-------------------------------------
def First(self):
FirstFrame = QtGui.QFrame()
FirstFrame.setFixedSize(230,700)
# LABEL
renderer_lb = QtGui.QLabel("Renderer :")
folders_lb = QtGui.QLabel("Folder :")
#COMBOBOX
self.renderer_cbx = QtGui.QComboBox()
self.renderer_cbx.addItem("Vray")
self.renderer_cbx.addItem("Octane")
# LIST VIEW FOLDER
self.folders_lv = QtGui.QListView()
# BUTTON
addnewobject_btn = QtGui.QPushButton("Add New Objects")
newset_btn = QtGui.QPushButton("New Set")
# DEFINE THE FUNCTION FOR FIRST FRAME
Firstbox = QtGui.QGridLayout()
Firstbox.addWidget(renderer_lb,0,0)
Firstbox.addWidget(folders_lb,2,0,1,4)
Firstbox.addWidget(self.renderer_cbx,0,1,1,3)
Firstbox.addWidget(self.folders_lv,3,0,1,4)
Firstbox.addWidget(addnewobject_btn,4,0,1,2)
Firstbox.addWidget(newset_btn,4,3)
Firstbox.setColumnStretch(1, 1)
FirstFrame.setLayout(Firstbox)
addnewobject_btn.clicked.connect(self.addnewobject)
return FirstFrame
def addnewobject(self):
secondFrame = QtGui.QFrame()
secondFrame.setFixedSize(230,700)
# LABEL
folders_lb = QtGui.QLabel("Folder :")
# DEFINE THE FUNCTION FOR FIRST FRAME
secondGridLayout = QtGui.QGridLayout()
secondGridLayout.addWidget(folders_lb,2,0,1,4)
secondGridLayout.setColumnStretch(1, 1)
secondFrame.setLayout(secondGridLayout)
window = QtGui.QMainWindow(self)
window.setWindowTitle('Select folder of new objects')
window.setFixedSize(600,700)
window.setCentralWidget(secondFrame) # Here is the main change: setLayout(QLayout) to setCentralWidget(QWidget)
window.show()
if __name__ == '__main__':
window = Window()
sys.exit(window.exec_())
Is this intended for Maya? If yes, I recommand you not to use modal windows as it will quickly fed up the users.

Categories