pyqt4 customize qlistwidget item - pyqt4

Im trying to customize qlistwidgetitem but there is a overlap between the text and the button.
Here is my code.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class myListWidget(QListWidget):
def Clicked(self, item):
QMessageBox.information(self, "ListWidget", "You clicked: "+item.text())
def main():
app = QApplication(sys.argv)
listWidget = myListWidget()
#Resize width and height
listWidget.resize(300,120)
l = QListWidgetItem()
l.setText('Im an item')
l.setFlags(l.flags() | Qt.ItemIsUserCheckable)
l.setCheckState(Qt.Checked)
button = QCheckBox('Hello')
l.setSizeHint(button.minimumSizeHint())
listWidget.addItem(l)
listWidget.setItemWidget(l, button)
listWidget.setWindowTitle('PyQT QListwidget Demo')
listWidget.itemClicked.connect(listWidget.Clicked)
listWidget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Reqt:-

Related

PyQt5: QLineEdit doesn't show inside a QGroupBox

I'm creating a PyQt5 application in which I want to put some of QLineEdit widgets inside a QGroupBox.
When I run my application, GroupBox is visible and LineEdit is not.
In CreateLinesEdit() I commented a line which sets the line edit visible, but it opens lineEdit in a new window.
from PyQt5.QtWidgets import QGroupBox, QApplication, QLineEdit, QVBoxLayout, QWidget, QHBoxLayout
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.InitWindow()
def InitWindow(self):
self.BoxLayout()
self.AddBox()
self.CreateLinesEdit()
self.show()
def BoxLayout(self):
self.groupBoxScreen = QGroupBox()
self.vbox = QVBoxLayout()
self.vbox_screenGame = QVBoxLayout()
self.hbox_lineEdit = QHBoxLayout()
def AddBox(self):
self.vbox_screenGame.addItem(self.hbox_lineEdit)
self.groupBoxScreen.setLayout(self.vbox_screenGame)
self.vbox.addWidget(self.groupBoxScreen)
self.setLayout(self.vbox)
def CreateLinesEdit(self):
self.lines_edit = []
for i in range(0, 4):
LineEdit = QLineEdit()
# LineEdit.setVisible(True)
self.lines_edit.append(LineEdit)
self.hbox_lineEdit.addWidget(self.lines_edit[i])
if __name__ == "__main__":
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

PyQt5 keep window on top by clicking button

I am trying to keep window by a Pin button. Here is the code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QPushButton
from PyQt5 import QtCore
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Keep Going'
self.width = 480
self.height = 360
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setFixedSize(self.width, self.height)
self.layout = QVBoxLayout()
self.button = QPushButton('PinTop')
self.button.setCheckable(True)
self.button.clicked.connect(self.winPinTop)
self.layout.addWidget(self.button)
# add tabs to widget
self.setLayout(self.layout)
self.show()
def winPinTop(self):
print('Pin')
button = self.sender()
if button.isChecked():
print('on top')
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.show()
else:
print('no top')
self.setWindowFlags(self.windowFlags())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
The window can not keep on top after clicking the button. I don't know how to fix this.
Infomation may help:
Python 3.6.8
PyQt version: 5.13.0
Ubuntu 19.04
UPDATE
I replace QWidget with QMainWindow class to check this bug.
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication, QWidget, QVBoxLayout, QPushButton, QPushButton
from PyQt5 import QtCore
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Keep Going'
self.width = 480
self.height = 360
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
win = QWidget()
win.setFixedSize(self.width, self.height)
layout = QVBoxLayout()
button = QPushButton('PinTop')
button.setCheckable(True)
button.clicked.connect(self.winPinTop)
layout.addWidget(button)
# add tabs to widget
win.setLayout(layout)
self.setCentralWidget(win)
self.show()
def winPinTop(self):
print('Pin')
button = self.sender()
if button.isChecked():
print('on top')
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.Dialog )
print(self.windowFlags())
self.show()
else:
print('no top')
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint )
print(self.windowFlags())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
The problem is still here.
UPDATE 2019-9-11
After remove the show function. I can get the window on top.
def winPinTop(self):
print('Pin')
button = self.sender()
if button.isChecked():
print('on top')
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.Dialog )
print(self.windowFlags())
#self.show()
else:
print('no top')
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint )
print(self.windowFlags())
#self.show()
But the window will close after I click the pin button. When I show this window, it stays on the top.
I don't know why this happened.
Reference: PyQt: Always on top

Display a specific item in QcomboBox at start up

