The central widget

The central area of a QMainWindow is taken by a widget that, not surprisingly, is called central widget.

Let's say the widget we want to show to the user in our main window is just a label saying hello.

What we have to do is creating a label on the heap, passing to the constructor a pointer to the main window, delegating in this way to Qt the nuisance of removing it from the memory when required, set it how we like and then passing its pointer to the QMainWindow::setCentralWidget() method.

That is, something like that:
void WinMain::sayHello()
  QLabel* pLb = new QLabel(this);
  pLb->setText("Hello Qt!");
  pLb->setFont(QFont("Times", 15, 0, true));
  pLb->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

It is worthy noticing the call to QLabel::setAlignment() where we pass the alignment mode we want to use for our label. Since we want to apply more than one value, actually we want the text in the label to be centered both horizontally and vertically, we OR the required modes.

No comments:

Post a Comment