我有一个名为“拼写”的UIForm,除其他外,它包含带有名为"PElement“的定制小部件的QGridLayout。PElement小部件的数量取决于我的数据库中的咒语数量。所以,我用QGridLayout填充ui->spellLayout->addWidget(...)
当点击PElement时,它会发出信号。我需要用拼写类中的插槽连接QGridLayout中的每个QGridLayout。我不知道该怎么做。谢谢你帮忙!
@编辑
这是一个将PictureElement添加到QGridLayout的函数。
void Spells::setSpellList(QString lore)
{
QList<QStringList> elementList = Database::instance()->getSpellElement(lore);
while(ui->spellLayout->count() > 0) {
QWidget *w = ui->spellLayout->itemAt(0)->widget();
ui->spellLayout->removeWidget(w);
delete w;
}
int w,h;
w = 162;
h = 203;
int maxCol = ui->spellScrollArea->width() / (w + ui->spellLayout->spacing());
if(maxCol<=0) {
Indicator::instance()->hide();
return;
}
foreach(QStringList list, elementList){
PictureElement *spellElement = new PictureElement;
spellElement->setText(list.at(0));
spellElement->setPixmap(list.at(1));
spellElement->setMinimumSize(w, h);
ui->spellLayout->addWidget(spellElement,
ui->spellLayout->count() / maxCol,
ui->spellLayout->count() % maxCol);
spellElement->show();
}
Indicator::instance()->hide();
}我想要的:用法术类中的插槽连接来自QGridLayout的每个QGridLayout(信号点击)。
发布于 2015-09-24 05:58:41
我不太清楚问题在哪里。假设您的类PictureElement继承了QObject,包含了Q_OBJECT宏并发出了信号,那么您只需在foreach循环中添加一条连接线:
foreach(QStringList list, elementList){
PictureElement *spellElement = new PictureElement;
...
QObject::connect(spellElement, SIGNAL(clicked()), this, SLOT(slotname()));
}您已经在Spells类中了,所以访问不应该成为一个问题。当然,需要将slotname()函数定义为标头中的一个槽。要识别哪个PictureElement发出插槽内的信号,可以使用QObject::sender()方法。
https://stackoverflow.com/questions/32749537
复制相似问题