PostgreSql, QSqlDatabase: Failed to open Database - python

My system: Windows 10 Pro 16299, Qt, PyQt 5.11.2, Python 3.6, PostgreSql 10
I tried to use QTableView/QSqlTableModel for in my gui to work with postgresql data. However, I am not able to open the database. I get the error message “Driver not loaded Driver not loaded”.
A new installation of Qt, PostgreSql and PyQt has not solved the problem. I tried also “Dependency Walker” to look for missing dlls, but was not able use the given information.
Do you have an idea how to fix this problem?
As an alternative: Is it possible to use QTableView/QSqlTableModel with psycopg2 (instead of QSqlDatabase)?
Thank you very much in advance!
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
from PyQt5.QtWidgets import QTableView, QApplication
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setPort(5432)
db.setDatabaseName("Test")
db.setUserName("postgres")
db.setPassword("xxxxx")
if (db.open() == False):
QMessageBox.critical(None, "Error", db.lastError().text())
else:
Print("Connected")
Dependency Walker Screenshot

Find the Postgres driver:
C:\Python\Python36\Lib\site-packages\PyQt5\Qt\plugins\sqldrivers\qsqlpsql.dll
where C:\Python\Python36 is your Python installation directory.
Load this file with dependency walker and check whether libpq.dll and all its dependencies can be found.
Typically the two Postgres folders have to be set on the system path:
C:\Program Files\PostgreSQL\10\bin\
C:\Program Files\PostgreSQL\10\lib\
The dll's version incompatibility (64-bit vs 32-bit) may be one of the probable problems.

Related

PyQt5 Missing QtQuick.Studio Module

Issue
I used Qt Design Studio to create some UI components and screens (.ui.qml format), but no matter what I do, whenever I try to run it in PyQt5 I kept encountering the following error:
module "QtQuick.Studio.Components" is not installed
The .ui.qml file has an import
import QtQuick.Studio.Components 1.0
I looked everywhere online and couldn't find much on the topic on how to fix this problem.
Question
What's the best way to overcome this issue? I've been using it on Windows but I am mainly looking to use the code on a raspberry pi, are there extra dependencies that need to be installed?
Python code
import os
from pathlib import Path
import sys
from PyQt5.QtCore import QCoreApplication, Qt, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == '__main__':
CURRENT_DIRECTORY = Path(__file__).resolve().parent
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.addImportPath(os.fspath(CURRENT_DIRECTORY.parents[0]))
url = QUrl.fromLocalFile(os.fspath(CURRENT_DIRECTORY / "Screen01.ui.qml"))
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
print("exit")
QCoreApplication.exit(-1)
engine.objectCreated.connect(
handle_object_created, Qt.ConnectionType.QueuedConnection
)
engine.load(url)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
Windows
I used print(engine.importPathList()) to see my import paths. This prompted me to go C:/Users/USER/AppData/Local/Programs/Python/Python38/lib/site-packages/PyQt5/Qt5/qml and noticed that QtQuick exists but does not have a Studio folder.
Since Qt Design Studio can run my code with no import issues, I knew that it would have a Studio folder. Therefore, I went into C:\Program Files\Qt\qtdesignstudio-2.2.0-community\qt5_design_studio_reduced_version\qml\QtQuick and copied the Studio folder from there onto the PyQt5 path above.
This fixed the issue.
Linux
I am yet to try it on Linux.

PyQt5 QMYSQL Driver not loaded

I know there a many questions and answers on SO for this question, but nothing worked for me.
I installed the newest QT Creator 4.9.2 with Qt 5.13.0 MinGW 32-Bit and MySQLServer 5.5 to compile the QMYSQL driver. I but the driver in the sqldrivers folder and the libmysql.dll in the bin folder.
If I use C++ to connect to the database, everything is working fine, but I doesn't with python.
Additionally I installed on a fresh Python 3.7 32-Bit:
PyQt5: pip install pyqt5 (5.13.0)
mysqlclient: pip install mysqlclient (1.4.2)
mysql-connector: pip install mysql-connector (2.2.9)
and but the driver I compiled before in the sqldrivers folder from PyQt5, because the driver was missing there as well.
My Class:
class SqlConnector(QSqlDatabase):
def __init__(self):
super(SqlConnector, self).__init__()
self.db = self.connect()
def __del__(self):
self.db.close()
def connect(self):
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("hostname")
db.setDatabaseName("database")
db.setUserName("username")
db.setPassword("1234567890")
ok = db.open()
if ok:
print("Connection established")
return db
else:
print("Connection failed: ", db.lastError().text())
return None
def getPools(self):
query = QSqlQuery("SELECT id, name, token, dlxCode FROM AnalysisPool", self.db)
model = QSqlQueryModel()
model.setQuery(query)
return model
Main Method:
from PyQt5.QtSql import QSqlDatabase, QSqlQueryModel, QSqlQuery
from PyQt5.QtWidgets import QApplication, QTableView
if __name__ == "__main__":
app = QApplication(sys.argv)
poolView = QTableView()
poolView.show()
database = SqlConnector()
# poolView.setModel(database.getPools())
sys.exit(app.exec_())
The libraryPaths from PyQt are C:/Python/lib/site-packages/PyQt5/Qt/plugins and C:/Python
The used OS is Windows 10 (1809) 64-Bit and I included in the path variable the MySQLServer lib folder.
I don't know what to do, I have read every SO question about this topic without a working solution for my problem.
For me worked this way: UNinstall package insraled via pip3.
pip3 uninstall PyQt5
Install the native package for your distro (linux case). In my case is Fedora then
Sudo dnf install python3-qt5 python3-qt5-qtbase --allowerasing
Use an option wich lets you overwrite old files like --force or --allowerasing or something equivalent

