Qt 如何将QGridLayout中的CustomWidget与UIForm类连接
我有一个名为“拼写”的UIForm,其中包含QGridLayout和名为“PElement”的自定义小部件。PElement小部件的数量取决于我数据库中的法术数量。因此,我通过Qt 如何将QGridLayout中的CustomWidget与UIForm类连接,qt,signals-slots,qgridlayout,Qt,Signals Slots,Qgridlayout,我有一个名为“拼写”的UIForm,其中包含QGridLayout和名为“PElement”的自定义小部件。PElement小部件的数量取决于我数据库中的法术数量。因此,我通过ui->spellLayout->addWidget(…) 单击PElement时,它会发出信号。我需要将QGridLayout中的每个PElement与Spell类中的插槽连接起来。我不知道怎么做。 谢谢你的帮助 @编辑 这是一个将PictureElement添加到QGridLayout的函数 void Spells::
ui->spellLayout->addWidget(…)
单击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();
}
void法术::设置拼写列表(QString知识)
{
QList elementList=数据库::实例()->getSpellElement(lore);
而(用户界面->拼写布局->计数()>0){
QWidget*w=ui->拼写布局->itemAt(0)->widget();
ui->spellLayout->removeWidget(w);
删除w;
}
int w,h;
w=162;
h=203;
int maxCol=ui->spellScrollArea->width()/(w+ui->spellLayout->spacing());
if(maxColhide();
返回;
}
foreach(QStringList列表、elementList){
PictureElement*spellElement=新的PictureElement;
拼写元素->setText(list.at(0));
spellElement->setPixmap(list.at(1));
拼写元素->设置最小大小(w,h);
ui->spellLayout->addWidget(spellElement,
用户界面->拼写布局->计数()/maxCol,
用户界面->拼写布局->计数()%maxCol);
拼写元素->显示();
}
指示符::实例()->隐藏();
}
我想要的是:
将QGridLayout中的每个PictureElement(单击的信号)与Spells类中的插槽连接。我不太确定问题出在哪里。假设您的类
PictureElement
继承QObject
,包含Q\u对象
宏并发出信号,只需在foreach循环中添加一条连接线:
foreach(QStringList list, elementList){
PictureElement *spellElement = new PictureElement;
...
QObject::connect(spellElement, SIGNAL(clicked()), this, SLOT(slotname()));
}
您已经在
拼写
类中,因此访问不应该是一个问题。当然,slotname()
函数需要定义为标头中的一个插槽。要确定哪个pictureeelement
在插槽中发出信号,可以使用QObject::sender()
method.我不太确定问题出在哪里。假设您的类PictureElement
继承QObject
,包含Q\u对象
宏并发出信号,您只需在foreach循环中添加一条连接线:
foreach(QStringList list, elementList){
PictureElement *spellElement = new PictureElement;
...
QObject::connect(spellElement, SIGNAL(clicked()), this, SLOT(slotname()));
}
您已经在
拼写
类中,因此访问不应该是一个问题。当然,slotname()
函数需要定义为标头中的一个插槽。要确定哪个pictureeelement
在插槽中发出信号,可以使用QObject::sender()
方法。我认为您需要提供更多的代码。我不太确定您在问什么。我更新了我的问题;)我认为您需要提供更多的代码。不太确定你在问什么。我更新了我的问题;)