Results 1 to 2 of 2

Thread: QGraphicsLayout: Problems adding QLabel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. QList<QGraphicsLayoutItem*> m_items;
    2. QMap<QString,QGraphicsLayoutItem*> m_labels;
    To copy to clipboard, switch view to plain text mode 

    When I attempt to add items to my list, I do it in the following manner:

    from my layout.cpp
    Qt Code:
    1. void FlowLayout::addItem(QGraphicsLayoutItem *item)
    2. {
    3. QGraphicsProxyWidget * gpw;
    4. CustomObject * cp;
    5. QLabel *label;
    6. //We want to make sure that we are entering something to the layout that is ultimately a custom //object
    7. //Hence, the dynamic casts with pointers that are never used for things other than null checks.
    8. gpw = dynamic_cast<QGraphicsProxyWidget *>(item);
    9. if(gpw != NULL)
    10. {
    11. cp = dynamic_cast<CustomObject *>(gpw->widget());
    12. label = dynamic_cast<QLabel *>(gpw->widget());
    13. if(cp != NULL)
    14. {
    15. //If we have a valid object, make this layout the parent and add it to the list!
    16. item->setParentLayoutItem(this);
    17. m_items.append(item);
    18. }
    19. else if(label != NULL)
    20. {
    21. item->setParentLayoutItem(this);
    22. m_labels.insert(label->text(),item);
    23. }
    24. }
    25. else
    26. {
    27. return ;
    28. }
    29. invalidate();
    30. }
    31.  
    32. bool FlowLayout::hasLabel(QString labelname){
    33. return m_labels.contains(labelname);
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. //We now need to iterate through the list of items again so that we can place labels appropriately
    2. for(int i = 0; i < m_items.count(); i++)
    3. {
    4. int j;
    5. if(m_order == Normal)
    6. {
    7. j = i;
    8. }
    9. else
    10. {
    11. j = m_items.count()-(i+1);
    12. }
    13. //We take the next item from the list.
    14. curritem = m_items.at(j);
    15. //We know that this item is going to be a custom object pointer, so we can associate a pointer //with it
    16. gpw = dynamic_cast<QGraphicsProxyWidget *>(curritem);
    17. cp = dynamic_cast<CustomObject *>(gpw->widget());
    18. labelitem = m_labels.value(cp->getRelatedSmartPointer()->getLabel());
    19. labelproxy = dynamic_cast<QGraphicsProxyWidget *>(labelitem);
    20. currlabel = dynamic_cast<QLabel *>(labelproxy->widget());
    21. if(currlabel != NULL)
    22. {
    23. currlabel->setWordWrap(true);
    24. currlabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    25. currlabel->setAlignment(Qt::AlignBottom | Qt::AlignLeft);
    26.  
    27. qDebug("[%d]cast for %s with text [%s]", j, qPrintable(cp->getRelatedSmartPointer()->getLabel()), qPrintable(currlabel->text()));
    28. labelitem->setGeometry(QRectF((qreal) 100+50*i,(qreal) 100+50*i,curritem->geometry().width(),curritem->geometry().height()));
    29.  
    30. currlabel->setMinimumSize(QSize(curritem->geometry().width(), curritem->geometry().height()));
    31. currlabel->setMaximumSize(QSize(curritem->geometry().width(), curritem->geometry().height()));
    32.  
    33. labelproxy->setMinimumSize(QSizeF(curritem->geometry().width(), curritem->geometry().height()));
    34. labelproxy->setPreferredSize(QSizeF(curritem->geometry().width(), curritem->geometry().height()));
    35. labelproxy->setMaximumSize(QSizeF(curritem->geometry().width(), curritem->geometry().height()));
    36. labelproxy->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    37.  
    38. labelitem->setMinimumSize(QSizeF(curritem->geometry().width(), curritem->geometry().height()));
    39. labelitem->setPreferredSize(QSizeF(curritem->geometry().width(), curritem->geometry().height()));
    40. labelitem->setMaximumSize(QSizeF(curritem->geometry().width(), curritem->geometry().height()));
    41. labelitem->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    42.  
    43. currlabel->show();
    44.  
    45. qDebug("item[%d] (%lf,%lf,%lf,%lf)", j, curritem->geometry().x(),curritem->geometry().y(),curritem->geometry().width(),curritem->geometry().height());
    46. qDebug("proxy[%d] (%lf,%lf,%lf,%lf)", j, labelproxy->geometry().x(),labelproxy->geometry().y(),labelproxy->geometry().width(),labelproxy->geometry().height());
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. QLabel *label = new QLabel(CO->getRelatedSmartPointer()->getLabel());
    2. QGraphicsProxyWidget *labelproxy = new QGraphicsProxyWidget();
    3. labelproxy->setWidget(label);
    4. m_currentLayout->addItem(labelproxy);
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. Replies: 10
    Last Post: 16th July 2011, 12:29
  2. QGraphicsLayout::geometry()
    By isutruk in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2009, 15:43
  3. Problems compiling after adding an action in a menu
    By grub87 in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 17:58
  4. Extending QGraphicsLayout
    By Darkman in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2009, 12:09
  5. Replies: 2
    Last Post: 7th January 2007, 21:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.