In Qt, it is convenient to add toolbars to the main window (Qmainwindow Class), using Addtoolbar directly, as shown below:
Filetoolbar = Addtoolbar (tr ("&file")); Filetoolbar-Addaction ( filenewaction); Filetoolbar->addaction (fileopenaction);
However, Addtoolbar is a function of the Qmainwindow class, and there is no addtoolbar similar function for adding toolbars in the Qwidget class, so you cannot add toolbars directly to the Qwidget class (the Qwidget subclass).
Can I add a toolbar to the Qwidget class window? Of course. Considering that the toolbar Qtoolbar is also qwidget subclass (class Q_gui_export qtoolbar:public qwidget), simply add the toolbar as normal Qwidget to the window. In general, the toolbar is at the top of the window, so you can take the layout and put the toolbar on top.
My example is to create a dockable component box that displays the corresponding toolbar above the component box, as follows:
First, the code excerpt
//2.1 ToolbarsCompsboxtoolbar =NewQtoolbar (TR ("Components"), dockcomptools); Compsboxtoolbar->seticonsize (Qsize ( -, -)); Compsboxtoolbar-addaction (compsnewaction); Compsboxtoolbar-addaction (compsopenaction); Compsboxtoolbar-addaction (compssaveaction);//2.2 Toolbox BodyListcomp =Newxcomplistwidget (); ..//2.3 Text Prompt boxqlabel* LBL =NewQlabel ( This); LBL->settext (TR ("Component Description:")); Txttipcomp=NewQtextedit (); ..//2.4 LayoutQvboxlayout *VBOXLAYOUT1 =Newqvboxlayout (); VBOXLAYOUT1->addwidget (Compsboxtoolbar);//Add the toolbar as a widget to the layoutVboxlayout1->AddWidget (Listcomp); VBOXLAYOUT1-AddWidget (LBL); VBOXLAYOUT1-AddWidget (txttipcomp);//2.5 Application Layoutqwidget* dockwidgetcontents =NewQwidget (); Dockwidgetcontents->setlayout (VBOXLAYOUT1);
Second, the results show
Qt adds a toolbar to Qwidget