I have a QcomboBox with some items in it. When Widget starts up and display first item from QcomboBox. How could QcomboBox be forced to display third item ( Index(2)) in the list by start up?
from PyQt5 import QtWidgets, QtGui
class combo(QtWidgets.QWidget):
def __init__(self, parent = None):
super(combo, self).__init__(parent)
layout = QtWidgets.QHBoxLayout(self)
self.cb = QtWidgets.QComboBox()
self.cb.addItems(["1", "2", "3","4"])
layout.addWidget(self.cb)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
ex = combo()
ex.show()
sys.exit(app.exec_())
The current item can be set with setCurrentIndex().
from PyQt5 import QtWidgets, QtGui
class combo(QtWidgets.QWidget):
def __init__(self, parent = None):
super(combo, self).__init__(parent)
layout = QtWidgets.QHBoxLayout(self)
self.cb = QtWidgets.QComboBox()
self.cb.addItems(["1", "2", "3","4"])
self.cb.setCurrentIndex(2) # <---
layout.addWidget(self.cb)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
ex = combo()
ex.show()
sys.exit(app.exec_())

QGraphicsview + scene + QGroupBox movement issue

For past few days I was trying to solve the issue of widget movement. At some point I tried rewriting QComboBox classes with mouse signals but that did not work. As a work around I settled for parenting my widget to a QGraphicsWidget but once I try to add another item it does not display any more and I'm not sure what to do. Here is full test script:
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QApplication,QGraphicsItem, QGraphicsView, QGraphicsScene, QDesktopWidget, QCheckBox, QGroupBox, QPushButton, QGridLayout, QLabel, QLineEdit, QComboBox, QFont, QRadioButton, QButtonGroup, QWidget, QShortcut, QKeySequence, QIcon, QListView, QStandardItemModel, QStandardItem, QAction, QIntValidator, QListWidget, QProgressBar, QSpacerItem
from PyQt4.QtCore import QRect
from functools import partial
import sys
class node_GUI(QtGui.QWidget):
def __init__(self):
super(node_GUI, self).__init__()
class Main(QtGui.QMainWindow):
def __init__(self, *args):
super(Main, self).__init__(*args)#QtGui.QMainWindow.__init__(self)
self.init_defaults()
def init_defaults(self):
self.setGeometry(800,800,500,200)
self.lay_main = QGridLayout()
self.centralwidget = QtGui.QWidget()
self.centralwidget.setLayout(self.lay_main)
self.setCentralWidget(self.centralwidget)
btn_create_node = QPushButton("Create Node View")
btn_create_node.clicked.connect(self.create_node_view)
self.lay_main.addWidget(btn_create_node)
def showWindow(self,window):
window.show()
def printTest(self):
print "Start"
box = QGroupBox("subWidget")
box_btn = QPushButton("Test")
box_btn.clicked.connect(self.printTest)
le_edit = QLineEdit()
lay = QGridLayout()
box.setLayout(lay)
lay.addWidget(box_btn)
lay.addWidget(le_edit)
area = QtGui.QGraphicsWidget()
area.setMinimumSize(QtCore.QSizeF(400,300))
area.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
area.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
proxy = self.scene.addWidget(box)
proxy.setParentItem(area)
print "END"
def create_node_view(self):
print "creting node view"
window = node_GUI()
window.setGeometry(QRect(100, 100, 400, 200))
window.setWindowTitle("node ")
window.setObjectName("node")
show_window = QPushButton("Show Node Editor")
show_window.setObjectName("btn")
show_window.clicked.connect(partial(self.showWindow,window))
self.lay_main.addWidget(show_window)
box = QGroupBox("Widgets")
box_btn = QPushButton("Test")
box_btn.clicked.connect(self.printTest)
le_edit = QLineEdit()
lay = QGridLayout()
box.setLayout(lay)
lay.addWidget(box_btn)
lay.addWidget(le_edit)
area = QtGui.QGraphicsWidget()
area.setMinimumSize(QtCore.QSizeF(300,300))
area.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
area.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
area.setAutoFillBackground(True)
ecs = QtGui.QGraphicsEllipseItem()
ecs.setRect(QtCore.QRectF(79,79,79,79))
ecs.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
ecs.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
view = QGraphicsView()
self.scene = QGraphicsScene()
self.scene.addItem(area)
proxy = self.scene.addWidget(box)
proxy.setParentItem(area)
self.scene.addItem(ecs)
view.setScene(self.scene)
lay_window = QGridLayout()
window.setLayout(lay_window)
lay_window.addWidget(view)
def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
When you click on Create Node View > Show Node Editor > Test button > a new GroupBox should appear but that does not work. Not sure why.
Right so I stopped using QGraphicsWidget() and instead I just use QGraphicsRectItem(ecs for example) once I did that change everything started to work as expected.

pyqt: New form does not show up when click button

This is my program:
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
class test(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
mainbox = QtGui.QVBoxLayout()
self.setLayout(mainbox)
btn = QtGui.QPushButton("open another panel")
mainbox.addWidget(btn)
btn.clicked.connect(self.onBtnClick)
def onBtnClick(self):
print "onBtnClick"
w = test()
w.setGeometry(100, 0, 200,300)
w.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
widget = test()
widget.setGeometry(0, 0,700, 700)
widget.show()
sys.exit(app.exec_())
From the first form, I want to call another form by clicking the button. However I don't know why the second form doesn't show up. Please have a look and show me what I did wrong. Thanks you.

Resources