How to create PySide widget in C/C++ - python

I have a very complex custom widget derived from QWidget in PySide.
At first its paintEvent was taking more than 1 second to complete. Then I've implemented a lot of caching with QPixmap to each layer of the "image" that I'm drawing. Now my paintEvent finishes in about 90ms - which is really fine, but not enough.
I'm wondering if I can implement this custom widget in plain C or C++, and then use it as an abstract widget in PySide (like PySide does with all other available widgets).
I found here that in PyQt I could use sip for this. But I can't find a match for this in PySide.
Does any one know how to do that?

Related

Why the widget btn1 in the subpage of QStackedWidget can only define signals for QStackedWidget, but not for page: deviceTypeSelect in QStackedWidget?

I want to use btn1 to send a signal to the slot function of deviceTypeSelect, but btn1 can only be associated with QStackedWidget.
There are two problems.
The first is conceptual: Designer cannot know anything about custom slots that are going to be implemented in a promoted widget, and it even should not. The connection to those slots can only be achieved programmatically, and that's for good reasons: Designer cannot "inspect" (nor it should) the source of the files that will be promoted, and it's also completely possible that one would promote a widget and use different classes with the same design files.
The other problem is technical: the "pages" of multipage widgets like QStackedWidget or QTabWidget cannot be used as signal targets on Designer. I don't know if it's just a missing feature or it's by design (knowing how multipage widget plugins are dealt with I wouldn't bet about the it), but unfortunately it really is not possible.
If you want to connect to standard QWidget slots (setEnabled() etc.), set a dummy boxed layout on the page, and add another QWidget in it, but if you use custom slots, as explained before, those must be manually connected from your code.

Is it possible to set QApplication style in QtDesigner?

I am currently working on a GUI application using PyQt5 and QtDesigner. As I have to make it multi-platform (at least Ubuntu and W10) I will use the "Fusion" style to make it look similar on both platforms.
I was wondering if one could simply set the QApplication style directly in QtDesigner ? I know that a simple <<.setStyle("Fusion")>> will do the trick in the code, but does it exist within QtDesigner so the lines can be automatically generated ?
I'm trying to learn QtDesigner and how much it can be pushed before going into the code.
No, it is not possible to set the style through Qt Designer.
What Qt Designer does allow is to display the GUI with different styles if you select Form-> Preview -> ...

tkinter custom widget collecting

I'm searching for a tkinter custom widget collection that I can include in a application designer I'm writing in 100% Python but haven't had much luck yet. I figured out a way to do a table for instance, but would like to save myself the work if there's a good implementation out there.
I found a couple of packages for pure Python custom widget creation with a little more searching online. One is Python megawidgets, at pmw.sourceforge.net, which, according to their documentation:
"is a toolkit for building high-level compound widgets in Python using the Tkinter module. It consists of a set of base classes and a library of flexible and extensible megawidgets built on this foundation. These megawidgets include notebooks, comboboxes, selection widgets, panes widgets, scrollable widgets, and dialog windows."
A different approach is writing custom widgets yourself using the Widget Construction Kit, at effbot.org/zone/wck.htm. This provides a base Widget class with primitive drawing methods, such as for borders, text, and colors, along with a basic but complete set of event definitions for binding your event handlers to your custom widgets. It has some good advice on doing animated widgets, such as drag and drop.
If anybody knows of any other packages of widgets or construction toolkit APIs, feel free to post it here. Developers will appreciate having a larger selection in a single location.

Custom Qt Widgets with python for Qt Designer

I am trying to write a custom widget for the Qt Designer using only Python. I was following a couple of tutorials I found online but none of them were working or anything close to what I would call to be a minimum working example.
So my questions are:
What steps are involved to make a a custom widget appear in the Widget Box of Qt Designer?
If you can spare the time: Please provide a minimum working example (like a widget with a label in it saying "A truly minimal working Qt custom widget example").
Or is it maybe not possible at all to include a custom widget using only python?
There are very few examples available on how to make a custom widget in pyqt. I wrote this article with a working example: Making a Custom Widget in PyQt
Here is the answer to your question #3: How do I use promote to in Qt Designer in pyqt4?
I am using PySide and it works the same way. This method works directly with your Python custom widget code. You do not need to write any separate plugin code.
After you have promoted your custom widget, you can right click on it and add your signals with "Change signals/slots..."
I would recommend putting all you widgets in a YourCostumWidgetsPack.UI file, and then when you load this file in Qt Designer, in addition to the UI you are working. It will load all your custom widget information.
I found this article to be your answer: https://doc.qt.io/archives/qq/qq26-pyqtdesigner.html
But, I haven't been able to install it in Qt Designer though :D

