How to call an event from another class in PyQt5 - python

I'm trying to call a mouse click event in QWebview to get a text by copy in the QWebView page but I don't know how. I have been trying to solve this for sometimes now.
Here is the minor code
import sys
from PyQt5.QtWidgets import *
from PyQt5 import Qt
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWebKitWidgets import *
class sw(QWebView):
def keyPressEvent(self, e):
clipboard=QApplication.clipboard()
data=clipboard.mimeData()
print(data.text)
super(Mainw, self).keyPressEvent(e)
class Mainw(QWidget):
def __init__(self):
super().__init__()
#QTextEdit().__init__()
self.m=QMenuBar(self)
self.file=self.m.addMenu("file")
self.new=QAction("new", self)
self.file.addAction(self.new)
ed=QWebView(self)
ed.setHtml(imported_data)
ed.setGeometry(20,30,400,400)
if __name__=="__main__":
a=QApplication(sys.argv)
app=Mainw()
#sb=app.open()
app.show()
sys.exit(a.exec_())
I know there are alot or errors in this code

Related

Switching ui without new window PyQT5

I want to change the UI without opening new windows. The problem is that when I do this on widgets then the app is not frameless. When I do it without widgets I have no idea how to change the UI.
Without widget
import sys, time, os
from PyQt5 import uic
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap
import sqlite3
class LoginMenu(QWidget):
def __init__(self):
super(LoginMenu, self).__init__()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
uic.loadUi("LoginMenu.ui",self)
self.clicked = False
def mousePressEvent(self, event):
self.old_pos = event.screenPos()
def mouseMoveEvent(self, event):
if self.clicked:
dx = self.old_pos.x() - event.screenPos().x()
dy = self.old_pos.y() - event.screenPos().y()
self.move(self.pos().x() - dx, self.pos().y() - dy)
self.old_pos = event.screenPos()
self.clicked = True
return QWidget.mouseMoveEvent(self, event)
app = QApplication(sys.argv)
log = LoginMenu()
log.show()
sys.exit(app.exec())
with widgets to run app
import sys, time, os
from PyQt5 import uic
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap
class LogMenu(QWidget):
def __init__(self):
super(LogMenu, self).__init__()
uic.loadUi("LoginMenu.ui",self)
self.SignUp.clicked.connect(self.gotoReg)
def gotoReg(self):
reg=Register()
widget.addWidget(reg)
widget.setCurrentIndex(widget.currentIndex()+1)
class Register(QWidget):
def __init__(self):
super(Register, self).__init__()
uic.loadUi("SignUp.ui",self)
#main
app = QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
log=LogMenu()
reg=Register()
widget.addWidget(log)
widget.addWidget(reg)
widget.show()
try:
sys.exit(app.exec())
except:
print("Exiting")
sry for my eng if sth i wrote sth bad
tbh idk how to make it frameless with widgets and draggable with clicking and holding the app or switchable without using widget to run it

Maximize/Restore buttons in the title bar are disabled in a QMainWindow - pyqt5

I was just creating my window in the Qt Designer, when I had finished it I noticed that even opening it from a .py file, the maximize and restore buttons in the title bar are disabled, they appear in there but I can't click it. I am using Windows 10 by the way. It appears like this:
enter image description here
I found some solutions using:
self.setWindowFlag(Qt.WindowMaximizeButtonHint) self.setWindowFlag(Qt.WindowMinimizeButtonHint)
but they don't work in my case.
Here's the complete code:
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import *
import sys
class mainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(mainWindow, self).__init__()
uic.loadUi('mainMenu.ui', self)
self.show()
self.setWindowFlag(Qt.WindowMaximizeButtonHint)
self.setWindowFlag(Qt.WindowMinimizeButtonHint)
app = QtWidgets.QApplication(sys.argv)
window = mainWindow()
app.exec_()

how to use a promoted class in PyQt5?

