所以我动态创建 QPushButton 对象,然后将它们映射为发出信号。从与信号关联的槽中,我想编辑对象的属性(在本例中为文本,即 QPushButton::text())。
在头文件中的类“对话框”中,我有:
private:
QSignalMapper *signalMapper;
private slots:
void buttonGeneric(QPushButton &button);
signals:
void clicked(QPushButton &button);
在 .cpp 文件的“对话框”类的构造函数中,我有:
signalMapper = new QSignalMapper(this);
for (int i = 0; i < 100; ++i)
{
QPushButton *button = new QPushButton(QString::number(i + 1));
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, button);
ui->gridLayout->addWidget(button, 2 + (i / 10), (i % 10));
}
connect(signalMapper, SIGNAL(mapped(QPushButton &button)), this, SIGNAL(clicked(QPushButton &button)));
connect(this, SIGNAL(clicked(QPushButton &button)), this, SLOT(buttonGeneric(QPushButton &button)));
最后在同一个 .cpp 文件中的构造函数之外,我有:
void dialog::buttonGeneric(QPushButton & button)
{
button.setText("hello");
}
此代码编译并运行,但是当我单击按钮时没有任何反应。在应用程序调试输出中,我得到以下信息:
QObject::connect: No such signal QSignalMapper::mapped(QPushButton &button)
QObject::connect: (receiver name: 'dialog')
QObject::connect: No such signal dialog::clicked(QPushButton &button)
QObject::connect: (sender name: 'dialog')
QObject::connect: (receiver name: 'dialog')
因此,我再次尝试将单击按钮的文本更改为新内容。我怎样才能做到这一点?
先感谢您。