当前位置:网站首页>Common forms in QT

Common forms in QT

2022-06-21 22:55:00 Clown's stage

Qwidget

1. brief introduction :

QWidget Class is the base class for all user interface objects . It receives the mouse from the window system , Keyboard and other events , And draw itself on the screen .

Simultaneous inheritance :QObject class and QPaintDevice class

  • QWidget: Class is the base class for all user interface objects .

  • QObject: It's all Qt Class base class . Provides a signal - Slot mechanism .

  • QPaintDevice: have access to QPainter The base class of the drawn object .

2. Constructors

 QWidget::QWidget(
    QWidget *parent = Q_NULLPTR,
    Qt::WindowFlags f = Qt::WindowFlags()
 );

parent: If it is nullptr, The widget will become a main window , Otherwise the widget will become parent When a child window in deletes its parent window , Will delete his widget .

f: Bitwise marked by any window “ or ” Value of composition .Qt::Window Flags() Is to construct a template class object. The default is Qt::Widget

3. Member functions

Moving functions :

void move(const QPoint &) void move(int x, int y)

// Set the color ( Commonly used with color fill sets )

void setPalette(const QPalette &)

// Color fill

void setAutoFillBackground(bool enabled)

// Set the font

void setFont(const QFont &);

// Set mouse tracking Events

void setMouseTracking(bool enable);

 // The widget 
 ​
     QWidget *widget=new QWidget(nullptr,Qt::Window);
     // Set the window title :
     widget->setWindowTitle(QStringLiteral(" This is because QWidget forms "));
     //widget->setToolTip(QStringLiteral(" This is a hint "));
     // Set the icon of the window 
     widget->setWindowIcon(QIcon("F:\\qtItem\\oftenForms\\image\\11.jpg"));
     // Set the state of the window :
     widget->setWindowState(Qt::WindowNoState);// The normal state 
     //widget->setWindowState(Qt::WindowMinimized);// Stay small 
     //widget->setWindowState(Qt::WindowMaximized);// Maximum state 
     //widget->setWindowState(Qt::WindowFullScreen);// Full screen , There are no buttons to maximize or minimize ;
     //widget->setWindowState(Qt::WindowActive);// Active window , I don't know what's the use 
     // Set window transparent reading 
     widget->setWindowOpacity(0.7);
     // Hide form :
    // widget->hide();
     // Closing Windows 
     //widget->close();
     // Set window fixed size 
     //widget->setFixedSize(400,400);
     // Set the cursor 
     widget->setCursor(Qt::CrossCursor);
     // Display window 
     widget->show();

4. attribute :

  1. enabled Enable or disable widget, Enabled by default .

  2. geometry widget Location and size of

  3. 3.sizePolicy Set up widget Horizontal and vertical scaling strategies and scaling factors , The so-called scaling strategy is actually widget treat sizeHint The strategy of ,

  4. palette: palette

    Value attribute Meaning
    Fixed Think sizeHint Value is optimal ,widget The size cannot be changed
    Minimum Think sizeHint The value is the minimum , The size can be increased , It can't be smaller
    Maximum Think sizeHint Value is maximum , The size can be reduced , Can't get bigger
    Preferred Think sizeHint Is the appropriate value , The size can be changed , But it is not recommended to get bigger
    Expanding Think sizeHint Is the appropriate value , The size can be changed , Than Preferred Suitable for getting bigger
    MinimumExpanding Think sizeHint Is minimum , The size can be increased , It can't be smaller
    Ignored Ignore sizeHint size , The size can be changed

QFrame( Form with border )

brief introduction :

1.QFrame And QWidget The difference between :

QFrame Is the base class of the basic control ,QWidget yes QFrame Parent class .

QFrame stay QWidget Provides control of the form border based on .

Function :

It mainly controls some border styles , for example : Bulge , Concave , shadow , Line width ;

