如何将 QGridLayout 中的 CustomWidget 与 UIForm 连接 class
How to connect CustomWidget in QGridLayout with UIForm class
我有一个名为 "Spell" 的 UIForm,其中特别包含 QGridLayout 和名为 "PElement" 的自定义小部件。 PElement 小部件的数量取决于我数据库中的法术数量。所以,我用 ui->spellLayout->addWidget(...)
填充 QGridLayout
单击 PElement 时它会发出信号。我需要将 QGridLayout 中的每个 PElement 与 Spell class 中的插槽连接起来。我不知道该怎么做。
感谢您的帮助!
@edit
这是一个将 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(单击 SIGNAL)连接到 Spells class.
中的插槽
我不太确定问题出在哪里。假设您的 class PictureElement
继承了 QObject
,包含 Q_OBJECT
宏并发出信号,您只需在 foreach 循环中添加一条连接线:
foreach(QStringList list, elementList){
PictureElement *spellElement = new PictureElement;
...
QObject::connect(spellElement, SIGNAL(clicked()), this, SLOT(slotname()));
}
您已经在 Spells
class 中,因此访问应该不是问题。当然 slotname()
函数需要定义为头部中的槽。要确定哪个 PictureElement
在插槽中发出信号,您可以使用 QObject::sender()
方法。
我有一个名为 "Spell" 的 UIForm,其中特别包含 QGridLayout 和名为 "PElement" 的自定义小部件。 PElement 小部件的数量取决于我数据库中的法术数量。所以,我用 ui->spellLayout->addWidget(...)
单击 PElement 时它会发出信号。我需要将 QGridLayout 中的每个 PElement 与 Spell class 中的插槽连接起来。我不知道该怎么做。 感谢您的帮助!
@edit
这是一个将 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(单击 SIGNAL)连接到 Spells class.
中的插槽我不太确定问题出在哪里。假设您的 class PictureElement
继承了 QObject
,包含 Q_OBJECT
宏并发出信号,您只需在 foreach 循环中添加一条连接线:
foreach(QStringList list, elementList){
PictureElement *spellElement = new PictureElement;
...
QObject::connect(spellElement, SIGNAL(clicked()), this, SLOT(slotname()));
}
您已经在 Spells
class 中,因此访问应该不是问题。当然 slotname()
函数需要定义为头部中的槽。要确定哪个 PictureElement
在插槽中发出信号,您可以使用 QObject::sender()
方法。