Qt工作总结02 <设置工具栏ToolBar>
相关博文
1. 代码
QToolBar * toolbar = new QToolBar(this);QAction * btn1 = new QAction("btn1");
btn1->setIcon(QIcon(":/images/btn1.png"));
toolbar ->addAction(btn1);QAction * btn2 = new QAction("btn2");
btn2->setIcon(QIcon(":/images/btn2.png"));
toolbar ->addAction(btn2);//设置属性
toolbar->setFloatable(false);
toolbar->setMovable(false);
toolbar->setStyleSheet("border:none");
toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);//利用布局添加toolbar
ui->horizontalLayout1->addWidget(toolbar);
ui->horizontalLayout1->setMargin(5);//创建弹簧
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, [this]{QPalette palette;palette.setColor(QPalette::WindowText, Qt::red);ui->label->setAutoFillBackground(true);ui->label->setPalette(palette);
});
2. QAction图标间隔
toolbar->layout()->setContentsMargins(10,10,10,10); //设置周围间隔
toolbar->layout()->setSpacing(20); //设置部件之间的间隔