一携丁、系統(tǒng)設(shè)置槽-信號(hào)
- 在QtCreator中建立槽信號(hào)事件
注意:我們的文件
ui_zbtx.h
是編譯環(huán)境(VS及其插件QtCreator)產(chǎn)生的描述程序(類(lèi)似QtCreator(Xcode)自動(dòng)布局產(chǎn)生控件)
- 設(shè)置槽事件
//.h
public slots:
void on_btn_addpara_click();
void on_btn_delpara_click();
void on_btn_save_clicked();
- 設(shè)置信號(hào)事件
//.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("數(shù)據(jù)") : QStringLiteral("子指標(biāo)"));
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());
}
二、自定義槽信號(hào)事件
-
設(shè)置對(duì)應(yīng)的槽信號(hào)事件
- 設(shè)置槽事件
//.h
public slots:
void on_tree_zhibiao_currentItemChanged(QTreeWidgetItem * current, QTreeWidgetItem * previous);//修改Tree Item
void on_btn_modify_zb_clicked();//應(yīng)用
void on_tree_zhibiao_customContextMenuRequested(const QPoint & pos);//tree Item 右選擇鍵 操作
- 設(shè)置信號(hào)事件
//.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()) 【兩次槽-信號(hào)的建立】
menu.addAction(QStringLiteral("添加子指標(biāo)"), this, SLOT(add_zb_sub_level()));//添加子指標(biāo)
menu.addAction(QStringLiteral("刪除指標(biāo)"), this, SLOT(delete_zb()));//刪除指標(biāo)
}
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("添加子指標(biāo)"));
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);
}
一掖举、系統(tǒng)自定義槽信號(hào)[改]
在項(xiàng)目中,場(chǎng)景SceCreator.h中,我們的評(píng)估方案中
//.h
private:
void connectSlots(); //連接signals-slots
private slots:
void on_newEval_clicked(); //評(píng)估指標(biāo) 新建方案
實(shí)現(xiàn)槽信號(hào)
//.cpp
void CSceCreator::connectSlots()
{
//評(píng)估方案管理
connect(ui.newEval, SIGNAL(clicked()), this, SLOT(on_newEval_clicked()));//注:ui.newEval 為QtCreator中 控件button的命名 name
}
實(shí)現(xiàn)槽函數(shù)
void CSceCreator::on_newEval_clicked()
{
m_zb_doc.clear();
m_evalIndDock->new_plan();
}
備注:
- 針對(duì)QLineEdit名秀、QTextEidt等輸入框 信息的保存
//彈出的屬性設(shè)置對(duì)話框(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();
}
- 關(guān)于每個(gè)控件對(duì)應(yīng)的槽信號(hào) 怎么去區(qū)分與響應(yīng)
//在QtCreator創(chuàng)建槽信號(hào)(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);