1. 程式人生 > >Qt設定樣式

Qt設定樣式

1直接採用setStyleSheet方法

   不再贅述。類似css

2載入資原始檔,用setObjectName設定

A、css檔案保留樣式,eg:

QPushButton#btLogin {
    min-height: 28px;
    max-height: 48px;
    min-width: 100px;
    max-width: 100px;
    font-weight: 500;
    color: white;
    border:1px solid #0C1686;
    border-radius: 4px;
    background-color: #0C1686;
}

QPushButton#btLogin:hover:!pressed {
    background-color: #0F1BAE;
}

QPushButton#btLogin:pressed {
    background-color: #090F60;
}

B、設定樣式

    // 樣式
    QFile *file;
    if (QFile::exists("style.css"))
    {
        file = new QFile("style.css");
    }
    else
    {
        file = new QFile(":/style/style.css");
        qDebug()<<"open failed =!";
    }
    file->open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream stream(file);
    stream.setCodec(QTextCodec::codecForName("UTF-8"));
    QString style = stream.readAll();
    this->setStyleSheet(style); //子視窗去繼承
    file->close();
    file->deleteLater();

    pushBtn->setObjectName("btLogin");