attribute :

  1. frameRect : QRect : Draw the rectangle of the frame . By default, the entire widget . When the widget changes size , The frame rectangle adjusts automatically .

  2. frameShadow : Shadow: Frame style frame shadow .

  3. frameShape : Shape : Frame shape of frame style .

  4. frameWidth : const int : The width determined by the style frame . for example ,NoFrame The frame width of the specified style is always 0, and Panel The specified style frame width is equal to lineWidth.

  5. lineWidth : int : The width of the frame .

    A frame used as a separator (HLine and VLine) Of lineWidth from frameWidth Appoint .

  6. midLineWidth : int : The width of an extra line in the middle of the frame , It uses a third color to get a special 3D effect . Bulge only (raised) Or sunken (sunken) Of Box、HLine、VLine Draw the centerline of the frame .

type :

1、enum QFrame::Shadow: This enumeration type definition is used to provide 3D The shadow type of the effect .

Plain: The frame and content are flush with the surroundings . Use the palette QPalette::WindowText Color painting ( There is no 3D effect ) ​ Raised: The frame and content are raised ; Draw using the light and dark colors of the current color group 3D Raised line ​ Sunken: The frame and content are sunken ; Draw using the light and dark colors of the current color group 3D Sag line 2、enum QFrame::Shape: This enumeration type defines the shapes of the available frames .

NoFrame: Draw nothing ​ Box: Draw a box around its contents ​ Panel: Draw a panel , To make the contents appear convex or concave ​ StyledPanel: Draw a rectangular panel , Its appearance depends on the current GUI style . It can be convex or concave ​ HLine: Draw a horizontal line , No frame ( Use as separator ) ​ VLine: Draw a vertical line , No box ( Use as separator ) ​ WinPanel: Draw a rectangular panel , It can look like Windows 2000 Raised or sunken like a panel in . Specifies that this shape sets the lineweight to 2 Pixels . Provide WinPanel For compatibility . about GUI Style independence , It is suggested to use it instead StyledPanel

application

     // First step : Create a QFrame object 
     QFrame frame;
     //frame.setStyleSheet()
     // Set border width 
     frame.setLineWidth(10);
     // Set the center line 
     frame.setMidLineWidth(5);
     // Set shape 
     frame.setFrameShape(QFrame::Box);
     // Set the background color of the window 
     //frame.setStyleSheet("background-color:red");
     // Set shadow 
     frame.setFrameShadow(QFrame::Raised);
     // Set border position   Draw the rectangular size of the frame ;
     frame.setFrameRect(QRect(10,0,400,400));
     // Get the width of the border 
     qDebug()<< frame.lineWidth();
     frame.show();

QDialog( Dialog box )

brief introduction :

Inherited from Qwidget, Is a container type component ;

When QWidget As a separate window when there is no parent component , When there is a parent component , Embed as a visible part into the parent component .

QDialog Cannot be embedded as a child in another container ;

QGroupBox( Group box )

QGroupBox Provides support for building group boxes . Group boxes usually have a border and a title bar , Used as a container part , Various window components can be arranged in it . Can be used as a container for a set of controls when laying out , But here's the thing , Layout controls must be used internally ( Such as QBoxLayout) Progressive cloth game .

attribute :

  1. alignment: Qt::Alignment

    Alignment of group box titles . The default is Qt::AlignLeft.

    • Qt::AlignLeft: Left alignment .

    • Qt::AlignRight: right justify .

    • Qt::AlignHCenter: align horizontal center .

  2. checkable : bool :

    Whether there is a check box in the title .

    If this check box is selected , Then enable the sub items of the group box ; otherwise , They will be disabled and inaccessible .

  3. checked : bool Whether the check box is selected .

  4. flat : bool

    Is it flattened .

    A group box usually consists of a surrounding frame with a title at the top . If this property is enabled , Then most styles only draw the top of the frame ; otherwise , The entire frame will be drawn .

    Be careful : In some styles , A flat group box and a non flat group box have similar representations , And may not be as distinguishable as in other styles .

  5. title : QString

    Title Text .

    If the title contains and symbols ('&') Followed by a letter , The group box title text will have keyboard shortcuts .

     g->setTitle("&User information");

    In the example above ,Alt+U Move keyboard focus to group box .

