How to provide headers for each section of a toolbar in Qt

I need to create a custom toolbar just like below. I used the following lines of code for this:

m_ptrFileToolBar = new QToolBar("File",theUi->toolBar);
m_ptrNewAct = new QAction(QIcon(":/res/images/New.png"), tr("&New"), this);
m_ptrNewAct->setShortcuts(QKeySequence::New);
m_ptrNewAct->setStatusTip(tr("Create a new file"));
m_ptrFileToolBar->addAction(m_ptrNewAct);

      

Only the toolbar action is shown. But I need to give the titles of each section of the toolbar. How to do it in Qt

?

enter image description here

+3


source to share


1 answer


You have subclass QWidgetAction and reimplement QWidgetAction :: createWidget ()

QWidget *myWidgetAction::createWidget(QWidget *parent)
{
   QWidget *myWidget = new QWidget(parent);
   QAction *act1 = new QAction(QIcon(":/1.png"),tr("New"),myWidget);
   QAction *act2 = new QAction(QIcon(":/2.png"),tr("Open"),myWidget);
   QAction *act3 = new QAction(QIcon(":/3.png"),tr("Create"),myWidget);
   QToolBar *toolbar = new QToolBar(myWidget);
   toolbar->addAction(act1);
   toolbar->addAction(act2);
   toolbar->addAction(act3);
   QLabel *title = new QLabel("test");
   QGridLayout *grid = new QGridLayout(myWidget);
   myWidget->setLayout(grid);
   grid->addWidget(title,0,0,1,0,Qt::AlignCenter);
   grid->addWidget(toolbar,1,0,1,0,Qt::AlignCenter);
   return myWidget;
}

      



Then you can add the object myWidgetAction

to the toolbar

m_ptrFileToolBar = new QToolBar("File",theUi->toolBar);
myWidgetAction *widgetAction = new myWidgetAction(this);
m_ptrFileToolBar->addAction(widgetAction);

      

+2


source







All Articles