Can't import QtMultimedia in PyQt 5 on Windows

I'm trying to learn PyQt5 + qml, and everything I tried so far works fine, however I've ran into a snag when trying to import QtMultimedia into my qml file results in the following error:
plugin cannot be loaded for module "QtMultimedia": Cannot load library
D:\py35venvQt\lib\site-packages\PyQt5\Qt\qml\QtMultimedia\declarative_multimedia.dll:
The specified module could not be found.
main.qml:
import QtQuick 2.8
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.1
import QtMultimedia 5.6
Window {
id: root
visible: true
title: 'my pyqt app'
width: 1280
height: 720
}
main.py
if __name__ == '__main__':
def handleStatusChange(status):
if status == QQuickView.Error:
errors = appLabel.errors()
if errors:
print (errors[0].description())
myApp = QApplication(sys.argv)
appLabel = QQuickView()
appLabel.statusChanged.connect(handleStatusChange)
model = models.ActorModel(DB_PATH)
ctxt = appLabel.rootContext()
ctxt.setContextProperty('myModel', model)
appLabel.setSource(QUrl('./qml/main/main.qml'))
try:
sys.exit(myApp.exec_())
except:
print("Exiting")
Without the QtMultimedia import everything works fine, also I've tried all Possible version of QtMultimedia (5.0,5.1 etc). In addition the dll does exist in the correct path.
When searching for a solution online I found some post saying that QtMultimedia is not supported in PyQt5, though I think those posts are too old.
I would appreciate it if someone would shed some light on this situation,
Thank you.
Edit: Python Version 3.5.2 PyQt Version 5.8.
PyQt installation process: new virtualenv -> pip install pyqt5
Edit2: Tried reinstalling to a completely new virtual environment using pip3 install pyqt5 still getting the same error.
Edit3: According to http://www.dependencywalker.com the following dlls can not be found in the dir where declarative_multimedia.dll is:
QT5MULTIMEDIA.DLL QT5QUICK.DLL QT5GUI.DLL QT5QML.DLL QT5CORE.DLL
QT5MULTIMEDIAQUICK_P.DLL
With the exception of QT5MULTIMEDIAQUICK_P.DLL they are all present in the \Lib\site-packages\PyQt5\Qt\bin directory
QT5MULTIMEDIAQUICK_P.DLL is not present at all.
I tried copying all the missing files into \Lib\site-packages\PyQt5\Qt\qml\QtMultimedia to see if it would make any difference. It did not.
I also tried to install PyQt 5 into my proper python 3.5 installation (without virtualenv) and running my code. The result is the same.
I had similar issue on Ubuntu and I solved my problem by adding environment variable LD_LIBRARY_PATH=/home/slav/Apps/Qt5.9.2/5.9.2/gcc_64/lib.
"/home/slav/Apps/Qt5.9.2/5.9.2/gcc_64/lib" here I've install Qt with QtInstaller
maybe You should use pyqt not pyside

Does PyQt5/pyqt4 already supports QtVirtualKeyboard with Handwriting Recognition?

