信号与槽(SIGNAL-SLOT)

一、系统设置槽-信号

  • 在QtCreator中建立槽信号事件

信号与槽(SIGNAL-SLOT)_第1张图片
槽的建立(添加Button)

注意:我们的文件 ui_zbtx.h是编译环境(VS及其插件QtCreator)产生的描述程序(类似QtCreator(Xcode)自动布局产生控件)

  • 设置槽事件
//.h
public slots:
   void on_btn_addpara_click();
   void on_btn_delpara_click();
   void on_btn_save_clicked();

  • 设置信号事件
//.cpp
void zbtx::on_btn_addpara_click()
{
    ParaSetting dlg;
    if (dlg.exec() == QDialog::Accepted)
    {
        int row = ui.tbl_paras->rowCount();
        ui.tbl_paras->insertRow(row);
        QTableWidgetItem* it = new QTableWidgetItem;
        //it->setText(dlg.name);
        it->setText(dlg.sign);
        ui.tbl_paras->setItem(row, 0, it);
        it = new QTableWidgetItem;
        //it->setText(dlg.typ == 1 ? tr("data") : tr("sub indicator"));
        it->setText(dlg.typ == 1 ?QStringLiteral("数据") : QStringLiteral("子指标"));
        it->setData(Qt::UserRole, dlg.typ);
        ui.tbl_paras->setItem(row, 1, it);
        it = new QTableWidgetItem;
        it->setText(dlg.ch);
        ui.tbl_paras->setItem(row, 2, it);
        /*it = new QTableWidgetItem;
        it->setText(QString::number(dlg.id));
        ui.tbl_paras->setItem(row, 3, it);*/
    }

}
void zbtx::on_btn_delpara_clicked(){
    ui.tbl_paras->removeRow(ui.tbl_paras->currentRow());

}
信号与槽(SIGNAL-SLOT)_第2张图片
槽信号事件

二、自定义槽信号事件

  • 设置对应的槽信号事件


    信号与槽(SIGNAL-SLOT)_第3张图片
    on_tree_zhibiao_customContextMenuRequested
信号与槽(SIGNAL-SLOT)_第4张图片
on_tree_zhibiao_currentItemChanged
  • 设置槽事件
//.h
public slots:
   void on_tree_zhibiao_currentItemChanged(QTreeWidgetItem * current, QTreeWidgetItem * previous);//修改Tree Item 
   void on_btn_modify_zb_clicked();//应用
   void on_tree_zhibiao_customContextMenuRequested(const QPoint & pos);//tree Item 右选择键 操作
  • 设置信号事件
//.cpp
void zbtx::on_tree_zhibiao_customContextMenuRequested(const QPoint & pos)
{

    QTreeWidgetItem * current = ui.tree_zhibiao->currentItem();
    if (!current)
        return;
    int ntyp = current->data(0, Qt::UserRole).toInt();

    QMenu menu;
        if (ntyp == 0)//
    {
         //建立槽SLOT(add_zb_sub_level())  【两次槽-信号的建立】
        menu.addAction(QStringLiteral("添加子指标"), this, SLOT(add_zb_sub_level()));//添加子指标
        menu.addAction(QStringLiteral("删除指标"), this, SLOT(delete_zb()));//删除指标
    }
        menu.exec(QCursor::pos());
}

void zbtx::add_zb_sub_level()
{
    QTreeWidgetItem * current = ui.tree_zhibiao->currentItem();
    if (!current)
        return;
    QTreeWidgetItem* it = new QTreeWidgetItem;
    it->setText(0, QStringLiteral("添加子指标"));
    it->setData(0, Qt::UserRole-1, g_zb_id++);//id
    it->setData(0, Qt::UserRole, 0);//indicator
    current->addChild(it);
    current->setExpanded(true);
}
void zbtx::delete_zb()
{
    QTreeWidgetItem * current = ui.tree_zhibiao->currentItem();
    if (!current)
        return;
    if (!current->parent())
        return;
    ui.tree_zhibiao->clearSelection();
    current->parent()->removeChild(current);
}

一、系统自定义槽信号[改]

在项目中,场景SceCreator.h中,我们的评估方案中

//.h
private:

    void connectSlots();         //连接signals-slots


private slots:


    void on_newEval_clicked();  //评估指标 新建方案

实现槽信号

//.cpp
void CSceCreator::connectSlots()
{
//评估方案管理
    connect(ui.newEval,   SIGNAL(clicked()),    this,   SLOT(on_newEval_clicked()));//注:ui.newEval 为QtCreator中 控件button的命名 name



}

实现槽函数

void CSceCreator::on_newEval_clicked()
{
    m_zb_doc.clear();
    m_evalIndDock->new_plan();
}

备注:

  1. 针对QLineEdit、QTextEidt等输入框 信息的保存
//弹出的属性设置对话框(QDialog)
void ParaSetting::accept()
{
    typ = ui.comb_typ->currentIndex() + 1;
    ch = ui.le_ch->text();
    sign = ui.le_sign->text();
    //name = ui.comb_para->currentText();
    //id = ui.comb_para->itemData(ui.comb_para->currentIndex(), 0).toInt();
    QDialog::accept();
}
  1. 关于每个控件对应的槽信号 怎么去区分与响应
//在QtCreator创建槽信号(Edit Signal/Slots)
//ui_zbtx.h

QObject::connect(btn_addpara, SIGNAL(clicked()), zbtx, SLOT(on_btn_addpara_click()));
QObject::connect(btn_delpara, SIGNAL(clicked()), zbtx, SLOT(on_btn_delpara_clicked()));
QObject::connect(btn_save, SIGNAL(clicked()), zbtx, SLOT(on_btn_save_clicked()));

QMetaObject::connectSlotsByName(zbtx);

你可能感兴趣的:(信号与槽(SIGNAL-SLOT))