Qt中常用connect用法总结

为方便演示,先自定义一个 Button,然后定义两个重载的信号
lass MyButton : public QWidget
{
Q_OBJECT
public:
explicit MyButton(QWidget *parent = nullptr);
signals:
void sigClicked();
void sigClicked(bool check);
};
那么在用这个 Button 的时候连接这两个信号,应该是这样:
connect(m_pBtn,SIGNAL(sigClicked()),this,SLOT(onClicked()));
connect(m_pBtn,SIGNAL(sigClicked(bool)),this,SLOT(onClicked(bool)));

m_pBtn: 按钮名字

sigClicked():每种信号中()的内容根据情况而定。
例如:
connect(locationEdit, SIGNAL(returnPressed()),this, SLOT(changeLocation()));
connect(reply, SIGNAL(finished()), this, SLOT(slotSourceDownloaded()));

onClicked():为定义的槽函数
例如:

void MainWindow::slotSourceDownloaded()
{
QNetworkReply* reply = qobject_cast(const_cast(sender()));
QTextEdit* textEdit = new QTextEdit(NULL);
textEdit->setAttribute(Qt::WA_DeleteOnClose);
textEdit->show();
textEdit->setPlainText(reply->readAll());
reply->deleteLater();
}

槽函数会在信号发送的时候直接被调用

你可能感兴趣的:(C语言)