qt 槽的使用心得

来源:互联网 发布:廖雪峰的python 编辑:程序博客网 时间:2024/06/04 17:43

1.button 槽使用。

1.1 直接ui点击button,右键选择go to slot  出现go to slot对话框,里面有clicked(),pressed(),resealed()等事件,点击ok后,会在相应文件中出现相对的代码.

如clicked(); 

会在dialog.cpp出现

void Dialog::on_pushButton_clicked()
{
}

会在dialog.h出现

private slots:
    void on_pushButton_pressed();

保存全部后,可以再 on_pushButton_pressed方法内添加相应的代码.

void Dialog::on_pushButton_clicked()
{

  qDebug("======0========");
}

1.2

dialog.h加入

private slots:
    void button2();

dialog.cpp加入

void Dialog::button2(){
    qDebug("======1========");
}

dialog.cpp初始化加入以下

 QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(button2()));

1.3QLineEdit 等其他组件都可以应用以上两种方法。

2类之间的数值传送

dialog.h加入

private slots:

   void bbb2(int i);

dialog.cpp主函数加入

bbbb *bb0 = new bbbb();
QObject::connect(bb0,SIGNAL(bbb1(int)),this,SLOT(bbb2(int)));
 
bb0->cccc(2);

再加入

void Dialog::bbb2(int i)
{
     qDebug("======4========%d",i);
}

bbbb.h

#ifndef BBBB_H
#define BBBB_H
#include <QWidget>
class bbbb : public QWidget
{
    Q_OBJECT
public:
    explicit bbbb(QWidget *parent = 0);
     void cccc(int i);
signals:
    void bbb1(int i);
public slots:
};
#endif // BBBB_H

bbbb.cpp

#include "bbbb.h"
bbbb::bbbb(QWidget *parent) :
    QWidget(parent)
{
}
void bbbb::cccc(int i){
    emit bbb1(i);
}

1.4

如果碰上 没用过的槽函数 ,如下应用

例:


QQmlComponent

F1查看帮助

Signals

voidprogressChanged(qreal progress)voidstatusChanged(QQmlComponent::Status status)代码:

componentsinit.cpp
ComponentsInit::ComponentsInit(QQmlEngine *engine,QObject *parent) :
    QThread(parent)
{
    
  QQmlComponent* component = new QQmlComponent(engine, QUrl(lineStr),QQmlComponent::Asynchronous);
  connect(component,SIGNAL(statusChanged(QQmlComponent::Status)),this, SLOT(statusChanged(QQmlComponent::Status)));
}
void ComponentsInit::statusChanged(QQmlComponent::Status status) {
    if (status == QQmlComponent::Ready) {
       qDebug() <<"---AAAA---";
    }
}

componentsinit.h
public slots:
    void statusChanged(QQmlComponent::Status);








原创粉丝点击