在Qt QObject::使用move()连接问题中

在Qt QObject::使用move()连接问题中,qt,Qt,好的,当我运行这个(写下来只是为了显示问题): #包括 #包括“mainwindow.h” #包括 int main(int argc,char*argv[]) { 质量保证申请a(argc、argv); QWidget*窗口=新的QWidget; QPushButton*mainter=新的QPushButton(“推我!”,窗口); 连接(mainter,信号(released()),mainter,插槽(move(100100)); 窗口->调整大小(900500); 窗口->显示();

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

#包括
#包括“mainwindow.h”
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QWidget*窗口=新的QWidget;
QPushButton*mainter=新的QPushButton(“推我!”,窗口);
连接(mainter,信号(released()),mainter,插槽(move(100100));
窗口->调整大小(900500);
窗口->显示();
返回a.exec();
}

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

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


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

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

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

你最好问一下。你误解了信号和插槽。在Qt中,将一个信号连接到一个插槽,意味着如果插槽函数接受足够的参数,则使用与信号函数相同的参数调用插槽函数。因此,在
connect()
中设置参数将不起作用。非常感谢alexisdm,QProperty动画方法非常有效!:)
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();    
...