How do I create a custom slot in qt4 designer?

2020-02-08 12:20发布

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条回答
Anthone
2楼-- · 2020-02-08 12:48

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.

查看更多
小情绪 Triste *
3楼-- · 2020-02-08 12:48

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>
}
查看更多
走好不送
4楼-- · 2020-02-08 12:50

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.

查看更多
可以哭但决不认输i
5楼-- · 2020-02-08 12:53

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

查看更多
Melony?
6楼-- · 2020-02-08 12:53

This doesn't seem to be possible in a simple way.

The designer only allows you to promote existing widgets to your own custom widgets. yet it doesn't allow you to connect the signals and slots of the class of promoted widgets.

The way this is possible is creating a plugin for the designer as is described here and in the pages that follow it.

The normal course of action is to promote a widget to your own class and then to connect it manually in your own code. this process is described here

查看更多
beautiful°
7楼-- · 2020-02-08 13:02

Don't forget about the slot auto-connection features. There are a few drawbacks, like having to rename your function if you rename your widget, but we use those a lot at my company.

查看更多
登录 后发表回答