Qt教程
+ -

QT中UI设计

2025-10-10 0 0

addWidget() 和 addLayout()

addWidget和addLayout都是布局的成员函数。

  • addWidget():用于向布局中添加一个控件(Widget),例如按钮、标签、输入框等。
  • addLayout():用于向布局中添加一个子布局(Layout),从而实现布局的嵌套和复杂界面设计。
特性 addWidget(widget) addLayout(layout)
添加对象 控件 (Widget) 布局 (Layout)
对象类型 QPushButton, QLabel, QLineEdit, QTextEdit 等 QHBoxLayout, QVBoxLayout, QGridLayout 等
主要目的 将具体的UI控件放入布局中管理 实现布局嵌套,构建复杂的用户界面结构
层级关系 布局 管理 控件 父布局 管理 子布局
返回值 通常无直接返回值(或返回布局的索引) 通常无直接返回值(或返回布局的索引)

addLayout

addLayout用于实现layout嵌套。

void QtWidgetsApplication1::SetupUI()
{
    //
    this->resize(300, 200);
    QWidget* centerwidget = new QWidget(this);
    setCentralWidget(centerwidget);


    QLabel* m_statusLabel = new QLabel("Hello World", centerwidget);
    m_statusLabel->setAlignment(Qt::AlignCenter);
    m_statusLabel->setStyleSheet("QLabel { color: white; font-size: 25px; padding: 8px; background: #000000; }");


    QVBoxLayout* topLayout = new QVBoxLayout(centerwidget);
    topLayout->setContentsMargins(0, 0, 0, 0);
    topLayout->setSpacing(0);
    topLayout->addWidget(m_statusLabel);


    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    topLayout->addStretch(1);
    topLayout->addLayout(mainLayout);
    topLayout->addStretch(1);


    QVBoxLayout* leftLayout = new QVBoxLayout(this);
    QVBoxLayout* rightLayout = new QVBoxLayout(this);
    for (int i = 0; i < 6; i++)
    {
        QPushButton* pbtn = new QPushButton(QString("QPushButton %1").arg(i), this);
        if (i < 3)
        {
            leftLayout->addWidget(pbtn);
        }
        else
        {
            rightLayout->addWidget(pbtn);
        }
    }

    // 将左右两列添加到主布局
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
}

155939348441

addWidget

addWidget用于对一个布局增加可显示的控件对象,如常见的QPushButton,QLable等,当然也可以是一个QWidget。
布局与可显示控件对象的关系是:布局创建的父对象是Widget,而布局对象中的子Widget也应是布局的父对象Widget.
103040838746

void MicrophoneTestWindow::setupUI()
{
   QWidget* centerwidget = new QWidget(this);
   setCentralWidget(centerwidget);


   QLabel* m_statusLabel = new QLabel("Hello World", centerwidget);
   m_statusLabel->setAlignment(Qt::AlignCenter);
   m_statusLabel->setStyleSheet("QLabel { color: white; font-size: 25px; padding: 8px; background: #000000; }");

   QWidget* mainWidget = new QWidget(centerwidget);

   QVBoxLayout* topLayout = new QVBoxLayout(centerwidget);
   topLayout->setContentsMargins(0, 0, 0, 0);
   topLayout->setSpacing(0);
   topLayout->addWidget(m_statusLabel);
   topLayout->addWidget(mainWidget);


   QHBoxLayout* mainLayout = new QHBoxLayout(mainWidget);
   mainLayout->setContentsMargins(0, 0, 0, 0);
   mainLayout->setSpacing(0);

   QWidget* leftColumn = new QWidget();
   QWidget* rightColumn = new QWidget();
   QVBoxLayout* leftLayout = new QVBoxLayout(leftColumn);
   QVBoxLayout* rightLayout = new QVBoxLayout(rightColumn);

   for(int i=0;i<6;i++)
   {
       QPushButton* pbtn = new QPushButton(QString("QPushButton %1").arg(i),this);
       if (i < 3)
       {
           leftLayout->addWidget(pbtn);
       }
       else
       {
           rightLayout->addWidget(pbtn);
       }
   }

   // 将左右两列添加到主布局
   mainLayout->addWidget(leftColumn);
   mainLayout->addWidget(rightColumn);
}

120736728708

setContentsMargins和setSpacing

setContentsMargins和setSpacing是布局的成员函数。

  • setContentsMargins 函数用于设置布局周围的边距。
  • setSpacing函数用于设置布局管理器(如QHBoxLayout或QVBoxLayout)中控件之间的间距。
  • addStretch函数是一个非常有用的布局管理工具,它允许开发者在布局中添加一个可伸缩的空间,这个空间可以根据需要进行伸缩,以达到理想的布局效果。addStretch用于将剩余空间进行分配。

0 篇笔记 写笔记

作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!