0

I have QLayout class which accept QLabel and align it as Qt::AlignRight in cell, Initially I think it is working(since the size of layout cell is exactly equal to size of pixmap), I have an event associated with the QLabel, ie when mousepressEvent occured the size of QLabel increase(size of cell also increase so the size of entire column increases), That time the other QLabel in QLayout are getting left aligned, I want them as right aligned or centre aligned instead of left aligning,

My code is,

Container::Container()
{ 
    Layout = new QGridLayout;
    Layout->setHorizontalSpacing(0);
    Layout->setVerticalSpacing(10);
    Layout->setMargin(10); 
    for(int i = 0; i < 4; ++i)
    {
        holes[i] = new Hole; 
        Layout->addWidget(ui_holes[i], i, 0, 1, Qt::AlignRight);
        ui_holes[i].setPixmap("mypixmapname.png")
    } 
    Layout->setAlignment(Qt::AlignCenter);
    setLayout(Layout); 
    setMaximumSize(200,760);
    setMinimumSize(200,760);
    setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed);
}

void Screen::mousePressEvent(QMouseEvent *tevent)
{ 
    if(childAt(tevent->pos()))
    { 
        if(childAt(tevent->pos())->objectName() == "Hole")
        { 
            hole = static_cast<Hole *>(childAt(tevent->pos()));  
            hole->resize(QSize(160,160));  
        }
    }  
}  

void Screen::mouseReleaseEvent(QMouseEvent*)
{ 
    if(hole)
    {
        ui_Hole->resetSize();  
    } 
}

Hole is class inherited from QLabel and I have created two new member functions for Hole is resetSize and resize,

void Hole::resize(QSize size)
{
    setSize(size);
    if(!ui_HoleFlags[PIXMAP_EXISTS])
        return  void(0);
    QPixmap *tempPixmap = ui_resourceIcon();
    setPixmap(tempPixmap->scaled(size,Qt::IgnoreAspectRatio));
    delete tempPixmap;
} 

QPixmap* Hole::ui_resourceIcon()
{
    if(!ui_HoleFlags[ICON_EXISTS])
        return NULL;
    QPixmap *tempPixmap = new QPixmap(*pixmap());
    return tempPixmap;
}

void Hole::setSize(QSize size)
{
    setMaximumSize(size);
    setMinimumSize(size);
}

void Hole::resetSize()
{
    if(ui_HoleFlags[PIXMAP_EXISTS])
        setPixmap(*Pixmap);
    setSize(ICON_SIZE);
}

Thanks in advance

Akhil V Suku
  • 870
  • 2
  • 13
  • 34

1 Answers1

1

This way you will get labels always center aligned, no matter what size individual labels are.

#include <QLabel>
#include <QApplication>
#include <QLayout>
#include <QMouseEvent>

class Window : public QWidget
{
public:
    Window(QWidget *parent = 0) {}
    virtual ~Window() {}

protected:
    void mousePressEvent(QMouseEvent *event)
    {
        if (childAt(event->pos())) {
            QLabel *label = dynamic_cast<QLabel *>(childAt(event->pos()));
            if (label) {
                label->setMinimumSize(QSize(50, 50));
                label->setMaximumSize(QSize(50, 50));
            }
        }
    }
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    Window *window = new Window;
    window->show();
    window->resize(400, 300);

    QVBoxLayout *vLayout = new QVBoxLayout;

    QLabel *labels[4];
    for(int i = 0; i < 4; ++i) {
        labels[i] = new QLabel();
        labels[i]->setMaximumSize(30, 30);
        labels[i]->setMinimumSize(30, 30);

        QHBoxLayout *hLayout = new QHBoxLayout;
        hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
        hLayout->addWidget(labels[i]);
        hLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
        vLayout->addLayout(hLayout);
    }

    window->setLayout(vLayout);
    window->setStyleSheet("QLabel {border: 1px solid red;}");

    return app.exec();
}

enter image description here

svlasov
  • 9,923
  • 2
  • 38
  • 39
  • How spacer is working ? I have some doubts, I will explain my situation first, I have a Grid with only one column. All having size 100,100, When mouse click the clicked item increases its size to 200,200 that time the all the cells under the same column will increase their size, due to this the icons except clicked one(is zoomed right now) are aligning as left. I need something like when the cell size increased I need them centre aligned, (or spacer should work like pad the icon as centre aligned) – Akhil V Suku Feb 24 '15 at 15:08
  • I need to get the hole is centre aligned in layout in all cases – Akhil V Suku Feb 24 '15 at 15:11
  • Even I need to stick with QGridLayout, because I use one row orl become one column depending on a variable set by user Thanks for your help, I will check this and give feedback – Akhil V Suku Feb 24 '15 at 15:48
  • Now It is not aligning as centre or right, When I am setting spacer as 200 it will make the cell size 200. But alignment is not working still – Akhil V Suku Feb 25 '15 at 03:20
  • Why do you set spacer size to 200? I updated my answer with complete source, upon clicking a label it get resized and remain aligned to center. – svlasov Feb 25 '15 at 10:14
  • unfortunately It was not working for me that's why I have change it to 200, I will try the latest one :) – Akhil V Suku Feb 25 '15 at 10:43