Skip to content

Language Bindings

You're using Qt with other languages than C++, eh? Post here!
865 Topics 3.4k Posts
  • 0 Votes
    3 Posts
    16k Views
    P
    I figured it out finally, some things around OOP are still very confusing for me, I guess I need time and more experience to settle all down. Below is a functional example of simple window with two QToolButtons - a regular one and modified one. @JonB: I think that there is no difference between using PyQt and PySide, except for import statements, and loading ui, I think that PyQt cannot load ui file directly, it must be converted to py, maybe I'm wrong. Now there are four things that bother me: how to use regular QtToolButton as a classic button with drop-down menu (see my example), it seems that triggered signal is always called. Is it designed to work that way? If I don't add signal in code than button does nothing? are there any major flaws in my example that should be corrected/fixed? how to "inject" my custom made object into ui at wanted location? In my example I just inserted it inside grid_layout. Can I make a placeholder somehow? Let's say I have a more complicated window with many widgets, for example: [image: 15b98bb6-b3ab-4265-bd5f-99236b3670b3.jpg] and I want to replace that Add Something button with custom one. What would be the best procedure to do this? 4) is calling GUI directly from a ui XML file smart thing to do or it would be better to convert it to py first? My example, program.py, and form.ui: #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Docs """ import os, sys from PySide2 import QtWidgets, QtUiTools class CustomToolButton(QtWidgets.QToolButton): ''' CustomToolButton description ''' def __init__(self, parent=None): ''' Constructor ''' super(CustomToolButton, self).__init__(parent) self.setPopupMode(self.MenuButtonPopup) self.triggered.connect(self.setDefaultAction) return None class Program(QtWidgets.QWidget): ''' Program description ''' def __init__(self, parent=None): ''' Constructor ''' super(Program, self).__init__(parent) self.program_path = os.path.dirname(os.path.realpath(__file__)) self.loader = QtUiTools.QUiLoader() ui_file_path = os.path.join(self.program_path, 'form.ui') self.window = self.loader.load(ui_file_path, parent) self.setup_ui(self.window) return None def setup_ui(self, window): self.grid_layout = window.gridLayout # tool button created in Qt Designer self.tool_btn = window.toolButton ### THIS RUNS EVERY TIME BUTTON IS CLICKED NO MATTER ### WHAT ACTION YOU CHOOSE FROM DROP-DOWN MENU ### IT SEEMS THAT IN THIS CASE YOU CANNOT USE BUTTON LIKE ### CLASSIC BUTTON, ### THEREFORE IT SEEMS THAT "setDefaultAction" MAKES NO SENSE self.tool_btn.triggered.connect(self.clicked_tool_btn) # custom tool button self.custom_tool_btn = CustomToolButton() self.custom_tool_btn.setText('Custom button') self.custom_tool_btn.setMinimumSize(150,50) self.custom_tool_btn.setMaximumSize(150,50) self.grid_layout.addWidget(self.custom_tool_btn) self.define_ui_elements() def define_ui_elements(self): # tool button created in Qt Designer tool_btn_menu = QtWidgets.QMenu(self) tool_btn_menu.addAction('Action 1', self.action1_activated) tool_btn_menu.addAction('Action 2', self.action2_activated) tool_btn_menu.addAction('Action 3', self.action3_activated) tool_btn_default_action = QtWidgets.QAction('Action 1',self) self.tool_btn.setMenu(tool_btn_menu) self.tool_btn.setDefaultAction(tool_btn_default_action) # custom tool button ''' 1) creating actions ''' action1 = QtWidgets.QAction('Action 1', self) action2 = QtWidgets.QAction('Action 2', self) action3 = QtWidgets.QAction('Action 3', self) ''' 2) creating connections ''' action1.triggered.connect(self.action1_activated) action2.triggered.connect(self.action2_activated) action3.triggered.connect(self.action3_activated) ''' 3) creating btn menu ''' custom_tool_btn_menu = QtWidgets.QMenu(self) custom_tool_btn_menu.addAction(action1) custom_tool_btn_menu.addAction(action2) custom_tool_btn_menu.addAction(action3) ''' 4) setting up menu and default action ''' self.custom_tool_btn.setMenu(custom_tool_btn_menu) self.custom_tool_btn.setDefaultAction(action1) ### THIS RUNS EVERY TIME BUTTON IS CLICKED NO MATTER WHAT ACTION YOU CHOOSE FROM DROP-DOWN MENU def clicked_tool_btn(self): # calling action1_activated() because it is default option self.action1_activated() print('Qt Designer button is clicked.') def action1_activated(self): print('Action 1 activated.') def action2_activated(self): print('Action 2 activated.') def action3_activated(self): print('Action 3 activated.') if __name__ == '__main__': program = QtWidgets.QApplication(sys.argv) main_window = Program() main_window.window.show() sys.exit(program.exec_()) <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>400</width> <height>300</height> </size> </property> <property name="maximumSize"> <size> <width>400</width> <height>300</height> </size> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QGridLayout" name="gridLayout"> <item row="1" column="0"> <widget class="QToolButton" name="toolButton"> <property name="minimumSize"> <size> <width>150</width> <height>50</height> </size> </property> <property name="maximumSize"> <size> <width>150</width> <height>50</height> </size> </property> <property name="text"> <string>QtDsgnrBtn</string> </property> <property name="popupMode"> <enum>QToolButton::MenuButtonPopup</enum> </property> </widget> </item> </layout> </widget> <resources/> <connections/> </ui>
  • 0 Votes
    4 Posts
    1k Views
    SGaistS
    That was already clear. The question was: can you reproduce that behaviour easily ? Can you share a minimal compilable example showing that behaviour ?
  • Python/PyQt QDialog with connect lambda "leaks"

    Solved
    13
    1 Votes
    13 Posts
    6k Views
    JonBJ
    For the original problem, and https://stackoverflow.com/a/48501804/489865, stating "a connected lambda which references self will not be garbage collected", I received the following code function from the PyQt mailing list. I am not going to use it myself unless I have to (I prefer to rewrite code so as not to use self-referencing lambdas), but I paste it here (untested; and the author states it's for Python 2.7 but should work for Python 3) in case other readers in future might be interested. def connect_lambda(bound_signal, self, func, **kw): # It can be used like this: # Instead of # self.editingFinished.connect(lambda: self.whatever()) # do # connect_lambda(self.editingFinished, self, lambda self: self.whatever()) import weakref r = weakref.ref(self) del self num_args = func.__code__.co_argcount - 1 if num_args < 0: raise TypeError('lambda must take at least one argument') def slot(*args): ctx = r() if ctx is not None: if len(args) != num_args: args = args[:num_args] func(ctx, *args) bound_signal.connect(slot, **kw)
  • Kluge to get Qt to work with CUDA v9.2.. must be a better way

    Unsolved
    6
    3 Votes
    6 Posts
    2k Views
    SGaistS
    Qt Creator is open source, you can modify it at will. What @VRonin meant is that the current version of Qt Creator doesn't provide such an option. You might want to open a feature request for it.
  • PyQt - Need help with QLineEdit QRegExp better understanding of what I'm seeing

    Solved
    4
    0 Votes
    4 Posts
    3k Views
    JonBJ
    @nightpoison If you are starting out now and choosing to use Qt's regular expressions (I use Python's), you really should use the newer QRegularExpression class (http://doc.qt.io/qt-5/qregularexpression.html), rather than QRegExp. Your example is indeed best handled via a numeric range validator, but there are plenty of other cases where a regular expression is suitable.
  • QML to Python

    Unsolved
    2
    0 Votes
    2 Posts
    2k Views
    SGaistS
    Hi and welcome to devnet, There's nothing to convert. main.py: #!/usr/bin/env python import sys from os.path import join from os.path import dirname from PyQt5.QtCore import QUrl from PyQt5.QtGui import QGuiApplication from PyQt5.QtQuick import QQuickView if __name__ == "__main__": app = QGuiApplication(sys.argv) url = QUrl(join(dirname(__file__), 'main.qml')) view = QQuickView() view.setSource(url) view.show() sys.exit(app.exec_()) main.qml: import QtQuick 2.0 Rectangle { width: 360 height: 360 Text { text: qsTr("Hello Qt") anchors.centerIn: parent } MouseArea { anchors.fill: parent onClicked: { Qt.quit() } } }
  • qtreeview, get mouse-selected-item into cell-editing

    Unsolved
    1
    0 Votes
    1 Posts
    562 Views
    No one has replied
  • Random exits code -1073741819 after switch to PySide2

    Solved
    3
    0 Votes
    3 Posts
    1k Views
    ewerybodyE
    I think I solved it! Although I didn't have a clue what caused it exactly I once just stepped through the whole thing and made some changes accordingly. Here is the commit that made my app work again. Basically made a widget in a tab page with some HTML content arranged in Qt Designer load on demand and not on startup right away. It mostly crashed when the translation kicked in on the setText like self.a2license_text.setText(QtWidgets.QApplication.translate( ...and here the HTML stuff ... And then I added some more on demand widget builds removing about 80 widgets from the startup. Probably better to have it as slim as possible anyway.
  • Embed Python interpreter using PySide in a QWidget C++ application

    Unsolved
    3
    0 Votes
    3 Posts
    2k Views
    ewerybodyE
    I dunno C++ well enough but this kind of stuff is surely possible. For instance Maya does it just the same way. Or actually Cryengine Sandbox! 😃 Here is the code on github!
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    12 Views
    No one has replied
  • Some issues with QTextEdit (PySide2)

    Unsolved
    3
    0 Votes
    3 Posts
    992 Views
    K
    Hi, I encountered it when developing a simple app. I was able to deal with it somehow, thus this is meant as a notice about these issues for Qt people. The missing signaling on select-all event can be seen by commenting out self.highlight_current_line() at (one of) def selection_changed(self): there. The issue with line highlighting of right-aligned lines can be seen by adding self.setAlignment(QtCore.Qt.AlignRight) at one of the QTextEdit-based classes, like at class CmdTextEdit(QTextEdit): e.g. just before self.setUndoRedoEnabled(True) by the end of its __init__: self.setAlignment(QtCore.Qt.AlignRight) self.document().setModified(False) self.setUndoRedoEnabled(True) with the self.document().setModified(False) being put there to convince Qt that this setting is still within initialization. BTW I've seen at the Qt example that line numbers are thought to be drawn by painters, though the painter did not want to work for me (complaining about something, may be about being not active, not sure now). And since I did not know how to convert that example onto partially-seen lines anyway, I did it a different way.
  • 0 Votes
    7 Posts
    5k Views
    SGaistS
    It's essentially the same, most of the time you have some translation to do from one language to the other, but the logic is exactly the same.
  • Qtextdit format A4

    Unsolved
    1
    0 Votes
    1 Posts
    587 Views
    No one has replied
  • Advanced design techniques

    3
    0 Votes
    3 Posts
    877 Views
    I
    Thank you! Second example is really what I wanted!
  • Runing a .exe inside a form

    Unsolved
    2
    0 Votes
    2 Posts
    934 Views
    _
    Hi HugoMandes, see the Qt-Documentation -> http://doc.qt.io/qt-5/qprocess.html It's also possible to get the window handle from the Qt Widget (http://doc.qt.io/qt-5/qwidget.html#winId). But in the past I want to embedded the Microsoft Excel inside the Qt application and this not work very well. I think it's easier to use only the microsoft eco system if possible - or a work around before embedding/drawing/event-handling a application inside a Qt application. Regards, Jakob
  • Qtreeview &QSortFilterProxyModel, select/scroll/expand to entry (pyqt5)

    Solved
    3
    0 Votes
    3 Posts
    2k Views
    H
    @SGaist said in Qtreeview &QSortFilterProxyModel, select/scroll/expand to entry (pyqt5): mapFromSource Thanks for your answer! While I was so sure, I had tried it already, obviously I had not.... It now works.! Thanks a lot!!
  • how to create custom style class on PyQt4 / PyQt5

    Unsolved
    5
    0 Votes
    5 Posts
    2k Views
    J
    @SGaist thx for the hint. Will look into it
  • trying to open .so with QLibrary

    Unsolved qlibrary open .so
    2
    0 Votes
    2 Posts
    911 Views
    SGaistS
    Hi and welcome to devnet, Are you sure you library can be found ? What version of Qt are you using ? On what platform ?
  • qtreeview select single item & column

    Solved
    3
    0 Votes
    3 Posts
    3k Views
    H
    Thanks a lot for the hints! I had tried these without success but your input triggered me to look further in this direction and I discovered something called selectionbehavior. In pyqt5 I use: self.view.setSelectionBehavior(QAbstractItemView.SelectItems) Thanks again!
  • pyqt5 qtreeview QAbstractItemModel set coloring of single column based on function?

    Solved
    3
    0 Votes
    3 Posts
    1k Views
    H
    Great, I had missed that.... I simply could not believe that it is actually that easy... Thanks a lot!