相关博文
1. 代码
QToolBar * toolbar = new QToolBar(this);
QAction * btn1 = new QAction("btn1");
btn1->setIcon(QIcon(":/images/btn1.png"));
value->setCheckable(true); //按钮按下弹起
toolbar ->addAction(btn1);
QAction * btn2 = new QAction("btn2");
btn2->setIcon(QIcon(":/images/btn2.png"));
value->setCheckable(true);
toolbar ->addAction(btn2);
//设置属性
toolbar->setFloatable(false);
toolbar->setMovable(false);
toolbar->setStyleSheet("border:none");
toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
//利用布局添加toolbar
ui->horizontalLayout1->addWidget(toolbar);
ui->horizontalLayout_5->setContentsMargins(0,0,0,0); //控件和widget之间的边距
ui->horizontalLayout_5->setSpacing(10); //控件和控件之间的间隔
//创建弹簧
QSpacerItem *spaceItem = new QSpacerItem(0,50,QSizePolicy::Expanding,QSizePolicy::Fixed);
spaceItem->setAlignment(Qt::AlignRight);
ui->horizontalLayout1->addSpacerItem(spaceItem);
ui->horizontalLayout1->setSpacing(0);
//功能链接
connect(btn1 , &QAction::triggered, this, [=](bool checked){
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::red);
ui->label->setAutoFillBackground(true);
ui->label->setPalette(palette);
ui->funcbtn1->setChecked(checked); //依据上述checked决定按钮funcbtn1的选中状态
});
2. QAction图标间隔
toolbar->layout()->setContentsMargins(10,10,10,10); //设置周围间隔
toolbar->layout()->setSpacing(20); //设置部件之间的间隔