QComboBox, QFrame, QLabel

QComboBox

1. Description

QComboBox provides a way to present a list of options to the user in a way that takes up the least amount of screen real estate.

QComboBox uses a model/view framework for its popup list and stores its items. By default, QStandardItemModel stores items and QListView subclasses display popup lists. The model and view can be accessed directly (using model() and view()), but QComboBox also provides functions for setting and getting item data (for example, setItemData() and itemText()). New models and views can also be set (using setModel() and setView()). For the text and icon in the combobox label, use the data from the model with Qt::DisplayRole and Qt::DecorationRole. Note that the SelectionMode of view() cannot be changed.

2. Type members

1. enum QComboBox::InsertPolicy: This enumeration specifies what QComboBox should do when the user enters a new string.

  • NoInsert: The string will not be inserted into the combo box.
  • InsertAtTop: The string will be inserted as the first item in the combo box.
  • InsertAtCurrent: The current item will be replaced by the string.
  • InsertAtBottom: The string will be inserted after the last item in the combo box.
  • InsertAfterCurrent: Inserts a string after the current item in the combo box.
  • InsertBeforeCurrent: Inserts a string before the current item in the combo box.
  • InsertAlphabetically: Strings are inserted into the combo box in alphabetical order.

2. enum QComboBox::SizeAdjustPolicy: This enumeration specifies how the size prompt of the QComboBox should be adjusted when new content is added or the content changes.

  • AdjustToContents: The combobox will always adjust to the content
  • AdjustToContentsOnFirstShow: The combo box will adjust its contents the first time it is shown.
  • AdjustToMinimumContentsLengthWithIcon: The combobox will adjust to minimumContentsLength plus the space for the icon. For performance reasons, this strategy should be used on large models.

Three, attribute members

1. [read-only] count: const int

The number of items in the combo box.

2. [read-only] currentData : const QVariant

Data for the current project.

3. currentIndex: int

The index of the current item in the combo box. The current index can change when items are inserted or removed.

4. currentText: QString

current text. If the combo box is editable, the current text is the value displayed by line editing.

If the combobox is editable, setCurrentText() will only call setEditText(). Otherwise, if there is a matching text in the list, currentIndex is set to the corresponding index.

5. duplicatesEnabled: bool

When it is editable, set setSizeAdjustPolicy() to insert the input content into the combo box, and whether to insert duplicate items in the combo box. The default is false.

Note that it is always possible to programmatically insert duplicates into a combobox.

6. editable: bool

Whether the combobox can be edited. The default is false. The effect of editing depends on the insertion strategy.

Note: validators and completers are removed when the editable state is disabled.

7. frame: bool

slightly.

8. iconSize: QSize

The size of the icon displayed in the combo box. This is the maximum size an icon can have, icons of smaller sizes will not be enlarged.

9. insertPolicy: InsertPolicy

Policy for where inserted items should appear in the combo box when editable.

The default is InsertAtBottom, i.e. the new item will appear at the bottom of the item list.

10. maxCount: int

The maximum number of items allowed in the combo box.

If the maximum number is set to be less than the number of items currently in the combo box, the extra items will be truncated. This also works if an external model is set on the combobox.

11. maxVisibleItems: int

The maximum number of items allowed on the combo box screen. The default is 10.

This property is ignored for non-editable combo boxes in styles for which QStyle::SH_ComboBox_Popup returns true.

12. minimumContentsLength: int

The minimum number of characters that should fit in the combo box. The default value is 0.

If this property is set to a positive value, minimumSizeHint() and sizeHint() will take this property into account.

13. modelColumn: int

The columns visible in the model. Default is 0.

14. placeholderText: QString

Placeholder text text to display when no valid index is set.

When an invalid index is set a placeholder text will be displayed and the text in the dropdown list will not be accessible. When this function is called before an item is added, the placeholder text will be displayed, otherwise you must call setCurrentIndex(-1) programmatically if you want to display the placeholder text.

When the QComboBox is editable, use lineEdit()->setPlaceholderText() instead.

15. sizeAdjustPolicy: SizeAdjustPolicy

Strategy for how the size of the combo box changes when the content changes. The default is AdjustToContentsOnFirstShow.

4. Member functions

1. QComboBox(QWidget *parent = nullptr)

Constructs a combo box with the given parent using the default model QStandardItemModel .

2. [Signal] void activated(int index)

This signal is sent when an item in the combo box is selected. Note that this signal is sent even if the selection has not changed.

[Signal] void textActivated(const QString & amp;text)

This signal is sent when an item in the combo box is selected. Note that this signal is sent even if the selection has not changed.

3. void clear()

Clear the combo box, removing all items.

