0

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

#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();
}

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

4

2 回答 2

1

信号和槽必须具有相同的签名。实际上 slot 的签名可以比信号更短:

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

在您的情况下,情况正好相反:插槽具有更长的签名。您可以尝试QSignalMapper创建一个“经纪人”来传递带有附加参数的信号。

于 2012-08-25T02:34:59.010 回答
0

move不是插槽,而是更改pos属性的访问器,它不能直接连接到信号。但是您可以将信号连接到将更改该属性的start()插槽:QPropertyAnimation

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()));
...

或者使该属性值成为 a 状态的一部分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();    
...
于 2012-08-25T23:01:23.987 回答