PyQt5: how to inherit a widget with its layout or geometry? - python

I put a QLineEdit in form.ui, and I want to use CustomLabel to inheriance it. But there are both 2 QLineEdit(lines in form.ui and CustomLabel) in my mainwindow.
How should I deal with this situation?
Here is the image of my programme
Here is my code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import PyQt5.uic as uic
form_ui,_ =uic.loadUiType('form.ui')
print(type(form_ui))
class Mainwindow(QWidget,form_ui):
"""docstring for App"""
def __init__(self):
super(Mainwindow, self).__init__()
self.setupUi(self)
path_lineEdit = CustomLabel(self.lines)
class CustomLabel(QLineEdit):
def __init__(self,parent=None):
super(CustomLabel,self).__init__(parent)
# self.resize(parent.size())
def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
def dropEvent(self, e):
self.setText('212'+e.mimeData().text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Mainwindow()
ex.show()
sys.exit(app.exec_())

Related

PYQT5 GIF freezes on GUI initialization

Keep gif running while GUI starts. Is that possible? I have read many reporitys but none with the true and understandable answer.
I have prepared a code example that shows the problem.
import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize, QThread
class Main_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(500, 500))
self.setWindowTitle("Main Window")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
gif = QLabel(self)
gif.setGeometry(0,0,500,500)
self.movie = QMovie(r"C:\Users\...\Pictures\Icon_LOAD.gif")
gif.setMovie(self.movie)
self.movie.start()
# #Call event handler to process the queue, but it is shocking, dirty and unsuitable
#app.processEvents()
self.show()
for i in range(0,1000000):
print(i)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWin = Main_Window()
sys.exit(app.exec_())
Now it works smooth
import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize
from threading import Thread
class Main_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(500, 500))
self.setWindowTitle("Main Window")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
gif = QLabel(self)
gif.setGeometry(0,0,500,500)
self.movie = QMovie(r"gif.gif")
gif.setMovie(self.movie)
self.movie.start()
self.show()
Thread(target=self.function_on_thread).start()
def function_on_thread(self):
for i in range(0,1000000):
print(i)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWin = Main_Window()
sys.exit(app.exec_())

How to get a button to move a widget in relation to its current position every time the button is pressed?

Here's my attempt at making this work:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.on_click()
def initUI(self):
button = QPushButton('PyQt5 button', self)
button.move(100,200)
button.clicked.connect(self.on_click)
def on_click(self):
text = QTextEdit(self)
insideText = "Text"
text.document().setPlainText(insideText)
text.resize(100,25)
print('PyQt5 button click')
position = text.pos()
text.move(position.x()+50,position.y()+0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
Maybe I need to make the QTextEdit go somewhere in the initUI() method, instead of being defined in on_click(). I already tried this but I get "text is not defined", and I'm not sure how to reference it inside another function/method. Any help is appreciated.
Edit: Here's the working code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,QTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.on_click()
def initUI(self):
self.button = QPushButton('PyQt5 button', self)
self.button.move(100,200)
self.button.clicked.connect(self.on_click)
self.text = QTextEdit(self)
insideText = "Text"
self.text.document().setPlainText(insideText)
self.text.resize(100,25)
def on_click(self):
print('PyQt5 button click')
position = self.text.pos()
self.text.move(position.x()+50,position.y()+0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())

PyQt5 QObject+QGraphicsItem multi-inheritance causes crash on QGraphicsItem.setFlags()

I discovered that, in a class inheriting from QGraphicsItem and QObject, calling QGraphicsItem.setFlags() causes immediate, errorless crash in PyQt5. The following example demonstrates this
import sys
from PyQt5.QtCore import QObject, QRectF
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QGraphicsItem, QGraphicsView, QGraphicsScene, QMainWindow, QApplication
class Item(QObject, QGraphicsItem):
def __init__(self):
QObject.__init__(self)
QGraphicsItem.__init__(self)
self.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable |
QGraphicsItem.ItemSendsScenePositionChanges)
def boundingRect(self) -> QRectF:
return QRectF(0,0,50,50)
def paint(self, painter, option, widget=...) -> None:
painter.fillRect(self.boundingRect(), QColor('#555555'))
class View(QGraphicsView):
def __init__(self):
super().__init__()
self.resize(400, 300)
scene = QGraphicsScene(self)
scene.setSceneRect(0,0,self.width(),self.height())
self.setScene(scene)
def mousePressEvent(self, event) -> None:
QGraphicsView.mousePressEvent(self, event)
if event.isAccepted():
return
newitem = Item()
self.scene().addItem(newitem)
newitem.setPos(event.pos())
class ViewWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setCentralWidget(View())
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = ViewWindow()
widget.show()
app.exec_()
The same code works fine with PySide2. Did I miss something there? I'm using Python 3.9.5 and PyQt5 5.15.4.

