Qt adds a toolbar to Qwidget

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.