欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中的布局管理器

发布时间:2025/4/5 编程问答 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Qt中的布局管理器 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • 1 Qt中的布局管理器
      • 1.1 绝对定位存在的问题
      • 1.2 Qt中的布局管理器
      • 1.3 布局管理器的注意事项
    • 2 布局管理器综合示例

1 Qt中的布局管理器

1.1 绝对定位存在的问题

绝对定位直接在像素级指定各个组件的位置和大小:

  • void QWidget::move(int x, int y)
  • void QWidget::resize(int w, int h)

绝对定位存在的问题:

  • 组件的位置和大小无法自适应父窗口的变化。

1.2 Qt中的布局管理器

布局管理器提供了相关的类对界面组件进行布局管理:

  • 能够自动排列窗口中的界面组件。
  • 窗口变化后自动更新界面组件的大小。

QLayout:

  • QLayout是Qt中布局管理器的抽象基类。
  • 通过继承QLayout实现了功能各异且互补的布局管理器。
  • Qt中可以根据需要自定义布局管理器。
  • 布局管理器不是界面部件,而是界面部件的定位策略。

1.3 布局管理器的注意事项

对于布局管理器:

  • 任意容器类的组件都可以指定布局管理器。
  • 同一个布局管理器中的组件拥有相同的父组件。
  • 设置布局管理器的同时隐式的指定了父子关系。


组件1和组件2的父组件均为QWidget对应的对象。


2 布局管理器综合示例

需求分析:练习开发一个向导用户界面

  • 在同一个界面上展现不同的向导页面。
  • 通过“上一步”和“下一步”按钮进行切换。
  • 不同页面上的元素组件和组件排布都不相同。
  • 页面中的组件通过布局管理器进行排布。

通过布局嵌套进行界面设计:

通过QStackedLayout管理不同的页面:

通过子组件的方式生成不同的页面:

示例代码如下:
Widget.h:

#ifndef _WIDGET_H_ #define _WIDGET_H_#include <QtGui/QWidget> #include <QPushButton> #include <QLabel> #include <QLineEdit> #include <QStackedLayout>class Widget : public QWidget {Q_OBJECTprivate:QPushButton preBtn;QPushButton nextBtn;QLabel fLbl1;QLabel fLbl2;QLabel fLbl3;QLabel fLbl4;QLineEdit sLineEdit;QPushButton tPushBtn1;QPushButton tPushBtn2;QStackedLayout sLayout;void initControl();QWidget* get1stPage();QWidget* get2ndPage();QWidget* get3rdPage(); private slots:void onPreBtnClicked();void onNextBtnClicked(); public:Widget(QWidget* parent = 0);~Widget(); };#endif // _WIDGET_H_

Widget.cpp:

#include "Widget.h" #include <QVBoxLayout> #include <QHBoxLayout> #include <QGridLayout> #include <QFormLayout> #include <QDebug>Widget::Widget(QWidget *parent) : QWidget(parent) {initControl(); }void Widget::initControl() {QVBoxLayout* vLayout = new QVBoxLayout();QHBoxLayout* hLayout = new QHBoxLayout();preBtn.setText("Pre Page");preBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);preBtn.setMinimumSize(160, 30);nextBtn.setText("Next Page");nextBtn.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);nextBtn.setMinimumSize(160, 30);connect(&preBtn, SIGNAL(clicked()), this, SLOT(onPreBtnClicked()));connect(&nextBtn, SIGNAL(clicked()), this, SLOT(onNextBtnClicked()));hLayout->addWidget(&preBtn);hLayout->addWidget(&nextBtn);sLayout.addWidget(get1stPage());sLayout.addWidget(get2ndPage());sLayout.addWidget(get3rdPage());vLayout->addLayout(&sLayout);vLayout->addLayout(hLayout);setLayout(vLayout); }QWidget* Widget::get1stPage() {QWidget* ret = new QWidget();QGridLayout* layout = new QGridLayout();fLbl1.setText("This");fLbl2.setText("is");fLbl3.setText("1st");fLbl4.setText("page");layout->addWidget(&fLbl1, 0, 0);layout->addWidget(&fLbl2, 0, 1);layout->addWidget(&fLbl3, 1, 0);layout->addWidget(&fLbl4, 1, 1);ret->setLayout(layout);return ret; }QWidget* Widget::get2ndPage() {QWidget* ret = new QWidget();QFormLayout* layout = new QFormLayout();sLineEdit.setText("This is 2rd page");layout->addRow("Hint:", &sLineEdit);ret->setLayout(layout);return ret; }QWidget* Widget::get3rdPage() {QWidget* ret = new QWidget();QVBoxLayout* layout = new QVBoxLayout();tPushBtn1.setText("This is");tPushBtn2.setText("3rd page");layout->addWidget(&tPushBtn1);layout->addWidget(&tPushBtn2);ret->setLayout(layout);return ret; }void Widget::onPreBtnClicked() {int index = ((sLayout.currentIndex() - 1) + 3) % 3;sLayout.setCurrentIndex(index); }void Widget::onNextBtnClicked() {int index = (sLayout.currentIndex() + 1) % 3;sLayout.setCurrentIndex(index); }Widget::~Widget() {}

main.cpp:

#include <QtGui/QApplication> #include "Widget.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }

参考资料:

  • QT实验分析教程
  • 总结

    以上是生活随笔为你收集整理的Qt中的布局管理器的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。