PyQt5, I need not to print continuously but instead it only changes the QLabel

I need it to be not continuously printing but instead it only change the QLabel,
I dont need to add more, just whenever you write in Line edit it should replace the existing text. I need it like a stocks
This is the code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.hbox = QHBoxLayout()
self.game_name = QLabel("Stocks:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Print", self)
self.search_button.clicked.connect(self.on_click)
self.hbox.addWidget(self.game_name)
self.hbox.addWidget(self.game_line_edit)
self.hbox.addWidget(self.search_button)
self.setLayout(self.hbox)
self.show()
#pyqtSlot()
def on_click(self):
game = QLabel(self.game_line_edit.text(), self)
self.hbox.addWidget(game)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
You have to create a QLabel, set it in the layout and only update the text with setText():
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.game_name = QLabel("Stocks:")
self.game_line_edit = QLineEdit()
self.search_button = QPushButton("Print")
self.search_button.clicked.connect(self.on_click)
self.game = QLabel()
hbox = QHBoxLayout(self)
hbox.addWidget(self.game_name)
hbox.addWidget(self.game_line_edit)
hbox.addWidget(self.search_button)
hbox.addWidget(self.game)
self.show()
#pyqtSlot()
def on_click(self):
self.game.setText(self.game_line_edit.text())
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

How to get value from a QSlider [duplicate]

This question already has answers here:
PySide (or PyQt) signals and slots basics
(3 answers)
Closed 4 years ago.
In this code i want to make a GUI of QSlider with PyQt5. What all I want is to print changing value as i move the slider. But here, it only prints 90 which is the primary value of the slider.
N.B: I know that continuously changing value can be printed from valuechange function. But I need to print the value only from init function.
The code:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout,
QGroupBox,QMenu, QPushButton,
QRadioButton, QVBoxLayout,
QWidget, QSlider,QLabel)
class sliderdemo(QWidget):
def __init__(self, parent = None):
super(sliderdemo, self).__init__(parent)
layout = QVBoxLayout()
self.sl = QSlider(Qt.Horizontal)
self.sl.setMinimum(0)
self.sl.setMaximum(180)
self.sl.setValue(90)
self.sl.setTickPosition(QSlider.TicksBelow)
self.sl.setTickInterval(10)
layout.addWidget(self.sl)
self.sl.valueChanged.connect(self.valuechange)
self.setLayout(layout)
self.setWindowTitle("slider")
print(self.valuechange())
def valuechange(self):
self.size = self.sl.value()
return self.size
def main():
app = QApplication(sys.argv)
ex = sliderdemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you really want, you can :-)
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout,
QGroupBox,QMenu, QPushButton,
QRadioButton, QVBoxLayout,
QWidget, QSlider,QLabel)
class sliderdemo(QWidget):
def __init__(self, vSl=90, parent=None):
super(sliderdemo, self).__init__(parent)
layout = QVBoxLayout()
self.sl = QSlider(Qt.Horizontal)
self.sl.setMinimum(0)
self.sl.setMaximum(180)
self.sl.setValue(vSl)
self.sl.setTickPosition(QSlider.TicksBelow)
self.sl.setTickInterval(10)
layout.addWidget(self.sl)
self.sl.valueChanged[int].connect(self.valuechange)
self.setLayout(layout)
self.setWindowTitle("slider")
#print(self.valuechange())
print("__init__vSl -> ", vSl)
def valuechange(self, value):
#self.size = self.sl.value()
self.__init__(value)
#return self.size
def main():
app = QApplication(sys.argv)
ex = sliderdemo(90)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Categories