首页| 论坛| 消息

标题:【提问】关于Designer里双击自动产生的slot函数是在哪里链接的?
作者:XChinux
日期:2005-09-05 10:45
内容:

  昨天在QtForum上看到有人说在Qt 4.0 Commercial for VS.net 里能够直接双击控件然后产生双击事件slot函数,就能直接在这个函数里写代码了。以前一直没注意这个问题,就试验了一下,果然能够。不过在.ui里并没有生成相关的conect,那么到底是怎样进行signal与slot的连接的?
  我在.ui生成的ui_xx.h里,看到有这样一句话,
QMetaObject::connectSlotsByName(ex46Class);
我想就应该是它了,但我却在qt reference里指找不到它的文档,那么它的规则到底是怎样的?我自己写程序模仿它却又不行。 下面是代码,请注意红色的部分。于是,我自己就又自己在代码里加上了另一个按钮的事件处理(绿色的),模仿前面的那两个红色的,竟然也能和前面的同样使用。
这是头文件:
#ifndef EX46_H
#define EX46_H
#include
#include "ui_ex46.h"
class ex46 : public QWidget
{
Q_OBJECT
public:
ex46(QWidget *parent = 0);
~ex46();
private:
Ui::ex46Class ui;
private slots:
void on_pushButton_clicked();
void on_pushButton_4_clicked();
void on_pushButton_3_clicked();
};
#endif // EX46_H
这是.cpp文件:
#include
#include "ex46.h"
ex46::ex46(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
ex46::~ex46()
{
}

void ex46::on_pushButton_4_clicked()
{
close();
}
void ex46::on_pushButton_clicked()
{
QMessageBox::about(this, QString::fromLocal8Bit("你好哇中国"), QString::fromLocal8Bit("这是信息内容"));
}

void ex46::on_pushButton_3_clicked()
{
QMessageBox::aboutQt(this, "this is a test");
}
还有.ui生成的头文件:
#ifndef UI_EX46_H
#define UI_EX46_H
#include
#include
#include
#include
#include
#include
#inclu ..


#1 [giscn 09-05 11:13]
文档里关于signal和slot里有讲,楼主没有仔细看
规则:
void on_widget名_signal名(signal的参数)
{}
当有Q_OBJECT存在的时候,moc编译后,会自动根据名字 connect到对应的signal上
#2 [XChinux 09-05 11:16]
哦,我在索引里面没有找到,在QMetaObject的成员里也没有找到,原来它藏在那里
哈哈,独立写也搞定了。我在MinGW里也试验成功了。
#3 [hitaeolus 09-13 09:59]
各位老大,我用的是redhat ES 3的QT3.1.2
关于signal和slot里的
void on_widget名_signal名(signal的参数)
在哪里有讲呀????
#4 [XChinux 09-13 10:06]
在Qt Assistang里的Using a Component in Your Application节里最下边有这样的一段:
A Dialog With Auto-Connect
Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use uic's auto-connection facilities to connect the OK button's clicked() signal to a slot in our subclass. To do this, we only need to declare and implement a slot with a name that follows a standard convention:
void on__();
Using this convention, we can define and implement a slot that responds to mouse clicks on the OK button:
class ImageDialog : public QDialog, private Ui::ImageDialog
{
Q_OBJECT
public:
ImageDialog(QWidget *parent = 0);
private slots:
void on_okButton_clicked();
};
Automatic connection of signals and slots provides both a standard naming convention and an explicit interface for widget designers to work to. By providing source code that implements a given interface, user interface designers can check that their designs actually work without having to write code themselves.
#5 [hitaeolus 09-13 14:53]
多谢搂主!!!!
在Qt 4.0的文档里找到了
这种功能在QT3里也能用,为什么QT3.0的文档里没介绍呢??

<< 1 2 >> (1/2)

回复 发表
主题 版块