If an external model is set on the combo box, it will still be cleared when this function is called.

4. void clearEditText()

Clears the contents of the row edited for editing in the combo box. Only makes sense when editable.

5. [Signal] void currentIndexChanged(int index)

This signal is sent whenever the currentIndex in the combobox is changed, either through user interaction or programmatically. The index is -1 if the combobox becomes empty.

6. [Signal] void currentTextChanged(const QString & amp;text)

This signal is sent whenever currentText changes. The new value is passed as text.

7. [Signal] void editTextChanged(const QString & amp;text)

This signal is emitted when the text in the combobox’s row edit widget has changed.

8. [Signal] void highlighted(int index)

This signal is sent when an item in the combobox’s popup list is highlighted.

9. void setEditText(const QString & amp;text)

Set the text in the text edit of the combo box. Only makes sense when editable.

10. [Signal] void textHighlighted(const QString & amp;text)

This signal is sent when an item in the combobox’s popup list is highlighted.

11. void addItem(const QString & amp; text, const QVariant & amp; userData = QVariant())

Adds an item to the combobox with the given text and contains the specified userData (stored in Qt::UserRole). The item will be appended to the list of existing items.

void addItem(const QIcon & amp;icon, const QString & amp;text, const QVariant & amp;userData = QVariant())

Adds an item to the combobox with the given icon and text, and contains the specified userData (stored in Qt::UserRole). The item will be appended to the list of existing items.

void addItems(const QStringList & texts)

Adds each string in the given text list to the combo box. Each item in turn is appended to the list of existing items.

void insertItem(int index, const QString & amp;text, const QVariant & amp;userData = QVariant())
void insertItem(int index, const QIcon & amp; icon, const QString & amp; text, const QVariant & amp; userData = QVariant())
void insertItems(int index, const QStringList & amp;list)

Inserts text and user data (stored in Qt::UserRole) into the combo box at the given index.

  • If the index is equal to or greater than the total number of items, the new item will be appended to the list of existing items.
  • If the index is zero or negative, the new item will be inserted at the front of the list of existing items.

QVariant itemData(int index, int role = Qt::UserRole)

Returns the data for the given role at the given index in the combo box, or an invalid QVariant if there is no data for this role.

void removeItem(int index)

Removes the item at the given index from the combo box. If the index is out of bounds, this function will do nothing.

12. void insertSeparator(int index)

Inserts a separator item into the combo box at the given index.

  • If the index is equal to or greater than the total number of items, the new item will be appended to the list of existing items.
  • If the index is zero or negative, the new item is added to the front of the list of existing items.

13. void setCompleter(QCompleter *completer) / QCompleter *completer()

Set the completer to use. If nullptr, auto-completion is disabled.

By default, for editable combo boxes, a QCompleter that performs case-insensitive inline completion is automatically created.

Completers are removed when the editable property becomes false, or when line editing is replaced by a call to setLineEdit(). Setting a completer on a non-editable QComboBox will be ignored.

14. int findData(const QVariant & data, int role = Qt::UserRole, Qt::MatchFlags flags = static_cast(Qt::MatchExactly|Qt::MatchCaseSensitive))

Returns the index of the item containing the given data for the given role, otherwise returns -1.

flags specifies how to search for items in the combo box. Qt::MatchFlags see QML control type: ComboBox

int findText(const QString & amp;text, Qt::MatchFlags flags = Qt::MatchExactly|Qt::MatchCaseSensitive)

Returns the index of the item containing the given text, otherwise returns -1.

15. [virtual] void hidePopup()

Hides the list of items in the combo box.

[virtual] void showPopup()

Displays a list of items in the combo box.

If you reimplement this function to display a custom popup, be sure to call hidePopup() to reset the internal state.

16. void initStyleOption(QStyleOptionComboBox *option)

Initializes the options with the values in this QComboBox. This method is useful when subclasses need a QStyleOptionComboBox, but don’t want to fill in all the information themselves.

17. QVariant itemData(int index, int role = Qt::UserRole)

void setItemData(int index, const QVariant & amp;value, int role = Qt::UserRole)

Returns/sets the data for the given role at the given index in the combo box, or returns an invalid QVariant if there is no data for this role.

18. void setItemDelegate(QAbstractItemDelegate *delegate)

Sets the item delegate for the popup list view. The combobox gets delegated ownership.

Any existing delegation will be deleted, but not deleted, and ownership of the existing delegation passes to the user.

19. QIcon itemIcon(int index) / void setItemIcon(int index, const QIcon & amp;icon)

Returns/sets the icon for the given index.

20. QString itemText(int index) / void setItemText(int index, const QString & amp;text)