I'm working on a desktop application using pyqt5, and I want to use a Virtual Keyboard with Handwriting Recognition. I saw that Qt, QtVirtualKeyboard already support it.
Here's a link!
I got the C++ Qt example code running on QtCreator.
But using python3.5 and PyQt5 it gives this message:
module "QtQuick.VirtualKeyboard" is not installed
import QtQuick.VirtualKeyboard 2.1
How should I go on from here?
Does PyQt5 comes with VirtualKeyboard module? if no How to install it on PyQt5?
for qt desinger you can add only this line on your .py file.
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
but If you want use QML with qtvirtualkeyboard;
There is no virtualkeyboard plugin in pyqt5.8, you must be use qt's paths.
For a example, basic steps for pyqt5, qt5.8 and qtvirtualkeyboard installiation on ubuntu:
1.step install qt5.8 with qtvirtualkeyboard
wget
http://download.qt.io/official_releases/qt/5.8/5.8.0/qt-opensource-linux-x64-5.8.0.run
chmod +x qt-opensource-linux-x64-5.8.0.run
./qt-opensource-linux-x64-5.8.0.run
2.step
apt-get install python3 python3-pip
pip3 install pyqt5
3.step
set environmental variables your qt paths on your python code.
import sys, os
os.environ["QT_DIR"] = "/opt/Qt5.8.0/5.8/gcc_64"
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins/platforms"
os.environ["QT_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins"
os.environ["QML_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QML2_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
#print(os.environ)
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtQuick import *
class keyboardapp(object):
def __init__(self):
self.view = QQuickView()
self.view.setObjectName("View")
#self.view.setFlags(Qt.FramelessWindowHint)
self.view.setSource(QUrl("main.qml"))
self.view.setResizeMode(QQuickView.SizeRootObjectToView)
#self.Screen = self.view.rootObject()
#print("Screen(Root) = " + str(self.Screen))
self.view.show()
app = QApplication(sys.argv)
test = keyboardapp()
sys.exit(app.exec_())
I been stuck with this too, and i am new to Qt
After some researching, and digging in source code, found the solution
You dont need to import it to use it since its a module it will implement itself to every qt input
Well you dont need to install it on PyQt5 but on Qt5 if it is not already come with your Qt package, if you use archlinux you can install it with pacman
pacman -S qt5-virtualkeyboard
If you cannot find it in you os repositories try to build it here is documantation
https://doc.qt.io/qt-5/qtvirtualkeyboard-index.html
Then to use it in your pyqt application, set environmental variable QT_IM_MODULE to "qtvirtualkeyboard" either from your bash or inside the top your script like
import os
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"

Installing SIP 4.16.4 and PyQt5.3.1 for Python 3.4 x64 on Windows 8.1

Downloaded SIP 4.16.4.zip(windows source) from riverbankcomputing - Unpacked and ran configure.py from within the folder. Sub-Directories sipgen, sip-lib are now in the Python34 directory along with sipconfig.py and Makefile.
I am unable to run any of the Makefiles either in the sipgen/sip-lib sub folders or in the main Python34 folder. Ran the installer for Py3.4-Qt5.3.1-x64 and the following test code from within PyCharm3.4.1
The code runs with exit code 0, but within the PyCharm editor, the import statements are flagged as
Cannot find reference 'QtCore' in '__init__.py'
and
Cannot find reference 'QtWidgets' in '__init__.py'
The Qt***.pyd (QWidget, QLabel, etc) files from:
Python34\lib\site-packages\PyQt5
used in the code are flagged by PyCharm as Unresolved reference.
My goal is to install PyQt5.3.1x64 for Python 3.4x64 on Windows 8.1 - I have searched extensively and most of the doc/posts I've encountered are for Visual Studio applications or leads off into some tangent that is not applicable.
I'm looking for a concise procedure to install on windows and am not sure if I should also install some base QT by running qt-opensource-windows-x86-1.6.0-6-online.exe installer.
I believe the problem is with not being able to run the make and make install steps for the Windows environment. There is a Makefile in each of the following directories...
Python34: python34\sipgen and python34\siplib
Do I need to run a make and install using each of these and if so how is this done on Windows 8.1?
TEST CODE
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Form(QWidget):
def \__init__(self, parent=None):
super(Form, self).\__init__(parent)
nameLabel = QLabel("Name:")
self.nameLine = QLineEdit()
self.submitButton = QPushButton("&Submit")
buttonLayout1 = QVBoxLayout()
buttonLayout1.addWidget(nameLabel)
buttonLayout1.addWidget(self.nameLine)
buttonLayout1.addWidget(self.submitButton)
self.submitButton.clicked.connect(self.submitContact)
mainLayout = QGridLayout()
# mainLayout.addWidget(nameLabel, 0, 0)
mainLayout.addLayout(buttonLayout1, 0, 1)
self.setLayout(mainLayout)
self.setWindowTitle("Hello Qt")
def submitContact(self):
name = self.nameLine.text()
if name == "":
QMessageBox.information(self, "Empty Field",
"Please enter a name and address.")
return
else:
QMessageBox.information(self, "Success!",
"Hello %s!" % name)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
screen = Form()
screen.show()
sys.exit(app.exec_())
In another post it was suggested that the unresolved references are caused by a bug in PyCharm. The code runs fine but the IDE sets flags throughout for all the Qt components. If anyone else is trying to install sip and Qt5 on windows 8 and needs help contact me and I will be glad to give step by step instructions using Visual Studio Developer Command Prompt.

Categories