usage

  // First step :
     QHBoxLayout *box1=new QHBoxLayout(this);
     QLabel *label1=new QLabel(QStringLiteral(" male "));
     QLabel *label2=new QLabel(QStringLiteral(" Woman "));
     QCheckBox *check1=new QCheckBox;
     QCheckBox *check2=new QCheckBox;
     QHBoxLayout *box2=new QHBoxLayout;
     box2->addWidget(check1);
     box2->addWidget(label1);
     box2->addWidget(check2);
     box2->addWidget(label2);
     QGroupBox *group=new QGroupBox(QStringLiteral(" Gender "));
     group->setCheckable(true);// Sets whether the check box displays 
     group->setChecked(false);// if true, Then the group box can use , Otherwise, the group box is unavailable 
     group->setAlignment(Qt::AlignHCenter);// Set the location of the information 
     group->setLayout(box2);
     box1->addWidget(group);
 ​

QScrollArea( Scroll area )

brief introduction :

QScrollArea Used as a container part , Provide a scrollable area form . When internal controls cannot be fully displayed ,QScrollArea You can scroll up and down or left and right to display . Be careful : Use QScrollArea It's usually through setWidget Method to add a QFrame object .

attribute :

usage :

  // Create a child window , This sub window is given to the scrolling area 
     QWidget* pSubWidget = new QWidget();
 ​
     // At the window (100,100) Position display 150*150 Size scrolling area 
     QScrollArea* pScrollArea = new QScrollArea(this);
     //pScrollArea->setFixedSize(350, 150);
     // Set whether to automatically resize the part . The default is false.
     //pScrollArea->setWidgetResizable(true);
     // Format alignment 
     pScrollArea->setAlignment(Qt::AlignHCenter);
     pScrollArea->move(QPoint(100, 100));
     // Set the vertical scrolling policy 
     pScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
     // Settings settings QScrollArea( Scroll area ) Sizing strategy for 
     pScrollArea-> setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
 ​
     // Set a vertical layout object for the child window , Add buttons dynamically , The child window will automatically resize 
     QVBoxLayout* pSubLayout = new QVBoxLayout(this);
     pSubWidget->setLayout(pSubLayout);
     for (auto i = 0; i < 10; ++i)
     {
         pSubLayout->addWidget(new QPushButton(QString("test%1").arg(i), this));
     }
     pScrollArea->setWidget(pSubWidget);

QToolBox hold-all

brief introduction :

QToolBox Class provides a list of tabbed widget items . Inherited from Qframe;

A toolbox is a widget , It displays a list of tabs one by one , The current item is displayed below the current tab . Each tab There is an index position in each tab column . The tab item is a QWidget.

attribute :

count : const int This property holds the number of items contained in the toolbox .

currentIndex : int This property holds the index of the current item

Method :

// Add items to the toolbox , You can specify the text content and icon content of the item

int addItem(QWidget *widget, const QIcon &iconSet, const QString &text)

int addItem(QWidget *w, const QString &text)

// In the toolbox index Insert item at , You can specify the text content and icon content of the item int insertItem(int index, QWidget *widget, const QIcon &icon, const QString &text) int insertItem(int index, QWidget *widget, const QString &text)

// Sets whether items are available according to the index void setItemEnabled(int index, bool enabled)

// Set the icon of the item according to the index void setItemIcon(int index, const QIcon &icon)

// Set the text of the item according to the index void setItemText(int index, const QString &text)

// Set the prompt content of the item according to the index void setItemToolTip(int index, const QString &toolTip)

QTabWidget( Label form )

QTabWidget It is mainly used for pagination display , One interface per page , Many interfaces share a common area , Save interface size , very It is convenient to display more information for users .

Common methods

 // Add a page at the end of the label form , And set the form of the page , Tag name and tag icon 
 int addTab(QWidget *page, const QString &label)
 int addTab(QWidget *page, const QIcon &icon, const QString &label)
 // In the label form index Position inserts a page , And set the form of the page , Tag name and tag icon 
 int insertTab(int index, QWidget *page, const QString &label)
 int insertTab(int index, QWidget *page, const QIcon &icon, const QString &label)
 // Set whether the label can be moved 
 void setMovable(bool movable)
 // Set the display position of the label 
 void setTabPosition(TabPosition)
 // Set whether the label is available 
 void setTabEnabled(int index, bool enable)
 // Set whether the label can be closed 
 void setTabsClosable(bool closeable)
 // Set the current label index or form 
 void setCurrentIndex(int index)
 void setCurrentWidget(QWidget *widget)