Sets/returns the text of the item at the given index.

21. QLineEdit * lineEdit()

Returns the line editor used to edit the items in the combo box, only editable combo boxes have a line editor.

void setLineEdit(QLineEdit *edit)

Set the line editor to use. The combobox takes ownership of the row editor.

Since the combo box’s line editor owns a QCompleter, any previous calls to setCompleter() will no longer have effect.

22. void setModel(QAbstractItemModel *model)

Set up the model. model cannot be nullptr. If you want to clear the contents of the model, call clear().

If the combobox is editable, then the model will also be set on the row edit’s completer.

23. void setRootModelIndex(const QModelIndex & amp;index)

Sets the root model item index for the items in the combo box.

24. void setView(QAbstractItemView *itemView)

Sets the view used in the combo box popup. The combobox takes ownership of the itemView.

If you want to use a convenience view (such as QListWidget, QTableWidget, or QTreeWidget), make sure to call setModel() on the combo box with the convenience widget model before calling this function.

Learn C + + Qt development tutorial videos for free, click the link below to sign up for free video learning materials

https://ke.qq.com/course/444655?flowToken=1044418

QFrame

1. Description

The QFrame class can also be used directly to create simple placeholder frames without any content.

Frame styles are specified by frame shape and shadow styles, and are used to visually separate the frame from surrounding widgets.

The margins between the frame and the frame contents can be customized using the QWidget::setContentsMargins() function.

Some combinations of styles and lineweights:

2. Type members

1. enum QFrame::Shadow: This enumeration type defines the shadow type used to provide 3D effects for the frame.

  • Plain: Frame and content are flush with surroundings. Draw with palette QPalette::WindowText colors (without any 3D effects)
  • Raised: Frame and content appear raised; 3D raised lines are drawn using the light and dark colors of the current color group
  • Sunken: Sunken frame and content; draws 3D sunken lines using the light and dark colors of the current color group

2. enum QFrame::Shape: This enumeration type defines the shape of the available frame.

  • NoFrame: draw nothing
  • Box: draws a box around its content
  • Panel: draws a panel, making the content appear raised or sunken
  • StyledPanel: Draws a rectangular panel whose appearance depends on the current GUI style. it can be raised or recessed
  • HLine: Draws a horizontal line without a frame (used as a separator)
  • VLine: draws a vertical line without a box (used as a separator)
  • WinPanel: Draws a rectangular panel that can be raised or sunken like a panel in Windows 2000. Specifying this shape sets the lineweight to 2 pixels. WinPanel is provided for compatibility. For GUI style independence, it is recommended to use StyledPanel instead

3. enum QFrame::StyleMask: This enumeration defines two constants that can be used to extract the two components of frameStyle():

  • Shadow_Mask: the shadow part of frameStyle()
  • Shape_Mask: the shape part of frameStyle()

Three, attribute members

1. frameRect: QRect

Draw the rectangle for the frame. The default is the entire widget. The frame rectangle is automatically adjusted when the widget changes size.

2. frameShadow: Shadow

Frame shadow for frame style.

3. frameShape: Shape

The frame shape for the frame style.

4. frameWidth: const int

The width determined by the style frame. For example, styles specified by NoFrame always have a frame width of 0, while styles specified by Panel have a frame width equal to lineWidth.

5. lineWidth: int

The width of the frame.

The lineWidth of the frame (HLine and VLine) used as separator is specified by frameWidth.

6. midLineWidth: int

There is an extra line’s width in the middle of the frame, which uses a third color for a special 3D effect. Only draws the median line for raised or sunken Box, HLine, VLine frames.

4. Member functions

1. QFrame(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())

Constructs a frame widget with frame style NoFrame and frameWidth = 1.

2. void setFrameStyle(int style)

Set the frame style. The frame shape is given in QFrame::Shape and the shadow style is given in QFrame::Shadow.

QLabel

1. Description

QLabel inherits from QFrame. QLabel is used to display text or images. No user interaction is provided.

2. Attribute members

1. alignment: Qt::Alignment

The alignment of the content. By default the content is left aligned and vertically centered.

2. [read-only] hasSelectedText: const bool

Whether any text is selected.

3. indent: int

Holds the label’s text indent in pixels. Defaults to -1.

When the label shows text:

  • If alignment() is Qt::AlignLeft, indentation is applied to the left edge
  • If alignment() is Qt::AlignRight, indentation is applied to the right edge
  • If alignment() is Qt::AlignTop, indentation is applied to the top edge
  • to the bottom edge if alignment() is Qt::AlignBottom
  • If this value is less than or equal to 0, the effective indentation of the label is calculated as follows:
  • If frameWidth() is 0, the effective indent becomes 0
  • If frameWidth() is greater than 0, the effective indent becomes half the width of ‘x’ characters of the widget’s current font().

