首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将QGridLayout中的QGridLayout与UIForm类连接

如何将QGridLayout中的QGridLayout与UIForm类连接
EN

Stack Overflow用户
提问于 2015-09-23 21:10:37
回答 1查看 128关注 0票数 1

我有一个名为“拼写”的UIForm,除其他外,它包含带有名为"PElement“的定制小部件的QGridLayout。PElement小部件的数量取决于我的数据库中的咒语数量。所以,我用QGridLayout填充ui->spellLayout->addWidget(...)

当点击PElement时,它会发出信号。我需要用拼写类中的插槽连接QGridLayout中的每个QGridLayout。我不知道该怎么做。谢谢你帮忙!

@编辑

这是一个将PictureElement添加到QGridLayout的函数。

代码语言:javascript
复制
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(信号点击)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-24 05:58:41

我不太清楚问题在哪里。假设您的类PictureElement继承了QObject,包含了Q_OBJECT宏并发出了信号,那么您只需在foreach循环中添加一条连接线:

代码语言:javascript
复制
foreach(QStringList list, elementList){
    PictureElement *spellElement = new PictureElement;
    ...
    QObject::connect(spellElement, SIGNAL(clicked()), this, SLOT(slotname()));
}

您已经在Spells类中了,所以访问不应该成为一个问题。当然,需要将slotname()函数定义为标头中的一个槽。要识别哪个PictureElement发出插槽内的信号,可以使用QObject::sender()方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32749537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档