PDA

View Full Version : QWidget layout problems



MarkoSan
12th January 2010, 07:36
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:
#ifndef MERCHANDISETABLEDELEGATEWIDGET_H
#define MERCHANDISETABLEDELEGATEWIDGET_H

// system includes
#include <QtGui/QWidget>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QIcon>
#include <QtGui/QPixmap>
#include <QtCore/QSize>

// custom includes

// constants
static const quint64 ICON_WIDTH=48;
static const quint64 ICON_HEIGHT=48;
static const quint64 DELTA=24;

static const quint64 DELAY_AUTO_REPEAT=300;
static const quint64 DELAY_AUTO_REPEAT_INTERVAL=100;

class MerchandiseTableDelegateWidget : public QWidget
{
Q_OBJECT

public:
explicit MerchandiseTableDelegateWidget(QWidget* parent=0);

/*
inline QSize sizeHint() const
{ return QSize(348, 348); }
*/

inline QVBoxLayout* textLayout() const
{ return m_pTextLayout; }
inline QHBoxLayout* priceLayout() const
{ return m_pPriceLayout; }
inline QVBoxLayout* operationsLayout() const
{ return m_pOperationsLayout; }
inline QHBoxLayout* mainLayout() const
{ return m_pMainLayout; }

inline QPixmap* merchandisePic() const
{ return m_pMerchandisePic; }

inline QLabel* merchandiseName() const
{ return m_pMerchandiseName; }
inline QLabel* merchandiseDescription() const
{ return m_pMerchandiseDescription; }
inline QLabel* merchandisePrice() const
{ return m_pMerchandisePrice; }
inline QLabel* merchandiseQuantity() const
{ return m_pMerchandiseQuantity; }
inline QLabel* merchandiseSubtotal() const
{ return m_pMerchandiseSubtotal; }

inline QPushButton* buttonIncreaseQuantity() const
{ return m_pButtonIncreaseQuantity; }
inline QPushButton* buttonDecreaseQuantity() const
{ return m_pButtonDecreaseQuantity; }
inline QPushButton* buttonTrash() const
{ return m_pButtonTrash; }

private:
QVBoxLayout* m_pTextLayout;
QHBoxLayout* m_pPriceLayout;
QVBoxLayout* m_pOperationsLayout;
QHBoxLayout* m_pMainLayout;

QPixmap* m_pMerchandisePic;

QLabel* m_pMerchandiseName;
QLabel* m_pMerchandiseDescription;
QLabel* m_pMerchandisePrice;
QLabel* m_pMerchandiseQuantity;
QLabel* m_pMerchandiseSubtotal;

QPushButton* m_pButtonIncreaseQuantity;
QPushButton* m_pButtonDecreaseQuantity;
QPushButton* m_pButtonTrash;

public slots:
void quantityIncrease();
void quantityDecrease();
void quantityZero();
};

#endif // MERCHANDISETABLEDELEGATEWIDGET_HAnd here is .cpp file:
#include "merchandisetabledelegatewidget.h"

