开发者

Qt designer: Custom code for custom actions

开发者 https://www.devze.com 2023-03-22 10:36 出处:网络
I\'ve been looking around and I didn\'t found anything yet, except old doc for Qt3 with version 3.x of qt designer.

I've been looking around and I didn't found anything yet, except old doc for Qt3 with version 3.x of qt designer.

I'll leave an example, not that I can't give code as my project is GPL but for simplicity.

Example: You are designing a GUI for your application. You insert some buttons in the GUI, and you want those buttons do something with a logic that maybe is already written.

Question: How do you add or set this code in a way that the button when it's checked wi开发者_C百科ll fire that code?

I don't want to connect widgets with signals/slots. My approach was to create a custom action in the action editor, and connect the desired button to that action hoping to find a way to write the code for that action. I could define its name, its icon (?), and so on. But I need to write its logic/code.

I read some documentation that instructs you to create a C++ header file, but it seems deprecated for the new Qt Designer version (4.7.3). The other vast resource I found about my question are all about conecting signals between default objetc actions. I repeat that is not what I need/want.

My question born from the fact that I don't want to edit the generated .py. I sense that it MUST be a way to set a file (custom code, header file, etc) with the custom action code but honestly, I can't find anything yet.

In case of a negative answer (that is, "not possible to do so"), It would be nice to hear some advice for "hacking" the code. I really don't like to modify the generated .py and it seems ugly to do something like: ui.callback in my code.

Regards,


You don't need to create a header file or modify the pyuic generated file. Assuming your action object is named myaction and you want to signal myaction_slot when it's toggled:

import sys
from PyQt4 import QtCore, QtGui

# import pyuic generated user interface file
from ui_mainwindow import Ui_MainWindow

class MyMainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)
        # connect myaction_logic to myaction.toggled signal
        self.myaction.toggled.connect(self.myaction_slot)

    def myaction_slot(self):
        pass # do something here

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyMainWindow()
    myapp.show()
    sys.exit(app.exec_())
0

精彩评论

暂无评论...
验证码 换一张
取 消