Lesson 54th Multi-page switch component in Qt

Source: Internet
Author: User

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

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.