Enumeration type :QTabWidget::TabPosition

name value explain
QTabWidget::North0 To the north
QTabWidget::South1 Southward direction
QTabWidget::West2 Westward
QTabWidget::East3 To the East

QListWidget List form

QListWidget( List form ) Is an item based (item) List form of . The parent class is QListView, Is based on Model( model type ) List view of . QListWidget( List form ) Contains a series of commonly used list form operation interfaces . It can meet the requirements of the general list seek . Can pass QListWidgetItem Set items .

Method list

 // Add items to the list form , You can specify the name of the item , The object of the item . Or add multiple items at once 
 void addItem(const QString &label)
 void addItem(QListWidgetItem *item)
 void addItems(const QStringList &labels)
 // Insert item 
 void insertItem(int row, QListWidgetItem *item)
 void insertItem(int row, const QString &label)
 void insertItems(int row, const QStringList &labels)
 // obtain row The item pointer of the line 
 QListWidgetItem *item(int row) const
 // Find items in the list according to their contents and related rules 
 QList<QListWidgetItem *> findItems(const QString &text, Qt::MatchFlags flags) 
 const
 // Set the currently selected row or item 
 void setCurrentItem(QListWidgetItem *item)
 void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags 
 command)
 void setCurrentRow(int row)
 void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)
 // Set whether items are sorted when they are added , No sort by default , Display in the order of addition . Set to true Display in ascending order . 
 void setSortingEnabled(bool enable)
 // Sort all items . The default is ascending . 
 void sortItems(Qt::SortOrder order = Qt::AscendingOrder)
 // The setting item is editable ,
 void closePersistentEditor(QListWidgetItem *item)
 // Close the editable state of the item 
 void openPersistentEditor(QListWidgetItem *item)
 // clear list 
 void clear()
 // The signal 
 // Send this signal when the selected item changes 
 void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
 // This signal is sent when the current line changes 
 void currentRowChanged(int currentRow)
 // Send this signal when the current item changes , The parameter is the text of the current item 
 void currentTextChanged(const QString &currentText)
 // When item This signal is sent when it is active .( Double click the mouse or click )
 void itemActivated(QListWidgetItem *item)
 // When item This signal is sent when the content of changes 
 void itemChanged(QListWidgetItem *item)
 // When the mouse clicks item Send the signal when 
 void itemClicked(QListWidgetItem *item)
 // When the mouse double clicks item Send the signal when 
 void itemDoubleClicked(QListWidgetItem *item)
 // When the mouse enters item Send the signal when , You have to set setMouseTracking by true
 void itemEntered(QListWidgetItem *item)
 // When the mouse goes away item Send the signal when , You have to set setMouseTracking by true
 void itemPressed(QListWidgetItem *item)
 // This signal is sent when the selected middle item changes 
 void itemSelectionChanged()

QStackedWidget Stack forms

Stack form (QStackedWidget) Can contain multiple forms , But only one form is displayed . You can set the cable To set the contents of the displayed form . Inherited from 、QFrame;

attribute

count : const int currentIndex : int

function

int addWidget(QWidget *widget): int count() const int currentIndex() const QWidget *currentWidget() const int indexOf(QWidget *widget) const int insertWidget(int index, QWidget *widget) void removeWidget(QWidget *widget) QWidget *widget(int index) const

QTableWidget( Table form )

QToolBar

attribute

allowedAreas : Qt::ToolBarAreas Area where tools can be placed

floatable:bool: This property saves whether the toolbar can be dragged and dropped as a separate window . The default is true

floating : const bool This property saves whether the toolbar is a separate window . The default is true

iconSize : QSize

movable : bool This attribute holds whether users can move toolbars within the toolbar area , Or move toolbars between toolbar areas

orientation : Qt::Orientation Define the orientation of the toolbar

toolButtonStyle : Qt::ToolButtonStyle

function

QT Standard dialog box for

QT Provides some basic dialog types :

  1. Standard file dialog (QFileDialog)

  2. Standard color dialog box (QColorDialog)

  3. Standard font dialog box (QFontDialog)

  4. Standard input dialog box (QInputDialog)

  5. Standard message dialog box (QMessageBox)

原网站

版权声明
本文为[Clown's stage]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212054022257.html