I have tried the procedure (Qt Charts and Data Visualization widgets) to integrate the qtchart plugin.
But it don't work. Makeing the plugin and add it to the desinger folder worked. qt designer ‎recognize the plugin but compiling the ui to a python file I get following error:
Unknown Qt widget: QtCharts.QChartView
I'm using linux with qt 5.7 and qtcharts and also pyqtcharts.
I thing the problem is the 's' at the end of QtCharts, but I have no idea how I can fix it.
Hope someone has a idea.
You don't have to integrate it.
Add a normal Widget in qt-designer, then right click it, and select Promote to ....
In the window that opens, write QChartView for the Promoted class name:, and PyQt5.QtChart for the Header file:. Press Add. It will be added to the list of Promoted Classes. Select it from the list, and press Promote. That's it.
Then in your python code, you can write something like this:
from PyQt5.QtChart import QChart, QLineSeries
...
chart = QChart()
series = QLineSeries()
series.append(1,3)
series.append(2,4)
chart.addSeries(series)
chart.setTitle('Example')
chart.createDefaultAxes()
self.ui.widget.setChart(chart) # this is the view you added in qt-designer
Make sure you have pyqtchart installed (with pip).
Related
A long time ago, I wanted to make a logo appear on top of the text in a QPushButton stacked on top of each other, but I couldn't find anyway
I read some stylesheets (couldn't find a single doc to read it all about all styles I can apply to a button)
tried the setLayoutDirection (RightToLeft and LeftToRight were there, but no UpToDown direction)
In my (I wish) last attempt I tried to inherit a QAbstractButton (I didn't find QAbstractPushButton, so I guess QAbstractButton is the answer) and change its paintEvent/paintEngine to draw an image or maybe add a vbox inside it as a layout to draw to components, but I can't find anything in python (specially PySide) which has an example in any possible way close to that. The best thing I found was the analogue clock example which was not very helpful because it was trying to work a QWidget and not a QAbstractButton and I want to keep the feel of a Native looking button.
I like my final product to be something like this.
source of the implemention of that
Python Enaml toolkit supported this feature out of the box (in one of its widgets), and I know it is QT based, so I really wish to know how it is possible?
p.s.: Also, is there a market for qt widgets? e.g.: a plugin system. Because rewriting an android like switch doesn't seem like the correct thing that I should do! even a good tutorial or doc would be appreicated (excluding official doc)
It is easier than you think, you can use QToolButton() like this:
import sys
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QVBoxLayout,QStyle, QWidget,
QToolButton
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
button = QToolButton()
# here you choose the position of the icon and its text
button.setToolButtonStyle(
Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
# here I just use built-in icon by PySide6 for this example
name = 'SP_DialogSaveButton'
pixmapi = getattr(QStyle, name)
icon = self.style().standardIcon(pixmapi)
# here we set text and icon of size 32x32 to the button
button.setIcon(icon)
button.setText("Sample text")
button.setIconSize(QSize(32, 32))
# finally we add our button to the layout
lay = QVBoxLayout(self)
lay.addWidget(button, alignment=Qt.AlignCenter)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
My problem is that I have a file with my UI called xxx.ui. Then as many have suggested I created another python file called test.py where I have put code to use my xxx.ui:
# imports
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import uic
import sys
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('xxx.ui', self)
self.show()
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
Up until this stage everything works ok. But now I would like to add a checkbox to my UI whe program starts without messing inside xxx.ui (so the checkbox will be created dynamicaly when the program runs).
How can I do that ???
Thank You in advance.
After many fail attempts I have found how to do it:
To add Checkbox outside of xxx.ui file. I went to my test.py file and added code below this line:
uic.loadUi('xxx.ui', self)
The code looks like that (I am using horizontal layout widget created in designer called seasonLayout and my checkbox is inside that layout):
self.checkBox = QtWidgets.QCheckBox(self.horizontalLayoutWidget)
self.checkBox.setObjectName("checkBox_0")
self.checkBox.setText('Hello')
self.seasonLayout.addWidget(self.checkBox)
Then if You want to get to that object all You have to do is to use code below:
(here i change text of this newly created checkbox):
self.checkBoxs = self.findChild(QtWidgets.QCheckBox, 'checkBox_0')
self.checkBoxs.setText('test')
Hopefuly it will be helpful for other because I have really tried to find answer for that almost everywhere and everyone were just using widgets from designer - noone explain how to add them outside of it.
I need to add some features to a graphics widget in a form I created using the Qt Designer.
For example I would normally do something like this:
class custom_gv(QGraphicsView):
def __init__(self):
super().__init__()
def zoom(self):
# custom code here
But in this case the graphics view is a part of the window I made in Qt Designer. I know you can use the "promote to" feature in Qt designer but I don't know how to utilise that in code, especially considering that I use this method to use Qt Designer windows:
from PyQt5.uic import loadUiType
custom_window = loadUiType('ui.ui')
class Window(QMainWindow, custom_window):
def __init__(self):
QMainWindow.__init__(self)
custom_window.__init__(self)
self.setupUi(self)
So how would I go about customising the code of the graphics view in my window when I use Qt Designer?
The most common way to solve this is by using widget promotion. This will allow you to replace a widget defined in Qt Designer with your own custom class. The steps for doing this are as follows:
In Qt Designer, select the QGraphicsView you want to replace, then right-click it and select Promote to... . In the dialog, set Promoted class name to "custom_gv", and set Header file to the python import path for the module that contains this class (e.g. "mypkg.widgets"). Then click Add, and Promote, and you will see the class change from "QGraphicsView" to "custom_gv" in the Object Inspector pane.
When the Qt Designer ui file is converted into PyQt code, it will automatically add an import statement like this:
from mypkg.widgets import custom_gv
and then in the converted code it will replace something like this:
self.graphicsView = QtWidgets.QGraphicsView(MainWindow)
with this:
self.graphicsView = custom_gv(MainWindow)
So the code in the ui file knows nothing about the custom class: it's just a name that is imported from elsewhere. That means you are completely free to write the custom class in any way you like.
In PyQt, this mechanism works in the same way with pyuic as it does with the uic module. The loadUi and loadUiType functions generate exactly the same code as pyuic does. The only difference is that the pyuic tool writes the generated code to a file, whereas the uic module loads it directly via exec.
I need to add some features to a graphics widget in a form I created using the Qt Designer.
For example I would normally do something like this:
class custom_gv(QGraphicsView):
def __init__(self):
super().__init__()
def zoom(self):
# custom code here
But in this case the graphics view is a part of the window I made in Qt Designer. I know you can use the "promote to" feature in Qt designer but I don't know how to utilise that in code, especially considering that I use this method to use Qt Designer windows:
from PyQt5.uic import loadUiType
custom_window = loadUiType('ui.ui')
class Window(QMainWindow, custom_window):
def __init__(self):
QMainWindow.__init__(self)
custom_window.__init__(self)
self.setupUi(self)
So how would I go about customising the code of the graphics view in my window when I use Qt Designer?
The most common way to solve this is by using widget promotion. This will allow you to replace a widget defined in Qt Designer with your own custom class. The steps for doing this are as follows:
In Qt Designer, select the QGraphicsView you want to replace, then right-click it and select Promote to... . In the dialog, set Promoted class name to "custom_gv", and set Header file to the python import path for the module that contains this class (e.g. "mypkg.widgets"). Then click Add, and Promote, and you will see the class change from "QGraphicsView" to "custom_gv" in the Object Inspector pane.
When the Qt Designer ui file is converted into PyQt code, it will automatically add an import statement like this:
from mypkg.widgets import custom_gv
and then in the converted code it will replace something like this:
self.graphicsView = QtWidgets.QGraphicsView(MainWindow)
with this:
self.graphicsView = custom_gv(MainWindow)
So the code in the ui file knows nothing about the custom class: it's just a name that is imported from elsewhere. That means you are completely free to write the custom class in any way you like.
In PyQt, this mechanism works in the same way with pyuic as it does with the uic module. The loadUi and loadUiType functions generate exactly the same code as pyuic does. The only difference is that the pyuic tool writes the generated code to a file, whereas the uic module loads it directly via exec.
I wrote a lot of custom widget plug-ins for using them in the Qt Designer and my Pipeline.
This is working fine on my Mac (Mavericks, PyQt4, Python 2.7), and this week I wanted to implement those plugins on my Windows environment as well. But it wasn't working. The plug-ins are not appear in the Qt Designer's Widget Box on the left (Windows 7, PyQt4, Python 2.7).
After a lot of try's, I downloaded the PyQt4 example files and followed the instructions from: PyQt Reference Guide and also the instructions
from the example launcher file examples/designer/plugins/plugins.py itself, but it was still not working.
So I copied the the following example files:
plugin files to "C:\designer_plugins"
widget files to "C:\designer_widgets"
Just to making the code as simple as possible to figure out what is going wrong.
So, this is my testing plugins.py file:
#!/usr/bin/env python
import sys
import os
from PyQt4 import QtCore, QtGui, uic
env = os.environ.copy()
env['PYTHONPATH'] = r"C:\designer_widgets" #("%s"%os.pathsep).join(sys.path)
env['PYQTDESIGNERPATH'] = r"C:\designer_plugins"
qenv = ['%s="%s"' % (name, value) for name, value in env.items()]
# Start Designer.
designer = QtCore.QProcess()
designer.setEnvironment(qenv)
designer_bin = r"C:\Python27x64\Lib\site-packages\PyQt4\designer.exe"
designer.start(designer_bin)
designer.waitForFinished(-1)
sys.exit(designer.exitCode())
I thought that my designer object didn't get the right path, so I implemented the folowing to check it's environment:
# Check if paths are right
print "\n # Designer Env:"
for pypath in designer.environment():
if "PYTHONPATH" in pypath:
print " # ",pypath
if "PYQTDESIGNERPATH" in pypath:
print " # ",pypath
Console output:
# Designer Env:
# PYQTDESIGNERPATH="C:\designer_plug"
# PYTHONPATH="C:\designer_widgets"
The paths are right. And like I said, on my mac it works perfectly.
I was trying this also with different Qt Designer installations. But both (PyQt4 and PySide) designers doesn't show any of the example plugins. Both Qt Designer couldn't find the example plugins. I double checked it inside the designer with Help/About Plugins).
Any ideas what I did wrong? Or is this generally not working on a Windows 7 System?
All fixed. I rebuild PyQt4, so I think I made probably a mistake in the source code of Qt in the past.