PyQt5 QWebEngineView not working properly - python-3.x

This code doesn't show anything on execution. Not even the main window.
How can I use QWebEngineView() in order to make a useful code.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle('Browser Lite')
self.setGeometry(5, 30, 1355, 730)
self.browser = QWebEngineView()
self.browser.load(QUrl('http://www.google.com'))
self.setCentralWidget(self.browser)
app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()

If you mean you get a blank screen try adding the following
from OpenGL import GL # apt install python3-opengl ..will get blank screen and console spam "shader program is not linked"
For a full example you could take a look at the following simplebrowser/pyqt5

Related

PyQt5.QIcon Is not showing any icon

I am trying to show the icon in the window bar and this is not simply working. I tried several solutions but still it doesn't work. Any solution for this code?
import os
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.browser = QWebEngineView()
self.browser.setUrl(QUrl('http://gmail.com'))
self.setCentralWidget(self.browser)
self.showMaximized()
self.setWindowTitle('G')
self.setWindowIcon(QIcon(os.path.join('cil.png')))
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
Try putting the icon like this
icon = QIcon()
icon.addPixmap(QtGui.QPixmap()) #add full path or respective path to the icon image inside QtGui.QPixmap()
self.setWindowIcon(icon)

How to create Signal for QDockWidget?

When QTabWidget is used, it is straightforward to call a function (e.g. on_tab_changed) when any of the tabs is clicked on with something like self.currentChanged.connect(self.on_tab_changed). However, I cannot figure out how to do similarly with QDockWidget. I was able to come up with a simplified MRE that reads:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("OOP")
self.uinterface()
self.show()
def uinterface(self):
self.dock = QDockWidget("OOP example", self)
self.listWidget = QListWidget()
self.listWidget.addItem("One")
self.listWidget.addItem("Two")
self.listWidget.addItem("Three")
self.dock.setWidget(self.listWidget)
self.dock.currentChanged.connect(self.on_tab_changed) # This is the line that I'd like to modify
def on_tab_changed(self, index: int) -> None:
print ("DO SOMETHING")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
This doesn't work because currentChanged is not a Signal for QDockWidget (which is for QTabWidget) although both derive from QWidget. I want to come up with a relatively simple way to accomplish this task as converting QDockWidget into QTabWidget is relatively straightforward for the MRE but a lot more complex and time consuming for the whole app.
P.S. Based on some of the comments, maybe the code could have read
...
self.dock.setWidget(self.listWidget)
self.dock.Clicked.connect(self.on_dockwidget_clicked)
def on_dockwidget_clicked(self) -> None:
print ("DO SOMETHING")
albeit this still would not work and it'd be necessary to use the accepted solution.
You could overwrite the the event method a QDockWidget subclass and listen for mousePressEvents and emit a custom signal that sends some kind of identifier, in case you have multiple dockWidgets and you need to know which one sent the signal. Then you can set your mainWindow to listen for the custom signal and connect you method slot to it.
For Example:
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class DockWidget(QDockWidget):
hasFocus = pyqtSignal([QDockWidget])
def __init__(self, text, parent=None):
super().__init__(text, parent=parent)
self.setObjectName(text)
def event(self, event):
if event.type() == QEvent.MouseButtonPress and event.button() == 1:
self.hasFocus.emit(self)
return super().event(event)
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("OOP")
self.uinterface()
self.show()
def uinterface(self):
self.dock = DockWidget("OOP example", self)
self.listWidget = QListWidget()
self.listWidget.addItem("One")
self.listWidget.addItem("Two")
self.listWidget.addItem("Three")
self.dock.setWidget(self.listWidget)
self.dock.hasFocus.connect(self.on_dock_focus)
def on_dock_focus(self, dockwidget):
print(f"DO SOMETHING WITH {dockwidget.objectName()}")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

PyQt5 QPushButton fires more than once

is there a way in Python3 PyQt5 to get the number of times a signal is connected to a slot i.e.:
QPushButton.clicked.connect(foo)
to have an idea of how many times the slot (foo) will be called upon emitting the signal ?
I am not talking of counters inside my code but a way to get that number from
where PyQt5 stores that info
Mechanically I would consider placing a
print('any message') #like this you are directly notified via the console console or terminal,...
in each methode you will be calling, for example:
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QPushButton
import sys
#import other modules
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# Geometry of main window:
self.setGeometry(200, 200, 300, 400)
# create a Button Btn1
self.Btn1 = QPushButton('Fire Button :)', self)
self.Btn1.clicked.connect(self.Fire1)
self.Btn1.move(0, 0)
self.Btn2 = QPushButton('2nd Fire_Button :)', self)
self.Btn2.clicked.connect(self.Fire2)
self.Btn2.move(130, 0)
def Fire1(self):
print('Worked, Fired once!...details(...)')
def Fire2(self):
print('It is Working, but he needs to add his code! Thanks')
self.setStyleSheet('background:rgba(80, 97, 15, 184);')
self.resize(600, 210)
pass
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
it's my answer is helping you (it's good!), otherwise provide us with more detailed infomations or sample of code you are working-on in order to better help you...

