Put a button on the qlabel that fills the screen
-
Fill the window with an image like the one pictured above.
And the button is located in the middle below.
But my code doesn't work like that. The imageLabel is just very small and centered.
where do i need to fix it?ui->setupUi(this); mainLayout = new QVBoxLayout; QLabel *imageLabel = ui->label; imageLabel->setStyleSheet("QLabel { background-color : yellow; color : blue; }"); imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); imageLabel->setScaledContents(true); mainLayout->addWidget(imageLabel, 0, Qt::AlignCenter); buttonLayout = new QHBoxLayout; buttonLayout->addWidget(ui->beginBtn); buttonLayout->addWidget(ui->prevBtn); buttonLayout->addWidget(ui->NextBtn); buttonLayout->addWidget(ui->endBtn); buttonLayout->setAlignment(Qt::AlignBottom | Qt::AlignHCenter); mainLayout->addLayout(buttonLayout); setCentralWidget(new QWidget); centralWidget()->setLayout(mainLayout); createStatusBar();
If I do this part as follows, the qlabel is filled, but the button is pushed below it.
mainLayout->addWidget(imageLabel);
-
If you have to use a
QLabel
for your background (because there better ways to do so) then I suggest using itsgeometry
.
set itspos
to(0,0)
, andsize
to the size of your window or central widget, and you might also need to change itsZ-Order
in which case you can uselower
orraise
functions
this topic might be of use : Bring widget at the back of another widget -
@MyNameIsQt If I understand it correctly, you want to add two buttons on top of label image. Then
buttonLayout = new QHBoxLayout( imageLabel ); // this layout is for imageLabel
comment this out
mainLayout->addLayout(buttonLayout);
add
mainLayout->addWidget( imageLabel ); -
I dont know the purpose of your image, but you could also fill the background of your
centralWidget
with it (by using stylesheet, for example) and then simply add your buttons to it. -
If you have to use a
QLabel
for your background (because there better ways to do so) then I suggest using itsgeometry
.
set itspos
to(0,0)
, andsize
to the size of your window or central widget, and you might also need to change itsZ-Order
in which case you can uselower
orraise
functions
this topic might be of use : Bring widget at the back of another widget -