How do I create a custom slot in qt4 designer?

前端 未结 10 562
我在风中等你
我在风中等你 2020-12-14 18:29

Whenever I use the signal/slot editor dialog box, I have to choose from the existing list of slots. So the question is how do I create a custom named slot?

相关标签:
10条回答
  • 2020-12-14 18:40

    I am able to do it by:

    In MainWindow.h, add the line:

    public slots:
         void example();
    

    in the MainWindow class.

    In MainWindow.cpp

    void MainWindow::example() {
         <code>
    }
    
    0 讨论(0)
  • 2020-12-14 18:42

    It is not possible to do it, because it means you would add a slot to an existing Qt class like QPushButton which is not really the way to go.

    You should create your own QWidget eventually by subclassing an existing one. Then integrating it into Qt Designer as a plugin as suggested. Having your own class allows you to add/modifiy the signals/slots available as you want.

    0 讨论(0)
  • 2020-12-14 18:45

    Maybe it'll help.

    By default you have to choose from the existing list of slots. But you can add slot by right-clicking at you object in the list at right side of designer and choose "slot/signals" and add your custom slot/signal. After that, you can choose it in signal/slot editor.

    0 讨论(0)
  • 2020-12-14 18:49

    Unfortunately this is not possible in Qt4.

    In Qt3 you could create custom slots which where then implemented in the ui.h file. However, Qt4 does not use this file so custom slots are not supported.

    There is some discussion of this issue over on QtForum

    0 讨论(0)
  • 2020-12-14 18:49

    right click on the main window and select "change signals and slots" and add a new slot. It will appear in your signal slot editor.

    0 讨论(0)
  • 2020-12-14 18:49

    You can use the magic slot format of

    void on_objectName_signal() {
    // slot code here, where objectname is the Qt Designer object name
    // and the signal is the emission
    }
    

    The connection to this method is established by the method connectSlotsByName and whenever the signal is emitted, this slot is invoked.

    0 讨论(0)
提交回复
热议问题