in principal I reopen my question some days ago:
where-do-i-write-the-class-for-a-single-promoted-qwidget-from-qt-designer
The solution works, but I run into 2 more questions
if I want to change the text in the class neuLabel, how do i adress the class out of main program?
Additional information for minimal example as required:
I have a qt designer main window, named testpromote.ui. It contains only one label. Text is "MyLabel", the name is neuLabel and it is promoted as neulabel.
I need 2 .py files in one directory.
First the main testpromote.py
import sys
import os
from PyQt5 import uic
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import * #QPainter
import neulabel
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
uifile_1 = os.path.join(CURRENT_DIR, "testpromote.ui")
form_1, base_1 = uic.loadUiType(uifile_1)
class myApp(base_1, form_1):
def __init__(self):
super(base_1,self).__init__()
self.setupUi(self)
# ??????????????
#neuLabel.setText(neuLabel,"from main")
# ??????????????
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = myApp() #Dialog()
ex.show()
sys.exit(app.exec_())
2nd file is neulabel.py
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import * #QPainter
class neuLabel(QLabel):
def __init__(self,parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self.setText("from code")
print("the new pixmap?", self.text())
My aim is to change the text of the label out of the main program testpromote.py
I do not understand, how to import the neulabel.py, so that i could change the text of the label.I tried it with "neuLabel.setText(neuLabel,"from main")", but this doesn't work. I tried it with other code too, but not succesfully. (This is marked within the # ????????? lines)
To see, if the text in the label changes, I added in neulabel.py the
self.setText() statement, but the text does not change in the window.
(i hope, it is clearer now)

PyQt touch certain points on pixmap

I have a form with a picture on it, in this example it is a Humidity Indicator the user should be able to press the 60, 10 and 5% spot. The label should display the pressed spot.
How do I do that could somebody show me an example or is this not possible with Qt?
My code (which is pretty empty for now)
from PyQt5.QtWidgets import QApplication, QWidget, QDialog
from PyQt5.QtGui import QPixmap, QRegExpValidator
from PyQt5.QtCore import QRegExp
from mysql.connector import (connection)
from datetime import *
from bs4 import BeautifulSoup as bs
import os
import sys
import DatabaseHandling
'''Convert UI file to Python'''
os.chdir("C:\\Users\Gianni Declercq\AppData\Local\Programs\Python\Python36-32\Scripts")
os.system("pyuic5.exe M:\QtProjects\\Ui\RPI1_Third.ui -o M:\QtProjects\\RPI1_Third_ui.py")
from RPI1_Third_ui import Ui_Form3 # import after recreation of py file
class ThirdWindow(QWidget, Ui_Form3):
def __init__(self):
super(ThirdWindow, self).__init__()
self.dbu = DatabaseHandling.DatabaseUtility()
self.msl = None
# Show UI on screen + resize window
self.setupUi(self)
self.picInidicator.setPixmap(QPixmap("F:\QtProjects\\138691.jpg"))
self.setFixedSize(800, 480)
if __name__ == '__main__':
app = QApplication([])
window = ThirdWindow()
window.show()
sys.exit(app.exec_())
Example of the form

PyQt5 signal communication error

I need your help with the following problem that I've encountered.
I have two Python files, Main.py and Module.py, which need to communicate using PyQt5 signals. Here's the code:
Main.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from MyGUI import main_window_GUI
from Modules import Module.py
class MainWindow(QMainWindow, main_window_GUI.Ui_main_window):
def __init__(self):
QMainWindow.__init__(self)
main_window_GUI.Ui_main_window.__init__(self)
self.setupUI(self)
sub_win = QMdiSubWindow()
sub_win.setWidget(Module.moduleWindow())
self.mdi.addSubWindow(sub_win)
# this part reports error saying:
# 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'
Module.moduleWindow.my_signal.connect(self.do_something)
def do_something(self):
pass
Module.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from MyGUI import module_window_GUI
class moduleWindow(QMainWindow, module_window_GUI.Ui_module_window):
my_signal = pyqtSignal()
def __init__(self):
QMainWindow.__init__(self)
module_window_GUI.Ui_module_window.__init__(self)
self.setupUI(self)
# the rest is not important
# what is important is th following function
def closeEvent(self, event):
# when the user closes this subwindow signal needs to
# be emitted so that the MainWindow class knows that
# it's been closed.
self.my_signal.emit()
event.accept()
Any kind of help is more than welcome. Thanks in advance.
You need to connect the signal from an instance of the moduleWindow class, and not from the class itself:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from MyGUI import main_window_GUI
from Modules import Module
class MainWindow(QMainWindow, main_window_GUI.Ui_main_window):
def __init__(self):
QMainWindow.__init__(self)
main_window_GUI.Ui_main_window.__init__(self)
self.setupUI(self)
sub_win = QMdiSubWindow()
module_window = Module.moduleWindow()
sub_win.setWidget(module_window)
self.mdi.addSubWindow(sub_win)
module_window.my_signal.connect(self.do_something)
#pyqtSlot()
def do_something(self):
pass
I would also recommend to decorate the do_something method with pyqtSlot as reported in the documentation

Categories