QLabel 在左边添加小图标

不废话,直接上代码:

m_plbTip = new QLabel(this);
    LedtLeftBtnFactory fac;
    QPushButton * pbtnIco = fac.buildIcoBtn(this,":/Login/tipIcon.png");
    QImage img(":/Login/tipIcon.png");
    pbtnIco->setFixedSize(img.size());


    QHBoxLayout * pLay = new QHBoxLayout();
    pLay->setContentsMargins(0,0,0,0);
    pLay->addSpacing(5);
    pLay->addWidget(pbtnIco);
    pLay->addStretch(5);
    m_plbTip->setLayout(pLay);
    m_plbTip->setContentsMargins(0,10,10,10);

    //m_plbTip->setFixedSize(270,38);
    m_plbTip->hide();

    m_plbTip->setStyleSheet("color:#cc0000;background-color:#fff9ef;border-width: 1px;border-style: solid;border-color: #ff5b5b;");

如果只有上面的代码,则出来的效果的文字的左边会被遮挡,因此我们在setText时,要添加一些空格

void LoginViewUI::ShowTip(QString str){

    m_plbTip->setText("   "+str);
    m_plbTip->show();
}

效果图


猜你喜欢

转载自blog.csdn.net/sspdfn/article/details/80967760
今日推荐