stackUnder是什么意思?根据官方的解析,简而言之一句话:把窗口的小部件放置于父窗口的堆栈窗口中。本文将通过一则简单的示例,来具体解析stackUnder该如何使用。
官方解析
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.
See also raise() and lower().
示例代码
#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
for (int i=0; i<10; i++)
{
QLabel *label = new QLabel(this);
label->setText("label_" + QString::number(i));
//
QRect rc = QRect(30+i*30, 30+i*20, 150, 80);
label->setGeometry(rc);
//
QString qss = QString("background-color:rgb(100,%1,200); "
"border-radius:10px; "
"color:rgb(250,%2,250);").arg(i*10).arg(200+i*2);
label->setStyleSheet(qss);
//
label->stackUnder(this);
label->raise();
m_labels << label;
m_rects << rc;
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
QPoint pt = event->pos();
int index = 0;
for (int i=0; i<m_rects.size(); i++) {
QRect rc = m_rects.at(i);
if(rc.contains(pt.x(), pt.y())){
index = i;
}
}
m_labels.at(index)->raise();
this->update();
event->accept();
}
}