0

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);

}
Community
  • 1
  • 1
spring
  • 18,009
  • 15
  • 80
  • 160
  • you don't need to pass parent in constructor, QHBoxLayout* hbox = new QHBoxLayout();, same to ImageLabel2* image = new ImageLabel2(); because when you call addLayout or addWidget, ownership is transferred to layout. – Kunal Aug 19 '13 at 01:09
  • @Kunal - thanks. Yeah, I read up a bit more and understand about the ownership better. Still trying to get the center align working though. – spring Aug 19 '13 at 01:34
  • may be you need to try out setStretch(), I am not sure though – Kunal Aug 19 '13 at 01:47
  • The Question is, has your ImageLabel instance a fixed size or at least a maximum size? Without a maximum size Qt Layout will stretch the entire control. Test this by setting the `background-color` to red for example. – bkausbk Aug 19 '13 at 05:54
  • Are you sure it's your `ImageLabel2` widget that's not centered? Maybe it's the image inside that widget thats not centered. – thuga Aug 19 '13 at 09:01

3 Answers3

2

Try this:

QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addStretch();
hbox->addWidget(image);
hbox->addStretch();
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
1

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")));
}
spring
  • 18,009
  • 15
  • 80
  • 160
0

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()
{

}
Ashif
  • 1,652
  • 14
  • 30