MerchandiseTableDelegateWidget::MerchandiseTableDe legateWidget(QWidget* parent)
: QWidget(parent)
{
m_pTextLayout=new QVBoxLayout();
Q_ASSERT_X(textLayout()!=0,
"Text Layout object allocation",
"Text Layout object could not be allocated.");

m_pMerchandiseName=new QLabel(this);
Q_ASSERT_X(merchandiseName()!=0,
"Merchandise name label object allocation",
"Merchandise name label object could not be allocated.");

m_pMerchandiseDescription=new QLabel(this);
Q_ASSERT_X(merchandiseDescription()!=0,
"Merchandise description label object allocation",
"Merchandise description label object could not be allocated.");

textLayout()->addWidget(merchandiseName());
textLayout()->addWidget(merchandiseDescription());

m_pPriceLayout=new QHBoxLayout();
Q_ASSERT_X(priceLayout()!=0,
"Price layout object allocation",
"Price layout object could not be allocated.");

m_pMerchandisePrice=new QLabel(this);
Q_ASSERT_X(merchandisePrice()!=0,
"Merchandise price label object allocation",
"Merchandise price label object could not be allocated.");

m_pMerchandiseQuantity=new QLabel(this);
Q_ASSERT_X(merchandiseQuantity()!=0,
"Merchandise quantity label object allocation",
"Merchandise quantity label object could not be allocated.");

m_pMerchandiseSubtotal=new QLabel(this);
Q_ASSERT_X(merchandiseSubtotal()!=0,
"Merchandise subtotal label object allocation",
"Merchandise subtotal label object could not be allocated.");

priceLayout()->addWidget(merchandisePrice());
priceLayout()->addWidget(merchandiseQuantity());
priceLayout()->addWidget(merchandiseSubtotal());

textLayout()->addLayout(priceLayout());

m_pOperationsLayout=new QVBoxLayout();
Q_ASSERT_X(operationsLayout()!=0,
"Operations layout object allocation",
"Operations layout could not be allocated.");

m_pButtonIncreaseQuantity=new QPushButton(QIcon(":/delegateIcons/quaInc"),
tr(""),
this);
Q_ASSERT_X(buttonIncreaseQuantity()!=0,
"Increase quantity pushbutton object allocation",
"Increase quantity pushbutton object could not be allocated.");

buttonIncreaseQuantity()->setSizePolicy(QSizePolicy::Fixed,
QSizePolicy::Fixed);
buttonIncreaseQuantity()->setIconSize(QSize(ICON_WIDTH,
ICON_HEIGHT));
buttonIncreaseQuantity()->setAutoRepeat(true);
connect(buttonIncreaseQuantity(),
SIGNAL(clicked()),
this,
SLOT(quantityIncrease()));

m_pButtonDecreaseQuantity=new QPushButton(QIcon(":/delegateIcons/quaDec"),
tr(""),
this);
Q_ASSERT_X(buttonDecreaseQuantity()!=0,
"Decrease quantity pushbutton object allocation",
"Decrease quantity pushbutton object could not be allocated.");

buttonDecreaseQuantity()->setSizePolicy(QSizePolicy::Fixed,
QSizePolicy::Fixed);
buttonDecreaseQuantity()->setIconSize(QSize(ICON_WIDTH,
ICON_HEIGHT));
buttonDecreaseQuantity()->setAutoRepeat(true);
connect(buttonDecreaseQuantity(),
SIGNAL(clicked()),
this,
SLOT(quantityDecrease()));

m_pButtonTrash=new QPushButton(QIcon(":/delegateIcons/quaZereo"),
tr(""),
this);
Q_ASSERT_X(buttonTrash()!=0,
"Trash pushbutton object allocation",
"Trash pushbutton object could not be allocated.");

buttonTrash()->setSizePolicy(QSizePolicy::Fixed,
QSizePolicy::Fixed);
buttonTrash()->setIconSize(QSize(ICON_WIDTH,
ICON_HEIGHT));
buttonTrash()->setAutoRepeat(false);
connect(buttonTrash(),
SIGNAL(clicked()),
this,
SLOT(quantityZero()));

m_pMainLayout=new QHBoxLayout();
Q_ASSERT_X(mainLayout()!=0,
"Main layout object allocation",
"Main layout object could not be allocated");

m_pMerchandisePic=new QPixmap(ICON_WIDTH+DELTA,
ICON_HEIGHT+DELTA);
Q_ASSERT_X(merchandisePic()!=0,
"Merchandise pic object allocation",
"Merchandise pic object could not be allocated.");

// mainLayout()->addWidget(merchandisePic());
mainLayout()->addLayout(textLayout());
mainLayout()->addLayout(operationsLayout());

setLayout(mainLayout());
}

void MerchandiseTableDelegateWidget::quantityIncrease()
{
}

void MerchandiseTableDelegateWidget::quantityDecrease()
{
}

void MerchandiseTableDelegateWidget::quantityZero()
{
}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

high_flyer
12th January 2010, 09:06
Try the following, just before
setLayout(mainLayout());



QLayout *pOldLayout = this->layout();
if(pOldLayout) delete pOldLayout;
setLayout(mainLayout());

MarkoSan
12th January 2010, 09:44
No effect, I also placed breakpoint on if clause, delete statement is skipped.

numbat
12th January 2010, 10:23
You haven't added the buttons to any layout. The labels lay out fine on my machine.