Results 1 to 6 of 6

Thread: Conceptual Question; QLabel as Banner?

  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 Conceptual Question; QLabel as Banner?

    This is more of a conceptual question, so I don't have code to post yet. I am a little bit stuck on thinking of the right way to implement this idea, and I was wondering if anyone could offer an idea.

    The Problem: I currently have a custom implementation of a linear layout, where widgets are being placed
    from left to right horizontally. When row space runs out, they are moved down to the next row, like so:

    1 2 3 4
    5 6 7 8
    9

    The widgets that I am placing each belong to groups that can be arbitrarily long. I am currently trying to design a banner that would be place on top of these object, so if widgets 1,2,3 are in group A, and 4,5.6 are in group B there would be a banner over them like:

    [ A ] [
    1 2 3 4
    B ]
    5 6

    I thought that QLabel would be the easiest way to do this, but if I run into the case where a group is split into the next row like above, I'm not sure how I can gracefully split the text inside of the label between rows :/. I was wondering if there was an easier way that I'm not thinking of or if I should just try to make this "split a QLabel's text into two different QLabels" idea work.

    Cheers,
    sp

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Conceptual Question; QLabel as Banner?

    It's not really clear to me what you mean with a banner?
    Do you want some column descriptions?

    example:

    Qt Code:
    1. [ItemBanner1Group1] [ItemBanner2Group1] [ItemBanner3Group1]
    2. item 1 item 2 item 3
    3. [ItemBanner3Group1]
    4. item 4
    5. [ItemBanner1Group2] [ItemBanner2Group2]
    6. item 1 item 2
    To copy to clipboard, switch view to plain text mode 

    where everything between [ ] is a banner, and the item is underneath the banner

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

    Default Re: Conceptual Question; QLabel as Banner?

    Thanks for the interest. I hope this does a better job of explaining what I'm going for:

    The banner acts as a label that is spread across a set of grouped items. So if items 1-3 are in group one, and items 4-6 are in group two, each
    group has its own "banner" with text that would say something like "group 1". I was thinking of making a QLabel that would have the same width
    as the sum of the 3 items and placing the label on top of all the grouped items, but I wasn't sure if this would be a good idea, since if a group is
    split the label must carry on to a new row.

    [Text describing group one] [Text de
    [Item 1] [item 2] [item 3] ] [Item 4]

    scribing group two]
    [item 5] [item 6]

  4. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Conceptual Question; QLabel as Banner?

    If I understand correctly what You need is QGroupBox.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Conceptual Question; QLabel as Banner?

    I gave it a try, check out the code below.
    It's absolutely not perfect. In fact, I think the most correct way to do this is to create a QLayout subclass.

    Screenshot attached below

    Edit:
    It works by keeping a list of groups and items.
    First clear the layout from any widget or layout item.
    Then go through each group.
    In each group, add all the items to the layout, keeping below the maximum amount of columns
    Then, when every item of a group is added add labels, keeping an eye on the span of the items for this group in the rows.

    It might not be exactly like you want, but I think you can take it from here :-)

    Header
    Qt Code:
    1. #ifndef SPECIALLAYOUT_H
    2. #define SPECIALLAYOUT_H
    3.  
    4. #include <QGridLayout>
    5. #include <QStringList>
    6. #include <QString>
    7. #include <QMultiMap>
    8. #include <QWidget>
    9.  
    10. class SpecialLayout : public QGridLayout
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit SpecialLayout(QWidget *parent = 0);
    15.  
    16. void addGroup(const QString &groupName);
    17. void addItemWidget(const QString &inGroup);
    18.  
    19. void setMaximumColumns(int columns);
    20.  
    21. QStringList groupNames() const;
    22.  
    23. signals:
    24.  
    25. public slots:
    26.  
    27. protected:
    28. void rebuildLayout();
    29.  
    30. private:
    31. QStringList m_groupNames;
    32. QMultiMap<QString, QString> m_groupWidgetMap;
    33. int m_maximumColumnCount;
    34. };
    35.  
    36. #endif // SPECIALLAYOUT_H
    To copy to clipboard, switch view to plain text mode 

    Implementation
    Qt Code:
    1. #include "speciallayout.h"
    2.  
    3. #include <QLabel>
    4. #include <QSpacerItem>
    5. #include <QSizePolicy>
    6.  
    7. SpecialLayout::SpecialLayout(QWidget *parent) :
    8. QGridLayout(parent)
    9. {
    10. m_maximumColumnCount = 10;
    11. }
    12.  
    13. void SpecialLayout::addGroup(const QString &groupName)
    14. {
    15. if (m_groupNames.contains(groupName))
    16. return;
    17.  
    18. m_groupNames.append(groupName);
    19. }
    20.  
    21. void SpecialLayout::addItemWidget(const QString &inGroup)
    22. {
    23. if (!m_groupNames.contains(inGroup))
    24. return;
    25.  
    26. m_groupWidgetMap.insert(inGroup, "Item");
    27.  
    28. rebuildLayout();
    29. }
    30.  
    31. QStringList SpecialLayout::groupNames() const
    32. {
    33. return m_groupNames;
    34. }
    35.  
    36. void SpecialLayout::setMaximumColumns(int columns)
    37. {
    38. m_maximumColumnCount = columns;
    39. }
    40.  
    41. void SpecialLayout::rebuildLayout()
    42. {
    43. QLayoutItem *item;
    44. while ((item = takeAt(0))) {
    45. if (item->widget())
    46. delete item->widget();
    47.  
    48. delete item;
    49. }
    50.  
    51. int currentColumn = 0;
    52. int currentRow = 0;
    53. int widgetColumn = 0;
    54. int widgetRow = 0;
    55. int columnCount = 0;
    56.  
    57. foreach(QString group, m_groupNames) {
    58.  
    59. if (!m_groupWidgetMap.values(group).isEmpty()) {
    60.  
    61. widgetColumn = currentColumn;
    62. widgetRow = currentRow;
    63.  
    64. foreach(QString string, m_groupWidgetMap.values(group)) {
    65. QLabel *itemWidget = new QLabel("<b>" + string + "</b>");
    66. addWidget(itemWidget, widgetRow + 1, widgetColumn);
    67.  
    68. ++widgetColumn;
    69.  
    70. if (widgetColumn > m_maximumColumnCount - 1) {
    71. widgetColumn = 0;
    72. widgetRow += 2;
    73. }
    74. }
    75.  
    76. columnCount = m_groupWidgetMap.values(group).count();
    77.  
    78. int columnSpan;
    79.  
    80. while (columnCount > 0) {
    81. columnSpan = qMin(m_maximumColumnCount - currentColumn, columnCount);
    82.  
    83. QLabel *label = new QLabel(group);
    84.  
    85. addWidget(label, currentRow, currentColumn, 1, columnSpan);
    86.  
    87. columnCount -= columnSpan;
    88. currentColumn += columnSpan;
    89. if (currentColumn > m_maximumColumnCount - 1) {
    90. currentColumn = 0;
    91. currentRow += 2;
    92. }
    93. }
    94. }
    95. }
    96. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  6. The following user says thank you to tbscope for this useful post:

    SneakyPeterson (28th June 2010)

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

    Default Re: Conceptual Question; QLabel as Banner?

    Quote Originally Posted by tbscope View Post
    I gave it a try, check out the code below.
    It's absolutely not perfect. In fact, I think the most correct way to do this is to create a QLayout subclass.

    Screenshot attached below

    Edit:
    It works by keeping a list of groups and items.
    First clear the layout from any widget or layout item.
    Then go through each group.
    In each group, add all the items to the layout, keeping below the maximum amount of columns
    Then, when every item of a group is added add labels, keeping an eye on the span of the items for this group in the rows.

    It might not be exactly like you want, but I think you can take it from here :-)

    Header
    Qt Code:
    1. #ifndef SPECIALLAYOUT_H
    2. #define SPECIALLAYOUT_H
    3.  
    4. #include <QGridLayout>
    5. #include <QStringList>
    6. #include <QString>
    7. #include <QMultiMap>
    8. #include <QWidget>
    9.  
    10. class SpecialLayout : public QGridLayout
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit SpecialLayout(QWidget *parent = 0);
    15.  
    16. void addGroup(const QString &groupName);
    17. void addItemWidget(const QString &inGroup);
    18.  
    19. void setMaximumColumns(int columns);
    20.  
    21. QStringList groupNames() const;
    22.  
    23. signals:
    24.  
    25. public slots:
    26.  
    27. protected:
    28. void rebuildLayout();
    29.  
    30. private:
    31. QStringList m_groupNames;
    32. QMultiMap<QString, QString> m_groupWidgetMap;
    33. int m_maximumColumnCount;
    34. };
    35.  
    36. #endif // SPECIALLAYOUT_H
    To copy to clipboard, switch view to plain text mode 

    Implementation
    Qt Code:
    1. #include "speciallayout.h"
    2.  
    3. #include <QLabel>
    4. #include <QSpacerItem>
    5. #include <QSizePolicy>
    6.  
    7. SpecialLayout::SpecialLayout(QWidget *parent) :
    8. QGridLayout(parent)
    9. {
    10. m_maximumColumnCount = 10;
    11. }
    12.  
    13. void SpecialLayout::addGroup(const QString &groupName)
    14. {
    15. if (m_groupNames.contains(groupName))
    16. return;
    17.  
    18. m_groupNames.append(groupName);
    19. }
    20.  
    21. void SpecialLayout::addItemWidget(const QString &inGroup)
    22. {
    23. if (!m_groupNames.contains(inGroup))
    24. return;
    25.  
    26. m_groupWidgetMap.insert(inGroup, "Item");
    27.  
    28. rebuildLayout();
    29. }
    30.  
    31. QStringList SpecialLayout::groupNames() const
    32. {
    33. return m_groupNames;
    34. }
    35.  
    36. void SpecialLayout::setMaximumColumns(int columns)
    37. {
    38. m_maximumColumnCount = columns;
    39. }
    40.  
    41. void SpecialLayout::rebuildLayout()
    42. {
    43. QLayoutItem *item;
    44. while ((item = takeAt(0))) {
    45. if (item->widget())
    46. delete item->widget();
    47.  
    48. delete item;
    49. }
    50.  
    51. int currentColumn = 0;
    52. int currentRow = 0;
    53. int widgetColumn = 0;
    54. int widgetRow = 0;
    55. int columnCount = 0;
    56.  
    57. foreach(QString group, m_groupNames) {
    58.  
    59. if (!m_groupWidgetMap.values(group).isEmpty()) {
    60.  
    61. widgetColumn = currentColumn;
    62. widgetRow = currentRow;
    63.  
    64. foreach(QString string, m_groupWidgetMap.values(group)) {
    65. QLabel *itemWidget = new QLabel("<b>" + string + "</b>");
    66. addWidget(itemWidget, widgetRow + 1, widgetColumn);
    67.  
    68. ++widgetColumn;
    69.  
    70. if (widgetColumn > m_maximumColumnCount - 1) {
    71. widgetColumn = 0;
    72. widgetRow += 2;
    73. }
    74. }
    75.  
    76. columnCount = m_groupWidgetMap.values(group).count();
    77.  
    78. int columnSpan;
    79.  
    80. while (columnCount > 0) {
    81. columnSpan = qMin(m_maximumColumnCount - currentColumn, columnCount);
    82.  
    83. QLabel *label = new QLabel(group);
    84.  
    85. addWidget(label, currentRow, currentColumn, 1, columnSpan);
    86.  
    87. columnCount -= columnSpan;
    88. currentColumn += columnSpan;
    89. if (currentColumn > m_maximumColumnCount - 1) {
    90. currentColumn = 0;
    91. currentRow += 2;
    92. }
    93. }
    94. }
    95. }
    96. }
    To copy to clipboard, switch view to plain text mode 
    This is very close to what I was going for. Thank you very much for the help!

    Cheers,
    sp

Similar Threads

  1. Conceptual MVC problem in Qt
    By Daniel Dekkers in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2010, 17:26
  2. QLabel text change signal question
    By MarkoSan in forum Newbie
    Replies: 10
    Last Post: 5th April 2008, 11:19
  3. Newbie question about special characters in QLabel
    By WinchellChung in forum Newbie
    Replies: 4
    Last Post: 3rd April 2008, 22:39
  4. QWizard and banner pixmap
    By desch in forum Qt Programming
    Replies: 8
    Last Post: 7th January 2008, 11:28
  5. QHeaderView as a banner
    By mclark in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2007, 20:28

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.