当前位置:   article > 正文

【QT学习笔记】QT教程:窗口设置Z方向的层叠顺序_qt z序

qt z序

QT--窗口设置Z方向的层叠顺序

方法1:在ui 文件中设置

在UI文件中,设置Z方向的层叠顺序,只能将窗口放到其父窗口的最上面,或者放到父窗口的最下面。
见下图

方法2:在cpp中通过代码实现(Qt自带API)


(1)将窗口置顶

  1. void QWidget::raise()

[slot] void QWidget::raise()
Raises this widget to the top of the parent widget’s stack.
After this call the widget will be visually in front of any overlapping sibling widgets.
Note: When using activateWindow(), you can call this function to ensure that the window is stacked on top.

(2)将窗口放到最底层

  1. void QWidget::lower()

[slot] void QWidget::lower()
Lowers the widget to the bottom of the parent widget’s stack.
After this call the widget will be visually behind (and therefore obscured by) any overlapping sibling widgets.

(3)将窗口放到某个窗口之下

  1. void QWidget::stackUnder(QWidget *w)

void QWidget::stackUnder(QWidget *w)
Places the widget under w in the parent widget’s stack.
To make this work, the widget itself and w must be siblings.

使用示例:

创建一个Widget,两个Lable,Widget作为Label的父类,Lable和父类宽高相等,使用绝对定位布局与左上角对齐,使Label完全重叠,然后通过raise()和lower切换它们在Z轴上的顺序,以实现决定显示谁。

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QWidget>
  5. #include <QLabel>
  6. #include <QTimer>
  7. namespace Ui {
  8. class MainWindow;
  9. }
  10. class MainWindow : public QMainWindow
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit MainWindow(QWidget *parent = nullptr);
  15. ~MainWindow();
  16. QWidget* widget_volumn;
  17. QLabel* label1;
  18. QLabel* label2;
  19. QTimer* timer;
  20. bool x;
  21. public slots:
  22. void slot_timeout();
  23. private:
  24. Ui::MainWindow *ui;
  25. };
  26. #endif // MAINWINDOW_H
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. widget_volumn = new QWidget;
  9. this->setCentralWidget(widget_volumn);
  10. label1 = new QLabel(widget_volumn);
  11. label1->setStyleSheet("background-color: red;");
  12. label2 = new QLabel(widget_volumn);
  13. label2->setStyleSheet("background-color: green;");
  14. label1->setGeometry(0,0,widget_volumn->width(),widget_volumn->height());
  15. label2->setGeometry(0,0,widget_volumn->width(),widget_volumn->height());
  16. timer = new QTimer;
  17. connect(timer,SIGNAL(timeout()),this,SLOT(slot_timeout()));
  18. timer->start(1000);
  19. x = false;
  20. }
  21. MainWindow::~MainWindow()
  22. {
  23. delete ui;
  24. }
  25. void MainWindow::slot_timeout()
  26. {
  27. x = !x;
  28. if(x)
  29. {
  30. label1->raise();
  31. label2->lower();
  32. }
  33. else
  34. {
  35. label1->lower();
  36. label2->raise();
  37. }
  38. }

参考文献:

Qt窗口设置Z方向的层叠顺序

 Qt--浮动效果(Z方向层叠切换)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/916019
推荐阅读
相关标签
  

闽ICP备14008679号