4. margin: int

The width of the margin. That is, the distance between the innermost pixel of the frame and the outermost pixel of the content. Default is 0.

5. openExternalLinks: bool

Specifies whether the QLabel should automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated() signal. The default value is false.

6. pixmap: QPixmap

The pixmap for the label. Setting the pixmap clears any previous content.

7. scaledContents: bool

Whether the label will scale its content to fill all available space. The default value is false.

When enabled and the label displays a pixmap, it will scale the pixmap to fill the available space.

8. [read-only] selectedText: const QString

selected text. If no text is selected, the value of this property is an empty string.

9. text: QString

The text of the label. Setting text clears any previous content.

Text will be interpreted as plain text or rich text, depending on the text formatting settings. The default setting is Qt::AutoText, i.e. the QLabel will try to automatically detect the format of the text set.

QLabel is ideal for displaying small rich-text documents, for example where document-specific settings (font, text color, link color) are obtained from the label’s palette and font properties. For large rich text documents, use QTextEdit in read-only mode. QTextEdit can also provide scrollbars when necessary.

This feature will enable mouse tracking if the text contains rich text.

10. textFormat: Qt::TextFormat

The text format of the label. The default format is Qt::AutoText.

  • Qt::PlainText: Text strings are interpreted as plain text strings.
  • Qt::RichText: Text strings are interpreted as rich text strings.
  • Qt::AutoText: Text strings are interpreted as Qt::RichText if Qt::mightBeRichText() returns true, otherwise as Qt::PlainText.
  • Qt::MarkdownText: Text strings are interpreted as Markdown formatted text.

Related:

bool Qt::mightBeRichText(const QString & amp;text)

Returns whether a string literal is likely to be rich text.

QString Qt::convertFromPlainText(const QString & amp;plain, Qt::WhiteSpaceMode mode = WhiteSpacePre)

Converts plain text strings to HTML-formatted paragraphs while preserving most of their appearance. mode defines how whitespace is handled.

11. textInteractionFlags: Qt::TextInteractionFlags

How the label should interact with user input when displaying text. The default is Qt::LinksAccessibleByMouse.

Qt::TextInteractionFlags see Qt Graphics View Framework: QGraphicsTextItem

  • If the flags include Qt::LinksAccessibleByKeyboard, the focus policy is also automatically set to Qt::StrongFocus.
  • If the flags include Qt::TextSelectableByKeyboard, the focus policy is set to Qt::ClickFocus.

12. wordWrap: bool

Wrap policy for tags, defaults to false. If true, the label text will wrap at necessary word breaks.

3. Member functions

1. void clear()

Clear any label content.

2. [Signal] void linkActivated(const QString & amp; link)

This signal is emitted when the user clicks on a link.

3. [Signal] void linkHovered(const QString & amp;link)

This signal is emitted when the user hovers over a link.

4. void setMovie(QMovie *movie)

Set the label content to QMovie. Any previous content will be cleared. The tag does not take ownership of the movie.

5. void setNum(double num)

void setNum(int num)

Set label content to plain text containing num text representations. Any previous content will be cleared.

6. void setPicture(const QPicture & picture)

Set the label content to an image. Any previous content will be cleared.

7. int selectionStart()

Returns the index of the first selected character in the label, or -1 if no text is selected.

8. void setBuddy(QWidget *buddy)

Sets this tag’s partner widget.

When the user presses the shortcut key indicated by this label, the keyboard focus will be transferred to the label’s friend widget.

The buddy mechanism only works for QLabels containing text, where a character is prefixed with an ampersand ” & amp; “. This character is set as a shortcut key. (To display the actual ampersand, use “& amp; & amp;”).

QLineEdit *nameEdit = new QLineEdit(this);
QLabel *nameLabel = new QLabel(" & amp;Name:", this);
nameLabel->setBuddy(nameEdit);
QLineEdit *phoneEdit = new QLineEdit(this);
QLabel *phoneLabel = new QLabel(" & amp;Phone:", this);
phoneLabel->setBuddy(phoneEdit);

Using the code above, when the user presses Alt + N the focus jumps to the Name field and when the user presses Alt + P the focus jumps to the Phone field.

To unset a previously set buddy widget, call setBuddy(nullptr).

9. void setSelection(int start, int length)

Selects length characters of text starting at position start.

Learn C + + Qt development tutorial videos for free, click the link below to sign up for free video learning materials

https://ke.qq.com/course/444655?flowToken=1044418

Original link: https://blog.csdn.net/kenfan1647/article/details/122276450