QT学习记录-样式表属性整理

修改某一类控件如QWidget
QWidget{
border:1px solid gray;
background:blue;
}
修改某一类指定的控件
QWidget#MyWidget{
background:blue;
}
整个界面那个多控件要设置stylesheet,个人感觉最方便的方法就是写在一个qss文件中,然后项目工程添加资源,读取文件设置,例如:

void MyWidget::loadStyleSheet()
{
    //工程没添加资源时,要使用绝对路径,添加了资源时,
    //直接右击复制相对路径就可以了
    QFile file(":/style/Res/style.qss");
    if (!file.open(QFile::ReadOnly))
    {
        qDebug() << "open stylesheet file failed";
        return;
    }
    QString styleSheet = QString::fromLatin1(file.readAll());
    qApp->setStyleSheet(styleSheet);
}

//配合在QT设计师界面单独设置一些控件的styleSheet

样式表示例

QWidget{
    /*边框:宽度 实线 灰色*/
    border:1px solid gray;
    /*正常形状的控件一般都是方形,可以四个边分开设置*/
    border-top:1px solid gray;
    border-left:1px solid gray;
    border-bottom:1px solid gray;
    border-right:1px solid gray;
    /*设置边框四个角的弧度*/
    border-radius:10px
    /*设置控件上字体颜色*/
    color:rgb(59,90,130);
    /*设置背景色*/
    background:#80898E
    /*也可以把背景设置成渐变色,4个设置颜色的地方多试试*/
    background:qlineargradient(x1:0, y1:0,x2:0,y2:1,
    stop:0 rgb(176,192,222),stop:0.4 rgb(119,136,153),stop:0.4 rgb(119,136,153),stop:1.0 rgb(112,128,144));
}

QPushButton{
    /*正常状态下*/
    border-style:inset;
    border-width:2px;
    border-color:rgb(100,149,237);
    /*QWidget的属性都可以引用*/
}
QPushButton:hover{
    /*鼠标在按钮上时*/
    border-style:none;}
QPushButton:pressed{
    /*鼠标按下时*/
    border-style:outset;}

QTableView{
    /*对QTableWidget和QTableView都有效果*/
    background:#BFEFFF;
    border:1px solid #80898E;
    gridline-color:#709DCA;
}
QHeaderView::section{
    /*表头*/
    background:#5CACEE;
}
QTableCornerButton::section{
    /*横表头和竖表头都存在时的交点*/
    background:#709DCA;
}

/*待更新*/

你可能感兴趣的:(QT笔记)