有几个选项:
每次在绘制/显示小部件之前,检查用户的访问级别,并取决于显示或隐藏控件:
void MyWidget::showEvent(QShowEvent * event) {
if(d_admin == true) {
ui->myAdminEdit->setVisible(true);
} else {
ui->myAdminEdit->setVisible(false);
}
/* Or one line */
ui->myAdminButton->setVisible(d_admin);
}
或者您连接一个信号以根据状态显示所有管理小部件:
/* In some constructor */
ui->myAdminEdit->setVisible(false);
ui->myAdminButton->setVisible(false);
connect(autClass, SIGNAL(adminLoggedIn(bool)), ui->myAdminEdit, SLOT(setVisible(bool)));
connect(autClass, SIGNAL(adminLoggedIn(bool)), ui->myAdminButton, SLOT(setVisible(bool)));
/* And then after authentication */
isAdmin = authenticate(username, password);
emit adminLoggedIn(isAdmin);
发射后,小部件将变得可见。