分享

Qt文档阅读笔记-stackUnder官方解析与实例

 勤奋不止 2019-06-25

目录

官方解析

博主例子


官方解析

这里可以配合raise()和lower()这两个函数来使用!

 

博主例子

用2个label,点击谁谁就浮在界面的最上面,很简单的代码,程序运行截图如下:

源码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QLabel;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    ~Widget();

private:
    Ui::Widget *ui;

    bool m_otherWidget;
    QList<QLabel*> m_list;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QLabel>
#include <QMouseEvent>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    m_otherWidget = false;
    QLabel *label1 = new QLabel(this);
    label1->setText("  label 1");
    label1->setGeometry(60, 60, 160, 110);
    label1->setStyleSheet("background-color: rgb(0,0,0); color: rgb(255,0,0);");
    label1->stackUnder(this);


    QLabel *label2 = new QLabel(this);
    label2->setGeometry(50, 50, 150, 100);
    label2->setText("  label 2");
    label2->setStyleSheet("background-color: rgb(0,255,255); color: rgb(255,0,0);");
    label2->stackUnder(this);

    label1->raise();
    label2->raise();
    qDebug()<<this->children();
    m_list<<label1;
    m_list<<label2;
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        if(m_otherWidget){
            m_list[0]->raise();

        }
        else{
            m_list[1]->raise();
        }
        m_otherWidget = !m_otherWidget;
        event->accept();
        this->update();
    }
}

Widget::~Widget()
{
    delete ui;
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多