Qt QToolBar 添加 换行 添加到底部 左侧 右侧

常用添加:

Qt QToolBar 添加 换行 添加到底部 左侧 右侧_第1张图片

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
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;
}

添加到底部

addToolBar(Qt::BottomToolBarArea, toolBar);

Qt QToolBar 添加 换行 添加到底部 左侧 右侧_第2张图片

 

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);
}

太多需要分行:

addToolBarBreak(Qt::BottomToolBarArea);

Qt QToolBar 添加 换行 添加到底部 左侧 右侧_第3张图片

 

    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);

添加分隔符

toolBar->addSeparator();

你可能感兴趣的:(QT,qt,QToolBar)