Results 1 to 4 of 4

Thread: QWidget layout problems

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QWidget layout problems

    Hi to all!

    I have a problem that I cannot crack. I have subclassed QWidget with some QLabels and some QPushButtons and several layouts with qwidget main layout, which is set by setLayout method at the end of subclassed QWidget constructor. And I have no idea, why the GUI elements inside subclassed QWidget are not layouted. Here is header file:
    Qt Code:
    1. #ifndef MERCHANDISETABLEDELEGATEWIDGET_H
    2. #define MERCHANDISETABLEDELEGATEWIDGET_H
    3.  
    4. // system includes
    5. #include <QtGui/QWidget>
    6. #include <QtGui/QHBoxLayout>
    7. #include <QtGui/QVBoxLayout>
    8. #include <QtGui/QLabel>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QIcon>
    11. #include <QtGui/QPixmap>
    12. #include <QtCore/QSize>
    13.  
    14. // custom includes
    15.  
    16. // constants
    17. static const quint64 ICON_WIDTH=48;
    18. static const quint64 ICON_HEIGHT=48;
    19. static const quint64 DELTA=24;
    20.  
    21. static const quint64 DELAY_AUTO_REPEAT=300;
    22. static const quint64 DELAY_AUTO_REPEAT_INTERVAL=100;
    23.  
    24. class MerchandiseTableDelegateWidget : public QWidget
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. explicit MerchandiseTableDelegateWidget(QWidget* parent=0);
    30.  
    31. /*
    32.   inline QSize sizeHint() const
    33.   { return QSize(348, 348); }
    34. */
    35.  
    36. inline QVBoxLayout* textLayout() const
    37. { return m_pTextLayout; }
    38. inline QHBoxLayout* priceLayout() const
    39. { return m_pPriceLayout; }
    40. inline QVBoxLayout* operationsLayout() const
    41. { return m_pOperationsLayout; }
    42. inline QHBoxLayout* mainLayout() const
    43. { return m_pMainLayout; }
    44.  
    45. inline QPixmap* merchandisePic() const
    46. { return m_pMerchandisePic; }
    47.  
    48. inline QLabel* merchandiseName() const
    49. { return m_pMerchandiseName; }
    50. inline QLabel* merchandiseDescription() const
    51. { return m_pMerchandiseDescription; }
    52. inline QLabel* merchandisePrice() const
    53. { return m_pMerchandisePrice; }
    54. inline QLabel* merchandiseQuantity() const
    55. { return m_pMerchandiseQuantity; }
    56. inline QLabel* merchandiseSubtotal() const
    57. { return m_pMerchandiseSubtotal; }
    58.  
    59. inline QPushButton* buttonIncreaseQuantity() const
    60. { return m_pButtonIncreaseQuantity; }
    61. inline QPushButton* buttonDecreaseQuantity() const
    62. { return m_pButtonDecreaseQuantity; }
    63. inline QPushButton* buttonTrash() const
    64. { return m_pButtonTrash; }
    65.  
    66. private:
    67. QVBoxLayout* m_pTextLayout;
    68. QHBoxLayout* m_pPriceLayout;
    69. QVBoxLayout* m_pOperationsLayout;
    70. QHBoxLayout* m_pMainLayout;
    71.  
    72. QPixmap* m_pMerchandisePic;
    73.  
    74. QLabel* m_pMerchandiseName;
    75. QLabel* m_pMerchandiseDescription;
    76. QLabel* m_pMerchandisePrice;
    77. QLabel* m_pMerchandiseQuantity;
    78. QLabel* m_pMerchandiseSubtotal;
    79.  
    80. QPushButton* m_pButtonIncreaseQuantity;
    81. QPushButton* m_pButtonDecreaseQuantity;
    82. QPushButton* m_pButtonTrash;
    83.  
    84. public slots:
    85. void quantityIncrease();
    86. void quantityDecrease();
    87. void quantityZero();
    88. };
    89.  
    90. #endif // MERCHANDISETABLEDELEGATEWIDGET_H
    To copy to clipboard, switch view to plain text mode 
    And here is .cpp file:
    Qt Code:
    1. #include "merchandisetabledelegatewidget.h"
    2.  
    3. MerchandiseTableDelegateWidget::MerchandiseTableDelegateWidget(QWidget* parent)
    4. : QWidget(parent)
    5. {
    6. m_pTextLayout=new QVBoxLayout();
    7. Q_ASSERT_X(textLayout()!=0,
    8. "Text Layout object allocation",
    9. "Text Layout object could not be allocated.");
    10.  
    11. m_pMerchandiseName=new QLabel(this);
    12. Q_ASSERT_X(merchandiseName()!=0,
    13. "Merchandise name label object allocation",
    14. "Merchandise name label object could not be allocated.");
    15.  
    16. m_pMerchandiseDescription=new QLabel(this);
    17. Q_ASSERT_X(merchandiseDescription()!=0,
    18. "Merchandise description label object allocation",
    19. "Merchandise description label object could not be allocated.");
    20.  
    21. textLayout()->addWidget(merchandiseName());
    22. textLayout()->addWidget(merchandiseDescription());
    23.  
    24. m_pPriceLayout=new QHBoxLayout();
    25. Q_ASSERT_X(priceLayout()!=0,
    26. "Price layout object allocation",
    27. "Price layout object could not be allocated.");
    28.  
    29. m_pMerchandisePrice=new QLabel(this);
    30. Q_ASSERT_X(merchandisePrice()!=0,
    31. "Merchandise price label object allocation",
    32. "Merchandise price label object could not be allocated.");
    33.  
    34. m_pMerchandiseQuantity=new QLabel(this);
    35. Q_ASSERT_X(merchandiseQuantity()!=0,
    36. "Merchandise quantity label object allocation",
    37. "Merchandise quantity label object could not be allocated.");
    38.  
    39. m_pMerchandiseSubtotal=new QLabel(this);
    40. Q_ASSERT_X(merchandiseSubtotal()!=0,
    41. "Merchandise subtotal label object allocation",
    42. "Merchandise subtotal label object could not be allocated.");
    43.  
    44. priceLayout()->addWidget(merchandisePrice());
    45. priceLayout()->addWidget(merchandiseQuantity());
    46. priceLayout()->addWidget(merchandiseSubtotal());
    47.  
    48. textLayout()->addLayout(priceLayout());
    49.  
    50. m_pOperationsLayout=new QVBoxLayout();
    51. Q_ASSERT_X(operationsLayout()!=0,
    52. "Operations layout object allocation",
    53. "Operations layout could not be allocated.");
    54.  
    55. m_pButtonIncreaseQuantity=new QPushButton(QIcon(":/delegateIcons/quaInc"),
    56. tr(""),
    57. this);
    58. Q_ASSERT_X(buttonIncreaseQuantity()!=0,
    59. "Increase quantity pushbutton object allocation",
    60. "Increase quantity pushbutton object could not be allocated.");
    61.  
    62. buttonIncreaseQuantity()->setSizePolicy(QSizePolicy::Fixed,
    63. QSizePolicy::Fixed);
    64. buttonIncreaseQuantity()->setIconSize(QSize(ICON_WIDTH,
    65. ICON_HEIGHT));
    66. buttonIncreaseQuantity()->setAutoRepeat(true);
    67. connect(buttonIncreaseQuantity(),
    68. SIGNAL(clicked()),
    69. this,
    70. SLOT(quantityIncrease()));
    71.  
    72. m_pButtonDecreaseQuantity=new QPushButton(QIcon(":/delegateIcons/quaDec"),
    73. tr(""),
    74. this);
    75. Q_ASSERT_X(buttonDecreaseQuantity()!=0,
    76. "Decrease quantity pushbutton object allocation",
    77. "Decrease quantity pushbutton object could not be allocated.");
    78.  
    79. buttonDecreaseQuantity()->setSizePolicy(QSizePolicy::Fixed,
    80. QSizePolicy::Fixed);
    81. buttonDecreaseQuantity()->setIconSize(QSize(ICON_WIDTH,
    82. ICON_HEIGHT));
    83. buttonDecreaseQuantity()->setAutoRepeat(true);
    84. connect(buttonDecreaseQuantity(),
    85. SIGNAL(clicked()),
    86. this,
    87. SLOT(quantityDecrease()));
    88.  
    89. m_pButtonTrash=new QPushButton(QIcon(":/delegateIcons/quaZereo"),
    90. tr(""),
    91. this);
    92. Q_ASSERT_X(buttonTrash()!=0,
    93. "Trash pushbutton object allocation",
    94. "Trash pushbutton object could not be allocated.");
    95.  
    96. buttonTrash()->setSizePolicy(QSizePolicy::Fixed,
    97. QSizePolicy::Fixed);
    98. buttonTrash()->setIconSize(QSize(ICON_WIDTH,
    99. ICON_HEIGHT));
    100. buttonTrash()->setAutoRepeat(false);
    101. connect(buttonTrash(),
    102. SIGNAL(clicked()),
    103. this,
    104. SLOT(quantityZero()));
    105.  
    106. m_pMainLayout=new QHBoxLayout();
    107. Q_ASSERT_X(mainLayout()!=0,
    108. "Main layout object allocation",
    109. "Main layout object could not be allocated");
    110.  
    111. m_pMerchandisePic=new QPixmap(ICON_WIDTH+DELTA,
    112. ICON_HEIGHT+DELTA);
    113. Q_ASSERT_X(merchandisePic()!=0,
    114. "Merchandise pic object allocation",
    115. "Merchandise pic object could not be allocated.");
    116.  
    117. // mainLayout()->addWidget(merchandisePic());
    118. mainLayout()->addLayout(textLayout());
    119. mainLayout()->addLayout(operationsLayout());
    120.  
    121. setLayout(mainLayout());
    122. }
    123.  
    124. void MerchandiseTableDelegateWidget::quantityIncrease()
    125. {
    126. }
    127.  
    128. void MerchandiseTableDelegateWidget::quantityDecrease()
    129. {
    130. }
    131.  
    132. void MerchandiseTableDelegateWidget::quantityZero()
    133. {
    134. }
    To copy to clipboard, switch view to plain text mode 
    Can someone please help me, why layoutng is not working, because I have no idea and I am losing mind.

    Thank all of you in advance,
    Marko
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget layout problems

    Try the following, just before
    Qt Code:
    1. setLayout(mainLayout());
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QLayout *pOldLayout = this->layout();
    2. if(pOldLayout) delete pOldLayout;
    3. setLayout(mainLayout());
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QWidget layout problems

    No effect, I also placed breakpoint on if clause, delete statement is skipped.
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget layout problems

    You haven't added the buttons to any layout. The labels lay out fine on my machine.

Similar Threads

  1. layout QLabel and QWidget
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2010, 12:53
  2. Problems with QMainWindow and layout
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2009, 23:49
  3. Problems resizing QWidget & Phonon
    By gmclachl in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2009, 17:32
  4. Replies: 4
    Last Post: 8th August 2008, 18:28
  5. Resizing problems when applying a layout
    By JimBrown in forum Newbie
    Replies: 1
    Last Post: 21st February 2007, 23:54

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.