pyqt5 button click not working from my second window

i am stuck in call function from pushButton.
in my project:
app.py, which is the main file to run the project.
ui_mainWindow.py is the file consist of tab widget.
Account.py is the converted file from account.ui
main_Account.py is the file where i import Account.py file.
account_handler.py is the file where consist of functions.
now when i run my project by running app.py ,it will show all contents of ui_mainWindow.py .now if i choose account tab from tabwidget than it will show all contents of mainAccount.py. now if i hit a button from mainAccount.py than function will be call from account_handler.py.
everything working fine but while i hut pushButton nothin happen.
this is my previous post : PyQt5 push button method called from separate python file ,
i follow this separately and this working fine, but in my project samecode not working. can anyone tell me where i am wrong!
app.py
from importlib import reload
import PyQt5.QtCore as QtCore
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QMainWindow,QApplication
import sys
import files.interfaces.ui_mainWindow
import files.interfaces.dashboard
reload(files.interfaces.dashboard)
import files.main_Interfaces.mainAccount
reload(files.main_Interfaces.mainAccount)
import files.interfaces.account2
reload(files.interfaces.account2)
class MainWindow(QMainWindow, files.interfaces.ui_mainWindow.Ui_MainWindow):
def __init__(self):
# Base class
QMainWindow.__init__(self)
self.ui = files.interfaces.ui_mainWindow.Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowTitle("PORTFOLIO ACCOUNTING")
# import tab1
self.TabWidget = QtWidgets.QWidget()
ui = files.interfaces.dashboard2.Ui_Form()
ui.setupUi(self.TabWidget)
self.ui.tabWidget.insertTab(0, self.TabWidget, "Dashboard")
# import tab2
self.TabWidget = QtWidgets.QWidget()
ui = files.main_Interfaces.mainAccount.MainWindow()
ui.setupUi(self.TabWidget)
self.ui.tabWidget.insertTab(1, self.TabWidget, "Account")
def main():
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName("Portfolio Accounting")
application = MainWindow()
application.show()
app.exec_()
if __name__ == '__main__':
main()
main_Account.py
from PyQt5 import QtCore, QtGui, QtWidgets
from files.interfaces.account import Ui_Form
from event_handler.account_EventHndler import function2
class MainWindow(QtWidgets.QMainWindow,Ui_Form):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton_2.clicked.connect(function1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
account_handler.py
def function1():
print("function called")
You code is a bit confusing since you use the same class names in different modules and there is an inconsistencies between the names of the modules you are importing and the names of the .py files you provided but I'm assuming that files.main_Interfaces.mainAccount.MainWindow refers to mainWindow in main_Account.py. In that case, in app.MainWindow.__init__ tab2 should probably be something like
# import tab2
self.TabWidget = files.main_Interfaces.mainAccount.MainWindow()
self.ui.tabWidget.insertTab(1, self.TabWidget, "Account")

PyQt5 all button signals/events?

The signal for MOUSE1 on the button is widget.clicked, what are the ones for MOTION and MOUSE2? Also if anyone knows a site with all signals listed it would really help
import sys, pyautogui
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
def pressed_mouse2():
pass
def window():
app = QApplication(sys.argv)
root = QMainWindow()
root.setGeometry(200, 200, 500, 500)
root.setWindowTitle('Test')
root.setWindowFlags(QtCore.Qt.FramelessWindowHint)
root.setAttribute(Qt.WA_TranslucentBackground)
button = QtWidgets.QPushButton(root)
#i need here to signal when user has pressed on MOUSE2 on the button
button.clicked.connect(clicked)
button.move(50,50)
root.show()
sys.exit(app.exec_())
window()
You just need to connect the button's clicked signal to your function.
button.clicked.connect(pressed_mouse2)
Now when you click the button you can execute any code here:
def pressed_mouse2():
print('Button clicked')
There are many kinds of widgets, each with different signals. You can find them in the Qt documentation. Here are the signals for QAbstractButton, which is inherited by QPushButton.
There is no predefined signal for a right click on the button, but you can subclass QPushButton and emit your own signal in the mousePressEvent().
class Button(QPushButton):
right_clicked = pyqtSignal()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def mousePressEvent(self, event):
super().mousePressEvent(event)
if event.button() == Qt.RightButton:
self.right_clicked.emit()
And it will respond with:
button = Button(root)
button.right_clicked.connect(pressed_mouse2)

Resources