Mah*_*led 0 c++ qt qt4 qt-creator
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
button[i][j] = new QPushButton("Empty", this);
button[i][j]->resize(40, 40);
button[i][j]->move(40*j, 40*i);
connect(button[i][j], SIGNAL(clicked()), this, SLOT(changeText(button[i][j])));
}
}
Run Code Online (Sandbox Code Playgroud)
如果我用函数(例如全屏)更改了函数changeText,它可以工作,但是当我使用我定义的插槽(changeText)时,会出现此错误,我不知道如何解决它
QObject::connect: No such slot buttons::changeText(&button[i][j])
Run Code Online (Sandbox Code Playgroud)
这是函数changeText:
void buttons::changeText(QPushButton* button)
{
button->setText("Fish");
}
Run Code Online (Sandbox Code Playgroud)
注意:在头文件中我定义了这样的插槽:
类按钮:公共 QWidget
Q_OBJECT
public slots:
void changeText(QPushButton* button);
Run Code Online (Sandbox Code Playgroud)
这是示例:
QSignalMapper *map = new QSignalMapper(this);
connect (map, SIGNAL(mapped(QString)), this, SLOT(changeText(QString)));
for(i=0; i<height; i++)
{
for(j=0; j<width; j++)
{
button[i][j] = new QPushButton("Empty", this);
button[i][j]->resize(40, 40);
button[i][j]->move(40*j, 40*i);
connect(button[i][j], SIGNAL(clicked()), map, SLOT(map()));
map->setMapping(button[i][j], QString("Something%1%2").arg(i).arg(j));
}
}
Run Code Online (Sandbox Code Playgroud)
也许你可以删除一张桌子。
小智 5
如果SIGNAL没有提供某些参数,SLOT就无法接收它。信号 clicked() 不提供任何参数。接收它的 SLOT 也不应该有任何。在任何情况下,您都可以让 SLOT 接收比 SIGNAL 提供的参数少的参数(忽略其他一些参数),但除此之外不能。但是,您可以了解信号的发送者,将其转换为 QPushButton* 并对其进行处理:
void buttons::changeText()
{
QPushButton *pb = qobject_cast<QPushButton *>(sender());
if (pb){
pb->setText("fish");
} else {
qDebug() << "Couldn't make the conversion properly";
}
}
Run Code Online (Sandbox Code Playgroud)