1
votes

I have a UIForm named "Spell" that contains inter alia QGridLayout with custom widgets named "PElement". The amount of PElement widgets depends on number of spells in my database. So, I fill QGridLayout by ui->spellLayout->addWidget(...)

When PElement is clicked it emits signal. I need to connect each of PElement in QGridLayout with slot in Spell class. I have no idea how to do it. Thanks for help!

@edit

this is a function that add PictureElement to 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();
}

What I want: connect every PictureElement (SIGNAL clicked) from QGridLayout with slot in Spells class.

1
I think you need to provide a bit more code. Not really sure what you are asking.strubbly
I update my question ; )Mietek Mietkiewicz

1 Answers

0
votes

I am not quite sure where the issue ist. Assuming your class PictureElement inherits QObject, contains the Q_OBJECT macro and emits the signal, your simply add a connect line in your foreach loop:

foreach(QStringList list, elementList){
    PictureElement *spellElement = new PictureElement;
    ...
    QObject::connect(spellElement, SIGNAL(clicked()), this, SLOT(slotname()));
}

You are already in the Spells class so theaccess shouldn't be an issue. Of course the slotname() function needs to be defined as a slot in the header. To identify which PictureElement emitted the signal within the slot you can use the QObject::sender() method.