在C++中,可以使用QLabel::setStyleSheet()
方法来设置QLabel的样式表,将文本居中。示例代码如下:
#include <QApplication>
#include <QLabel>
#include <QWidget>
#include <QVBoxLayout>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
QVBoxLayout *layout = new QVBoxLayout();
QLabel *label = new QLabel("Hello World!");
label->setStyleSheet("QLabel { text-align: center; }"); // 将文本居中
layout->addWidget(label);
window.setLayout(layout);
window.show();
return app.exec();
}
其中,text-align: center;
是将文本水平居中的CSS属性。