赞
踩
在UI文件中,设置Z方向的层叠顺序,只能将窗口放到其父窗口的最上面,或者放到父窗口的最下面。
见下图
(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)将窗口放到最底层
- 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)将窗口放到某个窗口之下
- 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轴上的顺序,以实现决定显示谁。
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
- #include <QWidget>
- #include <QLabel>
- #include <QTimer>
-
- namespace Ui {
- class MainWindow;
- }
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- explicit MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- QWidget* widget_volumn;
- QLabel* label1;
- QLabel* label2;
-
- QTimer* timer;
-
- bool x;
-
- public slots:
- void slot_timeout();
-
- private:
- Ui::MainWindow *ui;
- };
-
- #endif // MAINWINDOW_H
-
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
- widget_volumn = new QWidget;
- this->setCentralWidget(widget_volumn);
- label1 = new QLabel(widget_volumn);
- label1->setStyleSheet("background-color: red;");
- label2 = new QLabel(widget_volumn);
- label2->setStyleSheet("background-color: green;");
-
- label1->setGeometry(0,0,widget_volumn->width(),widget_volumn->height());
- label2->setGeometry(0,0,widget_volumn->width(),widget_volumn->height());
-
- timer = new QTimer;
- connect(timer,SIGNAL(timeout()),this,SLOT(slot_timeout()));
- timer->start(1000);
-
- x = false;
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- void MainWindow::slot_timeout()
- {
- x = !x;
- if(x)
- {
- label1->raise();
- label2->lower();
- }
- else
- {
- label1->lower();
- label2->raise();
- }
- }
-
参考文献:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。