I've got this on my file System :
- myFolder
- mySubFolder
Within the TreeView I expand the folder "myFolder".
Then I rename it as "myFolder_2".
And finaly I try to rename the folder "mySubFolder" as "mySubFolder_2".
"mySubFolder_2" in is no more considered as a folder but as unknown with a size of -1 bytes and I've got the message : QFileSystemWatcher: failed to add paths: myFolder.
Here is the my source code :
from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
treeView = QtGui.QTreeView()
fileSystemModel = QtGui.QFileSystemModel(treeView)
fileSystemModel.setReadOnly(False)
treeView.setModel(fileSystemModel)
folder = "."
treeView.setRootIndex(fileSystemModel.setRootPath(folder))
treeView.show()
end = app.exec_()
Any help will be welcome.
You need to set the root path on the model before setting it on the treeview:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
treeView = QtGui.QTreeView()
fileSystemModel = QtGui.QFileSystemModel(treeView)
fileSystemModel.setReadOnly(False)
root = fileSystemModel.setRootPath('.')
treeView.setModel(fileSystemModel)
treeView.setRootIndex(root)
treeView.show()
app.exec_()
Related
Every time I create a Window UI - Dynamic load project in Qt Creator v8.0.2 and run the project I get the following error:
error: qt.pysideplugin: Environment variable PYSIDE_DESIGNER_PLUGINS
is not set, bailing out.
As a result, the window doesn't display any widgets, it is just a gray empty window
I have tried to configure os.environ['PYSIDE_DESIGNER_PLUGINS'] = '.' but then I get the following error:
error: qt.pysideplugin: No python files found in '.'.
Is there a way to load the pysideplugin to Qt Creator v8.0.2 or to remove the errors related to it and make the project run and show the widgets?
I am testing with a very simple UI
But when I run the project I can only see the following:
The complete code in the mainwindow.py file is:
# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.load_ui()
def load_ui(self):
loader = QUiLoader()
path = Path(__file__).resolve().parent / "form.ui"
ui_file = QFile(path)
ui_file.open(QFile.ReadOnly)
loader.load(ui_file, self)
ui_file.close()
if __name__ == "__main__":
#os.environ['PYSIDE_DESIGNER_PLUGINS']='.'
app = QApplication(sys.argv)
widget = MainWindow()
widget.show()
sys.exit(app.exec())
I am using python 3.10.5 and PySide6 version 6.4.0.1
I'm learning how to use pyqt, I'm using Windows 10 and python 3.10 and i'm testing a code to open pdf files, this allows you to select the file and then opens it on a new window. It works almost correctly except that doesn't show zoom buttons, or zoom percentage.
This is the code:
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
def main():
print(
f"PyQt5 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
)
app = QtWidgets.QApplication(sys.argv)
filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
if not filename:
print("please select the .pdf file")
sys.exit(0)
view = QtWebEngineWidgets.QWebEngineView()
settings = view.settings()
settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
url = QtCore.QUrl.fromLocalFile(filename)
view.load(url)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I was wondering if I need to pass an extra setting to display zoom buttons on pdf viewer. Anyone can help me? I highlighted where buttons/actions supposed to display in the image below.
That problem is because Qt version, if you test with the most recent version, PyQt6 you will see that works.
You need to install PyQt6:
pip install pyqt6
You also need to install webengine for pyqt6:
pip install PyQt6-WebEngine
And you need to change your code to this:
import sys
from PyQt6 import QtCore, QtWidgets, QtWebEngineWidgets
def main():
print(
f"PyQt6 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
)
app = QtWidgets.QApplication(sys.argv)
filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
if not filename:
print("please select the .pdf file")
sys.exit(0)
view = QtWebEngineWidgets.QWebEngineView()
settings = view.settings()
settings.setAttribute(view.settings().WebAttribute.PluginsEnabled, True)
url = QtCore.QUrl.fromLocalFile(filename)
view.load(url)
view.resize(640, 480)
view.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
And now should look like this:
I want to create a hyperlink of (file_path ) using python that will point to the file in operating system. I am able to get the path of the file. How can I create a hyperlink of this path and store in a variable. I want to display this in the QtWidgets as link where user can click and open the file stored in windows OS.
file_path = os.path.join(dir_path, FileName)
self.documents.setItem(0, 0, QtWidgets.QTableWidgetItem(file_path))
You can use QLabel with html content to create clickable link.
from PyQt5 import QtWidgets, QtCore
import os
if __name__ == "__main__":
app = QtWidgets.QApplication([])
path = os.environ["USERPROFILE"]
text = "click me"
label = QtWidgets.QLabel('{}'.format(path, text))
label.show()
label.linkActivated.connect(os.startfile)
app.exec()
I wrote below code
import sys,time
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
sys.path.append(r"C:\Users\hpaalm\Desktop")
a=QPushButton()
a.setIcon(QIcon('1.png'))
a.show()
app.exec_()
when i run it in IDE, it show my icon, but when run it in CMD it not show icon. what is problem?
python C:\Users\hpaalm\Desktop\a.py
sys.path contains a list of paths where python imports the modules, this does not serve to import files, icons or similar resources. Instead it is best to create a function that binds the directory path with the filename and return the full path of the icon:
import os
import sys
from PyQt5 import QtGui, QtWidgets
ICON_DIR = r"C:\Users\hpaalm\Desktop"
def get_path_icon(filename):
return os.path.join(ICON_DIR, filename)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
a = QtWidgets.QPushButton()
a.setIcon(QtGui.QIcon(get_path_icon('1.png')))
a.show()
sys.exit(app.exec_())
I am using following code to load a ui file, but keep seeing an error message.
# main.py
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("main_window.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
Error message:
FileNotFoundError: [Errno 2] No such file or directory: 'main_window.ui'
main_window.ui is located in the same folder as main.py
The name of the file you pass to loadUiType is relative to the working directory, not your python file. You can pass the full path instead. To get the full path, you can find the directory of your current file and then join that with the name of your UI file.
e.g:
...
ui_path = os.path.dirname(os.path.abspath(__file__))
form_class = uic.loadUiType(os.path.join(ui_path, "main_window.ui"))[0]
...