spring
spring

Reputation: 18517

Centering widgets in a dynamic layout?

I'm using the answer to this SO question to make a custom image widget which automatically scales correctly. It works fine but now I am trying to center the image widget instance at the center of my main window.

My idea was to create a QHBoxLayout, add the image widget to that and then add the hBox instance to the ui->verticalLayout.

Doesn't work. The image still displays flush left with the error message: QLayout: Attempting to add QLayout "" to MainWindow "MainWindow", which already has a layout

I then tried a few variations on 'setAlignment` but then the image doesn't appear at all. My simple test code is below.

What am I missing here?

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QPixmap pix;
    pix.load("/Users/home/Desktop/test.jpg");

    ImageLabel2* image = new ImageLabel2(this);
    image->setPixmap(pix);

    QHBoxLayout* hbox = new QHBoxLayout(this);
    hbox->addWidget(image);

    //    hbox->setAlignment(image,Qt::AlignCenter);

    hbox->setAlignment(Qt::AlignHCenter);

    //    ui->verticalLayout->addLayout(hbox);

    ui->verticalLayout->addLayout(hbox);

    //    ui->verticalLayout->addWidget(image);
    //    ui->verticalLayout->setAlignment(image,Qt::AlignCenter);
    //    ui->verticalLayout->setAlignment(Qt::AlignHCenter);

}

Upvotes: 0

Views: 9355

Answers (3)

spring
spring

Reputation: 18517

None of the suggestions here worked for me. Don't know why and it seems hard to debug layout problems

However a reply to my question on the Qt Project.org site works perfectly. So not my solution but I am posting it here since this "centering/resizing image" issue seems a common problem.

class CustomWidget : public QWidget {
public:
    CustomWidget(const QPixmap &p, QWidget* parent = 0)
        : QWidget(parent), pixmap(p) {}

    void paintEvent(QPaintEvent * e)
    {
        QRect srcRect(QPoint(), pixmap.size());
        QSize dstSize = srcRect.size().scaled(
              e->rect().size(), Qt::KeepAspectRatio);
        QRect dstRect(QPoint((width() - dstSize.width())/2,
                             (height() - dstSize.height())/2), dstSize);

        QPainter p(this);
        p.setRenderHint(QPainter::Antialiasing);
        p.drawPixmap(dstRect, pixmap, srcRect);
    }
private:
    QPixmap pixmap;
};

and then in the main window:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(new CustomWidget(QPixmap("test.png")));
}

Upvotes: 1

Ashif
Ashif

Reputation: 1684

main window(QMainwindow) API setCentralWidget to align the widget on center.

Note:Concept of centrewidget, is to differentiate the docking area( Left, Right, Bottom, Top ). with out a centre how some one know who is where. when we are developing with QMainWindow, you can see in UI_MainWindow.h, setting Centrewidget with dummy QWidget

Below code will work

#include "mainwindow.h"
#include <QLabel>
#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);

    QPixmap pix;
    pix.load("C:\\Users\\user\\Desktop\\Uninstallation failure2.png");

    //Replace with  ImageLabel2
    QLabel* image = new QLabel(this);
    image->setPixmap(pix);

    QHBoxLayout* hbox = new QHBoxLayout(this);
    hbox->addWidget(image);
    QWidget* centreWidget = new QWidget();

    //QMainwindow, having a feature called centreWidget, to set the layout.
    centreWidget->setLayout( hbox ); 
    setCentralWidget( centreWidget );
}


MainWindow::~MainWindow()
{

}

Upvotes: 0

Jeremy Friesner
Jeremy Friesner

Reputation: 73304

Try this:

QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addStretch();
hbox->addWidget(image);
hbox->addStretch();

Upvotes: 2

Related Questions