'QWebChannel python function returned values is always same
I am trying to get data from localStoragePy package for python and sending it to the QtWebEngineView's js but whenever I try to get data from localStoragePy in js I always get the same output even though the actual data in the storage is different.
Code: html
<body>
<script src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script>
window.onload = () => {
new QWebChannel(qt.webChannelTransport, async (channel) => {
window.backend = await channel.objects.backend;
getServerStat = async () => {
await window.backend.getValuesFromDatabaseBool("serverGo", (data) => {
if (data == true) {
emitter.emit("serverGo-true");
} else {
emitter.emit('serverGo-false');
}
console.log(`Server Status = ${data}`);
return Boolean(data);
});
};
})
}
</script>
</body>
py:
import sys,os,requests,sqlite3
from jellyfin_apiclient_python import JellyfinClient
from localStoragePy import localStoragePy
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCore import QUrl, QFileInfo, Slot, QObject
from PySide6.QtGui import QIcon
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWebEngineCore import QWebEngineSettings
from PySide6.QtWebChannel import QWebChannel
app = QApplication(sys.argv)
os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = "9000"
storagea = localStoragePy('prayag.jellyplayer.app', 'sqlite')
storagea.setItem("App_Version", "0.1.0")
class Backend(QObject):
@Slot(str, result=bool)
def saveServer(self, data):
ping = requests.get(f"{data}/System/Ping")
if ping.content == b'"Jellyfin Server"':
storagea.setItem('server', data)
storagea.setItem("serverGo", True)
return True
else:
storagea.setItem("serverGo", False)
return False
@Slot(str, result=bool)
def getValuesFromDatabaseBool(self, object):
print(f"Object: {object}")
item = storagea.getItem(object)
if item == True:
return True
elif item == False:
return False
@Slot(str, result=str)
def getValuesFromDatabaseStr(self, object):
item = storagea.getItem(object)
return item
class LoginWin(QMainWindow):
def __init__(self):
super().__init__()
self.dir = os.path.dirname(os.path.abspath(__file__))
self.view= QWebEngineView(self)
self.setCentralWidget(self.view)
# self.setWindowIcon()
self.view.settings().setAttribute(QWebEngineSettings.JavascriptEnabled, True)
self.view.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
self.view.settings().setAttribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls, True)
self.view.setWindowTitle('JellyPlayer App')
self.view.setWindowIcon(QIcon(f"{self.dir}/assests/icon.png"))
self.setWindowIcon(QIcon(f"{self.dir}/assests/icon.png"))
self.view.load(QUrl.fromLocalFile(f"{self.dir}\\renderer\\html\\main.html"))
self.view.loadFinished.connect(self.handleLoaded)
self.inspector = QWebEngineView()
self.inspector.setWindowTitle("Inspector")
self.inspector.load(QUrl("http://127.0.0.1:9000"))
self.backend = Backend()
self.channel = QWebChannel()
self.channel.registerObject('backend', self.backend)
self.view.page().setWebChannel(self.channel)
def handleLoaded(self, ok):
if ok:
self.view.page().setDevToolsPage(self.inspector.page())
self.inspector.show()
window = LoginWin()
window.showMaximized()
sys.exit(app.exec())
I am using PySide6, the emitter fire is received in another part of the code, I did try to debug this by trying to print the values in the database at the time of sending the data but the print just shows the actual value whereas the value received by the JS function is old value
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