Trying to learn PyQt with knowledge from Tkinter

Maybe I'm jumping into the deep end, but I'll give it a shot.
Here are some useful features of Tkinter:
The Tkinter Canvas widget is an object oriented drawing canvas. The elements of the drawing are essentially widgets themselves, as they can be moved, modified, and bound to events.
Tkinter uses bindings to trigger callbacks. The event is passed as a string. Custom events can be easily created with event_generate.
Tkinter has the after method, which waits for a specified amount of time without freezing the GUI.
Tkinter has predefined fonts like TkDefaultFont, and colors like systemButtonFace, which are dependant on the system.
My questions are:
What are the pyQt equivalents of these features (especially the bold ones)?
How can I "bind" elements of a widget (e.g. the label of a checkbutton only) to an event?
In Qt and PyQt events are called signals and you bind to them using slots (docs here). Generally speaking what you do define a slot with an # decorator.
class WindowImpl (QtGui.QMainWindow, Ui_TremorMain, Ui_Graphs):
def __init__ (self, buffer, parent = None, configuration = None):
# do some initialisation here (not GUI setup however)
#QtCore.pyqtSlot(int, name="on_confSelectorCombo_currentIndexChanged")
def confChanged (self, newConf):
# do some stuff here to handle the event
The above would be triggered on the currentIndexChanged event of an object called confSelectorCombo. The setup of the confSelectorCombo is done in the GUI builder or Qt Creator as Nokia has decided to call it. This really is what you want to use to get started. There's tutorials here on using Qt Creator. Obviously you'll want to go through the docs and see what signals are emitted by which widgets.
As for the font stuff all I know is what it says on the docs:
If you have not set a font for your application then the default font on your
machine will be used, and the default font can be different on different
machines. On Windows the default Windows font is used, on X11 the one in qtrc
can be used. If a default font can’t be found, then a font specified by Qt
will be used.
The QStyleSheet and QStyle act as proxies for changing the appearance of widgets (QStylesheet,QStyle).
As for making the application wait I found this
QTime dieTime = QTime::currentTime().addSecs(2);
while( QTime::currentTime() < dieTime ):
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
There is also QThread.sleep() (docs), depending on what kind of an effect you want. Probably also worth looking at the threading support over at Qt docs
Overall in finding information about how to do stuff in PyQt I have found it surprisingly useful to look at the Qt documentation and then just writing the stuff in Python. 9 times out of 10 this works. On another note, it's probably also worth looking into PySide which is another python Qt library. I've haven't used myself before as it has been in the works previously but I noticed that they had released a 1.0.6 version.
UPDATE
Just to reiterate Luke Woodward below, you can use QGraphicsScene and QGraphicsView to render stuff in an object oriented way. The QGraphicsScene doesn't actually render anything it just a scene graph, the QGraphicsView is then used to render the contents of the scene graph. For low level drawing there´s also QPainter - there's a basic drawing tutorial here. It's also worth looking at QGraphicsItem which is the base for all graphics items and
includes defining the item's geometry, collision detection, its painting
implementation and item interaction through its event handlers
docs here. The Context2D provides an HTML canvas (if I'm not mistaken through the use of WebKit). The canvas itself only has a changed slot, but any objects you place on the canvas will/can have more slots. There's a fairly complete looking tutorial on Context2D and Context2DCanvas here. For an explanation as to why so many different ways of rendering stuff, you'll have to ask someone else. My two cents is that is has something to do with the fact that Qt is supposed to work everywhere and Trolltech and later Nokia wanted to provide lots of choice. Luckily the docs are really good.

Categories