1. Multi-page Switch component (Qtabwidget)
(1) The ability to freely switch the contents of different pages in the same window
(2) is a container type of component, while providing a friendly page switching method
2. How to use qtabwidget
(1) Creating an Qtabwidget object in the application
(2) Add other Qwidget objects to the object . However, the Qtabwidget object can only join one Qwidget object at a time, and it will generate a new page .
(3) Solution to add multiple components to the same Qtabwidget page
① Creating a container-type Component Object
② to lay out multiple subcomponents in a container Object
③ Adding a container object to the Qtabwidget to generate a new page
(4) Basic usage of qtabwidget components
//set the position and size of the Qtabwidget object in the parent componentM_tabwidget.setparent ( This); M_tabwidget.move (Ten,Ten); M_tabwidget.resize ( $, $);//To Create a sub-componentqpushbutton* BTN =NewQpushbutton (&m_tabwidget); BTN->settext ("You ' re welcome!");//adding a Qtabwidget object to create a new pageM_tabwidget.addtab (BTN,"LST");
3. Advanced usage of qtabwidget components
(1) Frequently used advanced usage
① Setting the Tab tab location (North, South, West, East)
② setting tab appearance (rounded, triangular)
③ to set tab- closed mode
(2) Pre-defined signals in the Qtabwidget assembly
①void currentchange (int index): The currently displayed page is changed, and index is the new page subscript.
②void tabcloserequest (int index): the Close button on the index page is clicked to make a close request.
"Thinking" how to enable a text editor project to support multiple document operations (implemented with Qtabwidget components)
Preliminary discussion and advanced usage of "programming experiment" qtabwidget component
Main.cpp
" Widget.h " <QApplication>int main (intChar *argv[]) { qapplication a (argc, ARGV); Widget W; W.show (); return a.exec ();}
Widget.h
#ifndef Widget_h#defineWidget_h#include<QWidget>#include<QTabWidget>classWidgets: Publicqwidget{q_object qtabwidget m_tabwidget;protectedSlots:voidOntabcurrentchanged (intindex); voidOntabcloserequested (intindex); Public: Widgets (Qwidget*parent =0); ~Widget ();};#endif //Widget_h
Widget.cpp
#include"Widget.h"#include<QPlainTextEdit>#include<QLabel>#include<QPushButton>#include<QVBoxLayout>#include<QDebug>Widget::widget (Qwidget*parent): Qwidget (parent) {m_tabwidget.setparent ( This); M_tabwidget.move (Ten,Ten); M_tabwidget.resize ( $, $); M_tabwidget.settabposition (Qtabwidget::south); //the location of the labelM_tabwidget.settabshape (Qtabwidget::triangular);//the appearance of the labelM_tabwidget.settabsclosable (true);//to close the labelQplaintextedit* edit =NewQplaintextedit (&m_tabwidget); Edit->insertplaintext ("LST Tab Page"); M_tabwidget.addtab (Edit,"1st"); Qwidget* Widget =NewQwidget (&m_tabwidget); Qvboxlayout* Layout =Newqvboxlayout (); Qlabel* LBL =NewQlabel (widgets); Qpushbutton* BTN =NewQpushbutton (widgets); LBL->settext ("2nd Tab Page"); LBL-setalignment (Qt::aligncenter); BTN->settext ("2nd Tab Page"); Layout-AddWidget (LBL); Layout-AddWidget (BTN); Widgets-setlayout (layout); M_tabwidget.addtab (Widgets,"2nd"); M_tabwidget.setcurrentindex (1);//set the 2nd tab as the current pageConnect (&m_tabwidget, SIGNAL (currentchanged (int)), This, SLOT (Ontabcurrentchanged (int))); Connect (&m_tabwidget, SIGNAL (tabcloserequested (int)), This, SLOT (ontabcloserequested (int)));}voidWidget::ontabcurrentchanged (intindex) {Qdebug ()<<"Page Change to:"<<index;}voidWidget::ontabcloserequested (intindex) {M_tabwidget.removetab (index);} Widget::~Widget () {}
4. Summary
(1) A powerful multi-page component in the QT platform
(2) Qtabwidget components can only be added to one component at a time
(3) When adding multiple components through the container component and layout manager
(4) Qtabwidget can customize the Appearance and location of page labels
(5) Qtabwidget pre-defined signal to achieve advanced functions in the program
Lesson 54th Multi-page switch component in Qt