Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13110
  • 博文数量: 5
  • 博客积分: 43
  • 博客等级: 入伍新兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-01 10:38
文章分类

全部博文(5)

文章存档

2014年(1)

2010年(4)

我的朋友

分类:

2010-07-08 15:23:29

信号与槽

1,什么是信号?
信号是Qt类中的成员函数。当某个对象内部发生某些事件的时候,它能够返回信号。如果这个信号链接到槽,那么那个槽(函数)将会执行。

2,什么是槽?
槽也是Qt类中的标准成员函数。但是他们增加了特殊功能使他们能够链接到信号。每当槽所链接信号被发射的时候,槽就会被执行。

3,怎样将信号链接到槽?
要调用Qobject类或者其子类的connect()函数。如: p, li { white-space: pre-wrap; }

connect(b1,SIGNAL(clicked()),qApp,SLOT(quit()

p, li { white-space: pre-wrap; }


4,能否将多个槽链接到一个信号?

可以将槽与多个信号相连。但是当信号被发射时,与之相关的槽被激活的顺序将是随机的。


5,什么时候可以在调用connnect()函数时可以不指定定义他的类?

就是在调用Qobject类以及子类的时候是不必指定定义他的类的。而其他的时候需要指定定义QObject::connect()。


6,能够将被链接的槽和信号断开吗?

可以断开。使用disconnect()函数可以截断槽与信号的关系。


7,怎样将一个信号链接到其他信号?
p, li { white-space: pre-wconnect(b1,SIGNAL(clicked()),qApp,SIGNAL(aa()).如上这样的话就可以在clicked()信号发生的时候产生出列一个信号。


p, li { white-space: pre-wrap; }

#include

#include

#include

#include

#include

#include

class my:public QWidget

{

public:

my();

private:

QPushButton *b1;

QPushButton *b2;

QPushButton *b3;

QLineEdit *ledit;

};

my::my()

{

setGeometry(100,100,500,200);

b1=new QPushButton("CLick here to mark the text",this);

b1->setGeometry(10,10,380,40);

b1->setFont(QFont("Click here to mark the text",16,QFont::Bold));

b2=new QPushButton("CLick here to unmark the text",this);

b2->setGeometry(10,60,380,40);

b2->setFont(QFont("Click here to unmark the text",16,QFont::Bold));

b3=new QPushButton("CLick here to remove the text",this);

b3->setGeometry(10,110,380,40);

b3->setFont(QFont("Click here to remove the text",16,QFont::Bold));

ledit=new QLineEdit("this is a line of text",this);

ledit->setGeometry(10,160,380,30);

connect(b1,SIGNAL(clicked()),ledit,SLOT(selectAll()));

connect(b2,SIGNAL(clicked()),ledit,SLOT(deselect()));

connect(b3,SIGNAL(clicked()),ledit,SLOT(clear()));

// disconnect(b3,0,0,0);

}

//

int main(int argc, char ** argv)

{

QApplication app( argc, argv );

my win;

win.show();

app.exec();

}


编写一个具有两个button对象和一个单选按钮的程序。


p, li { white-space: pre-wrap; }

#include

#include

#include

#include

//#include

//

class my:public QWidget

{

public:

my();

private:

QPushButton *sel;

QPushButton *dds;

QPushButton *exi;

QCheckBox *dda;

};

my::my()

{

setGeometry(100,100,300,200);

sel=new QPushButton("select",this);

sel->setGeometry(10,10,100,40);

dds=new QPushButton("diselect",this);

dds->setGeometry(10,60,100,40);

exi=new QPushButton("Exit",this);

exi->setGeometry(10,110,100,40);

dda=new QCheckBox("test",this);

dda->setGeometry(10,170,80,20);

connect(sel,SIGNAL(clicked()),dda,SLOT(click()));

connect(dds,SIGNAL(clicked()),dda,SLOT(click()));

connect(exi,SIGNAL(clicked()),qApp,SLOT(quit()));

}

int main(int argc, char ** argv)

{

QApplication app( argc, argv );

my win;

win.show();

return app.exec();

}

阅读(667) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~