Qt 程序界面改样式 qss

Qt程序界面修改,用到qss (qt style sheet),记录遇到的一些问题。以后对样式的修改也可以加在这里。

1、QTableWidget 

表格各行不同色

// 代码里
tableWidget->setAlternatingRowColors(true); // 支持隔行不同色
tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // 列宽平均
tableWidget->verticalHeader()->setDefaultSectionSize(80); // 设置默认行高
tableWidget->verticalHeader()->setHidden(true); // 垂直序号隐藏

// qss 文件
QTableWidget
{
    alternate-background-color: rgba(248, 250, 252, 255);
    border: 1px solid rgba(212, 212, 212, 255);
}
QTableWidget::item::selected
{
    background-color: transparent;
    color : rgba(46, 101, 210, 255);
}

// QTableWidget的表头
QHeaderView::section {
    padding-left: 2px;
    margin: 0px;
    background-color: rgba(233,239,245,255);
    height: 50px;
    font-weight: bold;
}

表格部分可编辑部分不可编辑:整体可编辑,某个单元格设置不可编辑

tableWidget->item(i, 1)->setFlags(Qt::ItemIsEnabled);

2、QTreeWidget

选中子项选中一整行。

// qss 文件里
QTreeView{
    outline:0px; /*选中时没有虚线,没有轮廓*/
    border:none;
    show-decoration-selected: 1;
}
QTreeView::item {
    height: 30px;
}
QTreeView::item:selected:active{
    background-color: rgba(205, 232, 255, 255);
}
QTreeView::branch:selected:active{
    background-color: rgba(205, 232, 255, 255);
}
QTreeView::item:hover, QTreeView::branch:hover {
    background-color: rgba(229, 243, 255, 255);
}
QTreeView::item:selected:!active{
    background-color: rgba(205, 232, 255, 255);
}
QTreeView::branch:selected:!active{
    background-color: rgba(205, 232, 255, 255); /*brance是树子项前面缺的那一块*/
}

3、QDockWidget

浮动框标题粗体

QDockWidget{
    font:bold;
}

没有关闭、浮动等按钮

setFeatures(QDockWidget::DockWidgetMovable);

4、QComboBox

下拉框嵌在表格里了,表格隔行不同色,下拉框底色透明,随表格颜色。

QComboBox{
    border: none;
    background: transparent;
}

QComboBox::drop-down{
    border: none;  /*箭头所在的下拉按钮*/
    width: 20px;
}
QComboBox::down-arrow{ 
    width: 20px;
    height: 20px;
    image: url(:/Resources/array_down.png); /*下拉按钮箭头*/
}

/* 下拉后,整个下拉窗体每项的样式 */
QComboBox QAbstractItemView::item {
    height: 50px;   /* 项的高度(设置pComboBox->setView(new QListView());后,该项才起作用) */
}

5、QRadioButton 表格中单选框居中(具体数值根据自己的界面设置,通过padding居中)

QRadioButton * box = new QRadioButton;
box->setStyleSheet("border:1px solid red;padding:0px 45px 0px;");
ui.tableWidget->setCellWidget(i, 0, box);

 

posted @ 2022-10-26 17:47  yangly  阅读(602)  评论(0编辑  收藏  举报