int width = this->width();
this->resize(width, 28);
this->setWindowFlags(Qt::FramelessWindowHint);//设置标题栏隐藏
QPalette palette;//设置背景色
QColor color(190, 230, 250);
color.setAlphaF(0.6);
palette.setBrush(this->backgroundRole(), color);
this->setPalette(palette);
this->setAutoFillBackground(true);
//构建关闭按钮
close_button = new QToolButton(this);
QPixmap close_pix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
close_button->setIcon(close_pix);
close_button->setStyleSheet("QToolButton{background-color:transparent;}");
//获取主界面的宽度
int height = this->height();
close_button->setGeometry(width - 20, 0, 20, 20);
//设置提示图片
msg_label = new QLabel(this);
msg_label->setGeometry(QRect(5, 5, 20, 20));
msg_label->setStyleSheet("background-color:transparent;");
msg_label->setScaledContents(true);
//设置提示信息
ask_label = new QLabel(this);
ask_label->setStyleSheet("background-color:transparent;color:red;");
ask_label->setGeometry(QRect(30, 0, width - 60, height));
ask_label->setAlignment(Qt::AlignCenter);
close_button->setCursor(Qt::PointingHandCursor);
QObject::connect(close_button, SIGNAL(clicked()), this, SLOT(closeWidget()));
void errorTest::setTipInfo(QString info)
{
//设置提示信息
ask_label->setText(QString::fromLocal8Bit(info));
}
void errorTest::setTipIcon(QPixmap pixmap)
{
msg_label->setPixmap(pixmap);
}
//关闭按钮主要进行提示框的隐藏
bool errorTest::closeWidget()
{
this->hide();
return true;
}
下面是引用上面代码的地方:
//进行错误提示
QPixmap pixmap = QPixmap(":/icon/errortip");
error_widget->setTipIcon(pixmap);
error_widget->setTipInfo(info);
if(error_widget->isHidden())
{
error_widget->show();
}