1. 常用添加:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->setMovable(false);
QStringList toolBarlist;
toolBarlist << "aa" << "bb" << "cc";
foreach(QString name, toolBarlist)
{
//QString tmp = "&" + name + "..." ;
QAction * action = new QAction(name, this);
action->setStatusTip(name);
connect(action, SIGNAL(triggered()) , this, SLOT(getButtonText()));
toolBar->addAction(action);
}
}
void MainWindow::getButtonText()
{
QAction *button = (QAction *)(sender()); //关键一步是利用sender()
qDebug() << "button text:" << button->text();
if (0 == button->text().compare("aa"))
{
// option
}
}
MainWindow::~MainWindow()
{
delete ui;
}
2. 添加到底部
addToolBar(Qt::BottomToolBarArea, toolBar);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->setMovable(false);
QStringList toolBarlist;
toolBarlist << "aaaaaaa" << "bbbbbbbbbb" << "ccccccccccc";
foreach(QString name, toolBarlist)
{
//QString tmp = "&" + name + "..." ;
QAction * action = new QAction(name, this);
action->setStatusTip(name);
connect(action, SIGNAL(triggered()) , this, SLOT(getButtonText()));
toolBar->addAction(action);
}
addToolBar(Qt::BottomToolBarArea, toolBar);
}
3.太多需要分行:
addToolBarBreak(Qt::BottomToolBarArea);
QToolBar *toolBar = addToolBar(tr("&File"));
QStringList toolBarlist;
toolBarlist << "BooleanOperation" << "InteractorStyle" << "showSliderWidget"
<< "ShowPolygonWidget" << "SetPlaneWidgetAngle" << "RotatePlaneWidgetByLine" << "startCut" << "vtkOBJExporter"
<< "addLine" << "SelectPartMove" <<"CombineData"<<"SmoothPolyData"<<"SaveTransform";
foreach(QString name, toolBarlist)
{
//QString tmp = "&" + name + "..." ;
QAction * action = new QAction(name, this);
action->setStatusTip(name);
connect(action, SIGNAL(triggered()) , this, SLOT(getButtonText()));
toolBar->addAction(action);
}
// 分成二行;
QToolBar *toolBarSec = addToolBar(tr("&Tools"));
QStringList toolBarlistSec;
toolBarlistSec << "SelectPointMove" << "AddCylinderWidget" << "SetCylinderWidgetMode" << "AddAxesTool";
foreach(QString name, toolBarlistSec)
{
//QString tmp = "&" + name + "..." ;
QAction * action = new QAction(name, this);
action->setStatusTip(name);
connect(action, SIGNAL(triggered()), this, SLOT(getButtonText()));
toolBarSec->addAction(action);
}
addToolBar(Qt::BottomToolBarArea, toolBar);
addToolBarBreak(Qt::BottomToolBarArea);
addToolBar(Qt::BottomToolBarArea, toolBarSec);
4. 添加分隔符
toolBar->addSeparator();
code:
https://download.csdn.net/download/q610098308/88071388