在Qt QObject :: connect中的问题与move()

时间:2012-08-25 01:41:29

标签: qt

好的,当我运行它时(写下来只是为了显示问题):

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtGUI>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget *window = new QWidget;

    QPushButton *MainInter = new QPushButton("Push me!",window);

    QObject::connect(MainInter,SIGNAL(released()),MainInter,SLOT(move(100,100)));

    window->resize(900,500);
    window->show();
    return a.exec();
}

为什么点击时按钮不移动? :)

2 个答案:

答案 0 :(得分:1)

信号和插槽必须具有相同的signature。实际上,插槽的签名可能比信号短:

http://qt-project.org/doc/qt-4.8/signalsandslots.html

在您的情况下,情况正好相反:插槽的签名较长。您可以尝试QSignalMapper创建一个“代理”来传递带有其他参数的信号。

答案 1 :(得分:0)

move不是一个插槽,而是一个更改pos属性的访问器,它不能直接连接到信号。但您可以将信号连接到start() QPropertyAnimation的{​​{1}}位置,这将改变该属性:

QPushButton *MainInter = new QPushButton("Push me!",window);

QPropertyAnimation *animation = new QPropertyAnimation(MainInter, "pos");
// To make the move instantaneous
animation->setDuration(0);
animation->setEndValue(QPoint(100,100));

QObject::connect(MainInter, SIGNAL(released()), animation, SLOT(start()));
...

或使该属性值成为QStateMachine状态的一部分并使用该信号转换到该状态:

QPushButton *MainInter = new QPushButton("Push me!",window);

QStateMachine *machine = new QStateMachine();
QState *s1 = new QState(machine);
QState *s2 = new QState(machine);
// the transition replaces the connect statement
s1->addTransition(MainInter, SIGNAL(released()), s2);
s2->assignProperty(MainInter, "pos", QPoint(100,100));
machine->setInitialState(s1);
machine->start();    
...