【问题标题】:How to connect CustomWidget in QGridLayout with UIForm class如何将 QGridLayout 中的 CustomWidget 与 UIForm 类连接起来
【发布时间】:2015-09-23 21:10:37
【问题描述】:

我有一个名为“Spell”的 UIForm,其中包含 QGridLayout 以及名为“PElement”的自定义小部件。 PElement 小部件的数量取决于我的数据库中的法术数量。所以,我用ui->spellLayout->addWidget(...)填充QGridLayout

当 PElement 被点击时,它会发出信号。我需要将 QGridLayout 中的每个 PElement 与 Spell 类中的插槽连接起来。我不知道该怎么做。 感谢您的帮助!

@编辑

这是一个将 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 中的每个 PictureElement(单击的信号)与 Spells 类中的插槽连接起来。

【问题讨论】:

  • 我认为您需要提供更多代码。不太清楚你在问什么。
  • 我更新了我的问题; )

标签: qt signals-slots qgridlayout


【解决方案1】:

我不太确定问题出在哪里。假设您的类 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() 方法。

【讨论】:

    猜你喜欢
    • 2016-10-02
    • 2018-04-06
    • 2021-02-23
    • 2020-05-12
    • 2019-07-16
    • 2010-11-17
    • 2021-09-15
    • 2020-08-18
    相关资源
    最近更新 更多