QGraphicsLayout: Problems adding QLabel
Hello Everyone,
I've been working on the following problem without any luck:
I have a custom QGraphicsLayout that works in the following manner. Widgets are added to the
layout and arranged much in the same way that text is placed in a standard notepad format, from
left to right until the space for the line runs out, in which case, they are arranged in a new line. The
reason I used a custom implemenetation is because the layout needs the capability to right-align, add
custom space in between widgets, etc. One of the custom things I'm working on is placing a banner that
is spread across widgets that share a common property (they will always be adjacent). In order to implement this "banner", I've created a QLabel. Whenever I am about to add a widget to the layout, I check to see if it is part of a new group. If it is, then I create a new label which I also add to the widget list (via a QGraphicsProxyWidget). Thus, within my layout I currently have two seperate lists:
from my layout.h
Code:
QList<QGraphicsLayoutItem*> m_items;
QMap<QString,QGraphicsLayoutItem*> m_labels;
When I attempt to add items to my list, I do it in the following manner:
from my layout.cpp
Code:
void FlowLayout::addItem(QGraphicsLayoutItem *item)
{
QGraphicsProxyWidget * gpw;
CustomObject * cp;
//We want to make sure that we are entering something to the layout that is ultimately a custom //object
//Hence, the dynamic casts with pointers that are never used for things other than null checks.
gpw = dynamic_cast<QGraphicsProxyWidget *>(item);
if(gpw != NULL)
{
cp = dynamic_cast<CustomObject *>(gpw->widget());
label = dynamic_cast<QLabel *>(gpw->widget());
if(cp != NULL)
{
//If we have a valid object, make this layout the parent and add it to the list!
item->setParentLayoutItem(this);
m_items.append(item);
}
else if(label != NULL)
{
item->setParentLayoutItem(this);
m_labels.insert(label->text(),item);
}
}
else
{
return ;
}
invalidate();
}
bool FlowLayout
::hasLabel(QString labelname
){ return m_labels.contains(labelname);
}
}
And finally, when I call dolayout, everything in my first list is placed juuuust fine. But my problem comes when trying to place my labels:
from dolayout() in my flowlayout.cpp
Code:
//We now need to iterate through the list of items again so that we can place labels appropriately
for(int i = 0; i < m_items.count(); i++)
{
int j;
if(m_order == Normal)
{
j = i;
}
else
{
j = m_items.count()-(i+1);
}
//We take the next item from the list.
curritem = m_items.at(j);
//We know that this item is going to be a custom object pointer, so we can associate a pointer //with it
gpw = dynamic_cast<QGraphicsProxyWidget *>(curritem);
cp = dynamic_cast<CustomObject *>(gpw->widget());
labelitem = m_labels.value(cp->getRelatedSmartPointer()->getLabel());
labelproxy = dynamic_cast<QGraphicsProxyWidget *>(labelitem);
currlabel = dynamic_cast<QLabel *>(labelproxy->widget());
if(currlabel != NULL)
{
currlabel->setWordWrap(true);
currlabel->setAlignment(Qt::AlignBottom | Qt::AlignLeft);
qDebug("[%d]cast for %s with text [%s]", j, qPrintable(cp->getRelatedSmartPointer()->getLabel()), qPrintable(currlabel->text()));
labelitem
->setGeometry
(QRectF((qreal
) 100+50*i,
(qreal
) 100+50*i,curritem
->geometry
().
width(),curritem
->geometry
().
height()));
currlabel
->setMinimumSize
(QSize(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
currlabel
->setMaximumSize
(QSize(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
labelproxy
->setMinimumSize
(QSizeF(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
labelproxy
->setPreferredSize
(QSizeF(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
labelproxy
->setMaximumSize
(QSizeF(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
labelitem
->setMinimumSize
(QSizeF(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
labelitem
->setPreferredSize
(QSizeF(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
labelitem
->setMaximumSize
(QSizeF(curritem
->geometry
().
width(), curritem
->geometry
().
height()));
currlabel->show();
qDebug("item[%d] (%lf,%lf,%lf,%lf)", j, curritem->geometry().x(),curritem->geometry().y(),curritem->geometry().width(),curritem->geometry().height());
qDebug("proxy[%d] (%lf,%lf,%lf,%lf)", j, labelproxy->geometry().x(),labelproxy->geometry().y(),labelproxy->geometry().width(),labelproxy->geometry().height());
}
}
If you're still reading, thanks. One final piece. Here is where how I'm adding my labels to the layout:
from a seperate cpp file containing a QGraphicsScene, QGraphicsView and QGraphicsWidget embedded in the scene as a top level widget set with this QGraphicsLayout
Code:
QLabel *label
= new QLabel(CO
->getRelatedSmartPointer
()->getLabel
());
QGraphicsProxyWidget *labelproxy = new QGraphicsProxyWidget();
labelproxy->setWidget(label);
m_currentLayout->addItem(labelproxy);
The problem I run into, is that the original item is being set in the layout just fine, but that the QLabel will NOT show up no matter what I do. It's not that it isn't being placed properly, it's that it just never appears, and nothing I've tried will make it do so. I ran into this problem earlier when I was originally adding the items that these labels are serving as a banner for, but the problem disappeared when I set the appropriate sizes/size hints for them. I've tried doing the same thing for these labels without any luck. I've run out of ideas, sadly. Any suggestions you guys and gals can come up with would be greatly appreciated.
Cheers,
sp
Re: QGraphicsLayout: Problems adding QLabel
Issue solved!
Why does the answer always come to you shortly after you post?
So, my problem was that I was storing two seperate lists, one with my objects and another with my banners. But I was only keeping track of one of the lists
in the virtual count() removeat() and itemat() functions. So the reason everything in my second list wasn't being shown on the layout is because the parts working
behind the scenes in the QGraphicsLayout setup did no know about them.