for(int i = 0;i<10;i++){ MaxWidget = new QWidget; QHBoxLayout * Hbox_1 = new QHBoxLayout; QLabel * lab = new QLabel;//头像 Hbox_1->setAlignment(Qt::AlignVCenter); lab->setMaximumWidth(40); lab->setStyleSheet("background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #5B5F5F, stop: 0.5 #0C2436,stop: 1.0 #27405A); border:1px solid black;"); QVBoxLayout * Vbox_1 = new QVBoxLayout; QHBoxLayout * HbSjMz = new QHBoxLayout; QLabel * sj = new QLabel("14:30"); sj->setMaximumWidth(40); sj->setMinimumHeight(25); QLabel * mz = new QLabel(QStringLiteral("Qt 设计师")); HbSjMz->addWidget(mz); HbSjMz->addWidget(sj); QLabel * msg = new QLabel(QStringLiteral("哈哈")); sj->setAttribute(Qt::WA_TranslucentBackground); mz->setAttribute(Qt::WA_TranslucentBackground); msg->setAttribute(Qt::WA_TranslucentBackground); Vbox_1->addLayout(HbSjMz); Vbox_1->addWidget(msg); msg->setMinimumHeight(25); Hbox_1->addWidget(lab);
Hbox_1->addLayout(Vbox_1);
MaxWidget->setLayout(Hbox_1); MaxWidget->setMaximumSize(400,50); QLabel * line = new QLabel; line->setStyleSheet("background: #444444"); line->setMinimumHeight(1); line->setMaximumHeight(1); ui->verticalLayout->addWidget(MaxWidget); ui->verticalLayout->addWidget(line); ui->verticalLayout->setMargin(0);
MaxWidget->setStyleSheet("background-color:transparent"); MaxWidget->installEventFilter(this); Vbox_1->setSpacing(0); HbSjMz->setSpacing(0);
} ui->verticalLayout->setAlignment(Qt::AlignVCenter); ui->verticalLayout->addStretch();//弹簧为什么我widget里面的lab (头像) 不能居中显示。。。。