Qt自定义用户主题

时间:2014-02-20 21:38:12

标签: css qt

我正在尝试设置一个使用外部文件进行主题化的方法。目前,我有一个文件被读取并放入QString,然后被放置在“qApp-> setStyleSheet(string);”中,这似乎有效,但是,当我为按钮的背景着色它似乎不同样的css也直接在qt设计师的内部工作。

功能:

void SeniorProject::themer(QString theme_name)
{
    qDebug() << theme_name;
    QString file = QCoreApplication::applicationDirPath()
            + "/themes/" + "default" + "/theme.style";
    qDebug() << "file = " + file;
    QFile themeFile(file);
    QString themeStyle;
    if (themeFile.open(QIODevice::ReadOnly))
    {
        QTextStream in (&themeFile);
        themeStyle = in.readAll();
        themeFile.close();
    }
    else
    {
        qDebug() << "error";
    }
    qApp->setStyleSheet(themeStyle);
    update();
}

CSS文件

QPushButton#exit {
    color: rgb(220, 0, 0);
    border: none;
    outline: none;
}
QPushButton#exit:hover {
    color: rgb(255, 8, 0);
}
QPushButton#exit:Pressed {
    color: rgb(150, 0, 0);
}


QFrame#mainbox QPushButton {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgba(122,188,255,1), stop: 0.44 rgba(96,171,248,1), stop: 1 rgba(64,150,238,1));
    border: .1px outset rgb(122, 188, 255);
    border-radius:4px;
}
QFrame#mainbox QPushButton:hover {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgba(147,201,255,1), stop: 0.44 rgba(133,190,247,1), stop: 1 rgba(90,163,237,1));
    border: .1px outset rgb(122, 188, 255);
    border-radius:4px;
}
QFrame#mainbox QPushButton:pressed {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgba(84,169,255,1), stop: 0.44 rgba(66,155,244,1), stop: 1 rgba(45,141,237,1));
    border: .1px outset  rgb(122, 188, 255);
    border-radius:4px;
}

QPushButtons中的背景是我当前的问题,样式表正确加载和更新(我可以告诉因为按钮上的边框实际上改变为我想要的),但背景似乎不起作用。谁能帮我?感谢。

1 个答案:

答案 0 :(得分:0)

我弄清楚为什么背景不起作用。如果您之前设置的整个小部件的背景是您尝试通过QT设计器设置样式的项目的父级,则在未使用QT Designer设置时将无法获得背景。