Qt文档阅读笔记-stackUnder官方解析与实例
生活随笔
收集整理的這篇文章主要介紹了
Qt文档阅读笔记-stackUnder官方解析与实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
官方解析
博主例子
官方解析
這里可以配合raise()和lower()這兩個函數來使用!
?
博主例子
用2個label,點擊誰誰就浮在界面的最上面,很簡單的代碼,程序運行截圖如下:
源碼如下:
widget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE class QLabel; QT_END_NAMESPACEnamespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic: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_Hwidget.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(); }?
總結
以上是生活随笔為你收集整理的Qt文档阅读笔记-stackUnder官方解析与实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-进入文件夹或打开网站(QD
- 下一篇: Qt学习笔记-简单的TCP程序