3

I'm new to QT and I want to know how to add a label on a certain position by code. I create a new application and I have these code automatically:

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

MainWindow::~MainWindow()
{
    delete ui;
}

I want to add a label to a certain position, for example, its geometry is (10, 10, 30, 80). How do I do this by code? Someone can help me?

Hongxu Jin
  • 817
  • 2
  • 9
  • 16

1 Answers1

8

You need to create an instance of QLabel, like this: (Here is the example from the docs. Try implementing it yourself with your standards. The docs are helpful)

QLabel *label = new QLabel(this);
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
label->setText("first line\nsecond line");
label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
//Here is how to change position:
label->setGeometry(QRectF(10,10,30,80));

By the way, here is what an example QLabel looks like:

Text Label

Community
  • 1
  • 1
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88