Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLayout aligning widget as wrong
Qt 6.11 is out! See what's new in the release blog

QLayout aligning widget as wrong

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 516 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Akhil. V . S
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved