Solved QLabel not starting from 0,0
-
I am doing a project where I take the input coordinates on mouse clicked.
But when I set the Qlabel on the QMainWindow, I can see that there is a margin between QLabel and leftmost edge and topmost edge of the window.
I checked in the
mainwindow.ui
but the margin value is set as 0. So if I want the QLabel to start from top left corner then what should I do? -
Hi @Arqam
that's probably the margin of your layout. Try it with layoutLeftMargin, etc. set to 0.
-Michael.
-
Check the margins in layouts and in QLabel, too.
Also, see if the margin is not due to layout stretching (if the window is much bigger than your QLabel, the label will be stretched to fill the window, or if it is fixed size it can center itself).
-
@sierdzio @m-sue All my values are as per default.
Yeah I have fixed my window size as
700,700
.
My layoutLeftMargin is also set as 0.My UI window part : https://pastebin.com/TAEBh1Wx
-
Hi,
use setContentsMargin(0,0,0,0); method for both main layout and label. -
-
@m.sue said in QLabel not starting from 0,0:
<layoutdefault spacing="6" margin="11"/>
That section has a GUI and the XML is not editable. Any way that change can be done?
-
Hi @Arqam
I thought you wrote the XML. Then see above:
@Venkatesh-V said in QLabel not starting from 0,0:
use setContentsMargin(0,0,0,0); method for both main layout and label.
-Michael.
-
@Venkatesh-V @m-sue
How can I do that, I know for my QLabel containing the image I can do
ui->label_image->setContentsMargins(0,0,0,0);
Constructor :
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { this->setFixedSize(700,700); ui->setupUi(this); ui->label_image->setContentsMargins(0,0,0,0); }
So how can we change for main layout also, as just changing the value for label didn't do any thing.
-
@Arqam ,
First add your label to main layout and then call,
mainLayout->setContentMargins(-1,-1,-1,-1);
-
@Vinod-Kuntoji
I did :this->setContentsMargins(-1,-1,-1,-1); ui->label_image->setContentsMargins(0,0,0,0);
What it did is just that a bit to the left (and other direction) the margin is reduced.
When I give the default value i.e. -11 that is given in the XML then it starts from the leftmost region.
But when given for the top position i.e (-11,-11,0,0) QLabel doesn't start from the topmost part. -
@Arqam ,
I tried like this.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mLabel = new QLabel(this);
mLayout = new QVBoxLayout(this);QPixmap pix(":/image.PNG"); mLabel->setPixmap(pix); mLayout->addWidget(mLabel);
// mLayout->setContentsMargins(0,0,0,0);
mLayout->setMargin(0);
mLayout->setSpacing(0);
mLayout->setAlignment(mLabel,Qt::AlignTop);
QWidget *widget = new QWidget(this);
widget->setLayout(mLayout);this->setCentralWidget(widget);
}
There is a toolbar on top of the label, thats why it is showing some space on top of label. If you dont want the toolbar then remove it.
-
@Vinod-Kuntoji said in QLabel not starting from 0,0:
this->setCentralWidget(widget);
My widget main window is not having a toolbar, it was having a status bar which I removed.
The structure of my window is like this :![alt text]( image url)
If going by your example my mLayout should be Central Widget?
-
@Arqam ,
Please mark your issue as solved.