Qt learning summary (C fish) signal and slot 01

auto-association

The first is naturally manual association, as long as the connect function is called, automatic association can be achieved.
Here we focus on the second type, automatic association:
In order to realize the automatic association of slot functions, the signals already provided by Qt widgets can be named according to the following specifications:
void on_<窗口部件名称>_<信号名称>_(<信号参数>);

Automatic association is only applicable to signals that have been defined by Qt components. If it is a signal defined by the programmer, it must be manually associated. Also, for components added to the interface in the Qt designer, you can directly write the above function format to achieve automatic association. But for components that are not added to the interface in the Qt designer, we need to define the component before calling the setUi() function, and also use the setObjectName() function to specify the object name of the component. The point is, the setObjectName() function sets The object name must be the same as the widget name, that is, all names are the same, so that automatic association can be achieved.

E.g:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    //自动关联
    button=new QPushButton(this);
    button->move(50,100);
    button->setText(tr("二重点击"));
    button->setObjectName(tr("button"));//对象名尤其重要,一定要相同
    ui->setupUi(this);
    //手动关联connect(ui->showChildButton,SIGNAL(clicked()),this,SLOT(showChildDialog()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_button_clicked()
{
    QDialog *dialog=new QDialog(this);
    dialog->show();
}

Disconnect signals and slots (disconnect)

Signals and slots can be disconnected by calling disconnect when it is not necessary for them to remain connected.
bool QObject::disconnect (const QObject sender, const char signal, const Object receiver, const char slot) [static]
There are three situations where the disconnect() function must be used:
(1) Disconnect any object associated with an object.

disconnect(sender, 0, 0, 0) ;
//或者
sender->disconnect();

(2) Disconnect any association with a particular signal.
disconnect(sender, SIGNAL(signal()), 0, 0);```
//Or
sender->disconnect(SIGNAL(signal()));
(3) Disconnect the association between two objects.
disconnect(sender, 0, receiver, 0);
//or
sender->disconnect(receiver);

Signal mapper QSignalMapper class

The SignalMapper class can be simply understood as a signal translator and repeater. It can translate a parameterless signal into a signal with an int parameter, a QString parameter, a QObject parameter or a QWidget parameter, and forward it. Often used when multiple signals of the same type are connected to the same slot.

//1.第一步:创建信号映射器对象
QSignalMapper *signal_mapper = new QSignalMapper(this);

//2.让同种却多个信号的部件连接信号映射器map槽,map槽会发射mapped信号,mapped信号的参数由setMapping函数的第二个参数决定。
connect(push_button_1, &QPushButton::clicked, signal_mapper, &QSignalMapper::map);
connect(push_button_2, &QPushButton::clicked, signal_mapper, &QSignalMapper::map);
connect(tool_button_1, &QToolButton::clicked, signal_mapper, &QSignalMapper::map);
connect(tool_button_2, &QToolButton::clicked, signal_mapper, &QSignalMapper::map);

//3.设置翻译的内容,也就是第二个参数
signal_mapper->setMapping(push_button_1, QString::number(BUTTON_1, 10));
signal_mapper->setMapping(push_button_2, QString::number(BUTTON_2, 10));
signal_mapper->setMapping(tool_button_1, QString::number(BUTTON_3, 10));
signal_mapper->setMapping(tool_button_2, QString::number(BUTTON_4, 10));

//4.映射器重新发射信号,执行槽函数
connect(signal_mapper, &QSignalMapper::mapped, this, &MyWidget::changeButton);

void MyWidget::changeButton(QString text)
{
    int index = text.toInt();
    QString information = QString("");
    switch(index)
    {
    case BUTTON_1:
        information = QString("clicked 1");
        break;

    case BUTTON_2:
        information = QString("clicked 2");
        break;

    case BUTTON_3:
        information = QString("clicked 3");
        break;

    case BUTTON_4:
        information = QString("clicked 4");
        break;

    default:
        information = QString("which is clicked?");
        break;
    }
    QMessageBox::information(NULL, QString("Title"), information);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324980736&siteId=291194637