Урок №11. виджеты в qt5

Содержание

Member Function Documentation

Constructs a status bar with a size grip and the given parent.


See also ().

Removes any temporary message being shown.

See also (), (), and ().

void QStatusBar::messageChanged(const QString &message)

This signal is emitted whenever the temporary status message changes. The new temporary message is passed in the message parameter which is a null-string when the message has been removed.

See also () and ().

void QStatusBar::showMessage(const QString &message, int timeout = 0)

Hides the normal status indications and displays the given message for the specified number of milli-seconds (timeout). If timeout is 0 (default), the message remains displayed until the () slot is called or until the showMessage() slot is called again to change the message.

Note that showMessage() is called to show temporary explanations of tool tip texts, so passing a timeout of 0 is not sufficient to display a .

See also (), (), and ().

void QStatusBar::addPermanentWidget( *widget, int stretch = 0)

Adds the given widget permanently to this status bar, reparenting the widget if it isn’t already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space.

Permanently means that the widget may not be obscured by temporary messages. It is is located at the far right of the status bar.

See also (), (), and ().

void QStatusBar::addWidget( *widget, int stretch = 0)

Adds the given widget to this status bar, reparenting the widget if it isn’t already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space.

The widget is located to the far left of the first permanent widget (see ()) and may be obscured by temporary messages.

See also (), (), and ().

Returns the temporary message currently shown, or an empty string if there is no such message.

See also ().

Ensures that the right widgets are visible.

Used by the () and () functions.

int QStatusBar::insertPermanentWidget(int index, *widget, int stretch = 0)

Inserts the given widget at the given index permanently to this status bar, reparenting the widget if it isn’t already a child of this QStatusBar object. If index is out of range, the widget is appended (in which case it is the actual index of the widget that is returned).

The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space.

Permanently means that the widget may not be obscured by temporary messages. It is is located at the far right of the status bar.

This function was introduced in Qt 4.2.

See also (), (), and ().

int QStatusBar::insertWidget(int index, *widget, int stretch = 0)

Inserts the given widget at the given index to this status bar, reparenting the widget if it isn’t already a child of this QStatusBar object. If index is out of range, the widget is appended (in which case it is the actual index of the widget that is returned).

The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space.

The widget is located to the far left of the first permanent widget (see ()) and may be obscured by temporary messages.

This function was introduced in Qt 4.2.

See also (), (), and ().

Reimplements: (QPaintEvent *event).

Shows the temporary message, if appropriate, in response to the paint event.

void QStatusBar::reformat()

Changes the status bar’s appearance to account for item changes.

Special subclasses may need this function, but geometry management will usually take care of any necessary rearrangements.

void QStatusBar::removeWidget( *widget)

Removes the specified widget from the status bar.

Note: This function does not delete the widget but hides it. To add the widget again, you must call both the () and () functions.

See also (), (), and ().

Detailed Description

A QTableView implements a table view that displays items from a model. This class is used to provide standard tables that were previously provided by the QTable class, but using the more flexible approach provided by Qt’s model/view architecture.

The QTableView class is one of the and is part of Qt’s model/view framework.

QTableView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class.

Navigation

You can navigate the cells in the table by clicking on a cell with the mouse, or by using the arrow keys. Because QTableView enables by default, you can also hit Tab and Backtab to move from cell to cell.

Visual Appearance

The table has a vertical header that can be obtained using the () function, and a horizontal header that is available through the () function. The height of each row in the table can be found by using (); similarly, the width of columns can be found using (). Since both of these are plain widgets, you can hide either of them using their () functions.

Rows and columns can be hidden and shown with (), (), (), and (). They can be selected with () and (). The table will show a grid depending on the property.

The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the function, and later retrieved with .

By default, the cells in a table do not expand to fill the available space.

You can make the cells fill the available space by stretching the last header section. Access the relevant header using () or () and set the header’s property.

To distribute the available space according to the space requirement of each column or row, call the view’s () or () functions.

Detailed Description

QStackedLayout can be used to create a user interface similar to the one provided by QTabWidget. There is also a convenience QStackedWidget class built on top of QStackedLayout.

A QStackedLayout can be populated with a number of child widgets («pages»). For example:

    QWidget *firstPageWidget = new QWidget;
    QWidget *secondPageWidget = new QWidget;
    QWidget *thirdPageWidget = new QWidget;

     *stackedLayout = new ;
    stackedLayout->addWidget(firstPageWidget);
    stackedLayout->addWidget(secondPageWidget);
    stackedLayout->addWidget(thirdPageWidget);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(stackedLayout);
    setLayout(mainLayout);

QStackedLayout provides no intrinsic means for the user to switch page. This is typically done through a QComboBox or a QListWidget that stores the titles of the QStackedLayout’s pages. For example:

    QComboBox *pageComboBox = new QComboBox;
    pageComboBox->addItem(tr("Page 1"));
    pageComboBox->addItem(tr("Page 2"));
    pageComboBox->addItem(tr("Page 3"));
    connect(pageComboBox, QOverload<int>::of(&QComboBox::activated),
            stackedLayout, &::setCurrentIndex);

When populating a layout, the widgets are added to an internal list. The () function returns the index of a widget in that list. The widgets can either be added to the end of the list using the () function, or inserted at a given index using the () function. The () function removes the widget at the given index from the layout. The number of widgets contained in the layout, can be obtained using the () function.

The () function returns the widget at a given index position. The index of the widget that is shown on screen is given by () and can be changed using (). In a similar manner, the currently shown widget can be retrieved using the () function, and altered using the () function.

Member Function Documentation

QStackedLayout::QStackedLayout(QWidget *parent)

Constructs a new QStackedLayout with the given parent.

This layout will install itself on the parent widget and manage the geometry of its children.

QStackedLayout::QStackedLayout()

Constructs a QStackedLayout with no parent.

This QStackedLayout must be installed on a widget later on to become effective.

See also () and ().

void QStackedLayout::currentChanged(int index)

This signal is emitted whenever the current widget in the layout changes. The index specifies the index of the new current widget, or -1 if there isn’t a new one (for example, if there are no widgets in the QStackedLayout)

Note: Notifier signal for property .

See also () and ().

void QStackedLayout::setCurrentWidget(QWidget *widget)

Sets the current widget to be the specified widget. The new current widget must already be contained in this stacked layout.

See also () and ().

void QStackedLayout::widgetRemoved(int index)


This signal is emitted whenever a widget is removed from the layout. The widget’s index is passed as parameter.

See also ().

int QStackedLayout::addWidget(QWidget *widget)

Adds the given widget to the end of this layout and returns the index position of the widget.

If the QStackedLayout is empty before this function is called, the given widget becomes the current widget.

See also (), (), and ().

Returns the current widget, or if there are no widgets in this layout.

See also () and ().

int QStackedLayout::insertWidget(int index, QWidget *widget)

Inserts the given widget at the given index in this QStackedLayout. If index is out of range, the widget is appended (in which case it is the actual index of the widget that is returned).

If the QStackedLayout is empty before this function is called, the given widget becomes the current widget.

Inserting a new widget at an index less than or equal to the current index will increment the current index, but keep the current widget.

See also (), (), and ().

Property Documentation

This property holds the number of widgets contained in the layout

Access functions:

virtual int

See also () and ().

currentIndex : int

This property holds the index position of the widget that is visible

The current index is -1 if there is no current widget.

Access functions:

int
void

Notifier signal:

void

See also () and ().

stackingMode :

determines the way visibility of child widgets are handled.

The default value is . Setting the property to allows you to make use of the layout for overlay widgets that do additional drawing on top of other widgets, for example, graphical editors.

This property was introduced in Qt 4.4.

Access functions:

Reimplemented Protected Functions

virtual void
virtual void
virtual bool
virtual void

Detailed Description

Toolbar buttons are added by adding actions, using () or (). Groups of buttons can be separated using () or (). If a toolbar button is not appropriate, a widget can be inserted instead using () or (). Examples of suitable widgets are QSpinBox, QDoubleSpinBox, and QComboBox. When a toolbar button is pressed, it emits the () signal.

A toolbar can be fixed in place in a particular area (e.g., at the top of the window), or it can be movable between toolbar areas; see (), (), () and ().

When a toolbar is resized in such a way that it is too small to show all the items it contains, an extension button will appear as the last item in the toolbar. Pressing the extension button will pop up a menu containing the items that do not currently fit in the toolbar.

When a QToolBar is not a child of a QMainWindow, it loses the ability to populate the extension pop up with widgets added to the toolbar using (). Please use widget actions created by inheriting QWidgetAction and implementing () instead.

See also QToolButton, QMenu, QAction, and Application Example.

allowedAreas :

areas where the toolbar may be placed

The default is .

This property only makes sense if the toolbar is in a QMainWindow.

Access functions:

Qt::ToolBarAreas
void

Notifier signal:

void

See also .

floatable : bool

This property holds whether the toolbar can be dragged and dropped as an independent window.

The default is true.

Access functions:

bool
void

floating : const bool

This property holds whether the toolbar is an independent window.

By default, this property is .

Access functions:

bool

See also ().

iconSize : QSize

size of icons in the toolbar.

The default size is determined by the application’s style and is derived from the pixel metric. It is the maximum size an icon can have. Icons of smaller size will not be scaled up.

Access functions:

QSize
void

Notifier signal:

void

movable : bool

This property holds whether the user can move the toolbar within the toolbar area, or between toolbar areas.

By default, this property is .

This property only makes sense if the toolbar is in a QMainWindow.

Access functions:

bool
void

Notifier signal:

void

See also .

orientation :

orientation of the toolbar

The default is .

This function should not be used when the toolbar is managed by QMainWindow. You can use () or () if you wish to move a toolbar that is already added to a main window to another .

Access functions:

Qt::Orientation
void

Notifier signal:

void

toolButtonStyle :

This property holds the style of toolbar buttons

This property defines the style of all tool buttons that are added as QActions. Note that if you add a QToolButton with the () method, it will not get this button style.

To have the style of toolbuttons follow the system settings, set this property to . On Unix, the user settings from the desktop environment will be used. On other platforms, means icon only.

The default is .


Access functions:

Qt::ToolButtonStyle
void

Notifier signal:

void

Protected Slots

void
void
void
void
void
void

Detailed Description

A QTableView implements a table view that displays items from a model. This class is used to provide standard tables that were previously provided by the QTable class, but using the more flexible approach provided by Qt’s model/view architecture.

The QTableView class is one of the and is part of Qt’s model/view framework.

QTableView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class.

Navigation

You can navigate the cells in the table by clicking on a cell with the mouse, or by using the arrow keys. Because QTableView enables by default, you can also hit Tab and Backtab to move from cell to cell.

Visual Appearance

The table has a vertical header that can be obtained using the () function, and a horizontal header that is available through the () function. The height of each row in the table can be found by using (); similarly, the width of columns can be found using (). Since both of these are plain widgets, you can hide either of them using their () functions.

Rows and columns can be hidden and shown with (), (), (), and (). They can be selected with () and (). The table will show a grid depending on the property.

The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the function, and later retrieved with .

By default, the cells in a table do not expand to fill the available space.

You can make the cells fill the available space by stretching the last header section. Access the relevant header using () or () and set the header’s property.

To distribute the available space according to the space requirement of each column or row, call the view’s () or () functions.

Coordinate Systems

For some specialized forms of tables it is useful to be able to convert between row and column indexes and widget coordinates. The () function provides the y-coordinate within the view of the specified row; the row index can be used to obtain a corresponding y-coordinate with (). The () and () functions provide the equivalent conversion operations between x-coordinates and column indexes.

See also QTableWidget, , QAbstractItemModel, QAbstractItemView, Chart Example, Pixelator Example, and Table Model Example.

Property Documentation

cornerButtonEnabled : bool

This property holds whether the button in the top-left corner is enabled

If this property is then button in the top-left corner of the table view is enabled. Clicking on this button will select all the cells in the table view.

This property is by default.

This property was introduced in Qt 4.3.

Access functions:

bool
void

gridStyle :

This property holds the pen style used to draw the grid.

This property holds the style used when drawing the grid (see ).

Access functions:

Qt::PenStyle
void

showGrid : bool

This property holds whether the grid is shown

If this property is a grid is drawn for the table; if the property is , no grid is drawn. The default value is true.

Access functions:

bool
void

sortingEnabled : bool

This property holds whether sorting is enabled

If this property is , sorting is enabled for the table. If this property is , sorting is not enabled. The default value is false.

Note: . Setting the property to true with () immediately triggers a call to () with the current sort section and order.

This property was introduced in Qt 4.2.

Access functions:

bool
void

See also ().

wordWrap : bool

This property holds the item text word-wrapping policy

If this property is then the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property is by default.

Note that even of wrapping is enabled, the cell will not be expanded to fit all text. Ellipsis will be inserted according to the current .

This property was introduced in Qt 4.3.

Access functions:

bool
void

Property Documentation

count : const int

This property holds the number of tabs in the tab bar

By default, this property contains a value of 0.

Access functions:

int

currentIndex : int

This property holds the index position of the current tab page

The current index is -1 if there is no current widget.

By default, this property contains a value of -1 because there are initially no tabs in the widget.

Access functions:

int
void

Notifier signal:

void

documentMode : bool

This property holds whether or not the tab widget is rendered in a mode suitable for document pages. This is the same as document mode on macOS.

When this property is set the tab widget frame is not rendered. This mode is useful for showing document-type pages where the page covers most of the tab widget area.

This property was introduced in Qt 4.5.

Access functions:

bool
void

See also , , , and .

elideMode :

how to elide text in the tab bar

This property controls how items are elided when there is not enough space to show them for a given tab bar size.

By default the value is style dependant.

This property was introduced in Qt 4.2.

Access functions:

Qt::TextElideMode
void

See also , , and .

iconSize : QSize

This property holds the size for icons in the tab bar

The default value is style-dependent. This is the maximum size that the icons will have. Icons are not scaled up if they are of smaller size.

This property was introduced in Qt 4.2.

Access functions:

QSize
void

See also .

movable : bool

This property holds whether the user can move the tabs within the tabbar area.


By default, this property is ;

This property was introduced in Qt 4.5.

Access functions:

bool
void

tabBarAutoHide : bool

If true, the tab bar is automatically hidden when it contains less than 2 tabs.

By default, this property is false.

This property was introduced in Qt 5.4.

Access functions:

bool
void

See also .

tabPosition :

This property holds the position of the tabs in this tab widget

Possible values for this property are described by the enum.

By default, this property is set to .

Access functions:

QTabWidget::TabPosition
void

See also .

tabShape :

This property holds the shape of the tabs in this tab widget

Possible values for this property are (default) or .

Access functions:

QTabWidget::TabShape
void

See also .

tabsClosable : bool

This property holds whether close buttons are automatically added to each tab.

This property was introduced in Qt 4.5.

Access functions:

bool
void

See also ().

usesScrollButtons : bool

This property holds whether or not a tab bar should use buttons to scroll tabs when it has many tabs.

When there are too many tabs in a tab bar for its size, the tab bar can either choose to expand its size or to add buttons that allow you to scroll through the tabs.

By default the value is style dependant.

This property was introduced in Qt 4.2.

Access functions:

bool
void

Виджет QCheckBox

QCheckBox — это виджет чекбокса (англ. «checkbox»), состоящий из ячейки и подписи к ней. имеет 2 состояния: включено или выключено. При включенном состоянии, внутри ячейки отображается флажок (галочка или крестик), при выключенном — ничего.

В следующем примере мы выведем в окне виджет чекбокса. Если у чекбокса установлен флажок, то будет выводиться заголовок окна, в противном случае заголовок окна будет скрыт.

Заголовочный файл — checkbox.h:

#pragma once

#include <QWidget>

class CheckBox : public QWidget { Q_OBJECT

public: CheckBox(QWidget *parent = 0);

private slots: void showTitle(int); };

1 2 3 4 5 6 7 8 9 10 11 12 13 14

#pragma once   #include <QWidget>  

classCheckBoxpublicQWidget{

Q_OBJECT

public

CheckBox(QWidget*parent=);

privateslots

voidshowTitle(int);

};

Выводим чекбокс в окне и подключаем его к слоту showTitle().

Файл реализации — checkbox.cpp:

#include <QCheckBox> #include <QHBoxLayout> #include «checkbox.h»

CheckBox::CheckBox(QWidget *parent) : QWidget(parent) {

QHBoxLayout *hbox = new QHBoxLayout(this); QCheckBox *cb = new QCheckBox(«Show Title», this); cb->setCheckState(Qt::Checked); hbox->addWidget(cb, 0, Qt::AlignLeft | Qt::AlignTop);

connect(cb, &QCheckBox::stateChanged, this, &CheckBox::showTitle); }

void CheckBox::showTitle(int state) { if (state == Qt::Checked) { setWindowTitle(«QCheckBox»); } else { setWindowTitle(» «); } }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#include <QCheckBox> #include <QHBoxLayout> #include «checkbox.h»  

CheckBox::CheckBox(QWidget*parent)

QWidget(parent){

QHBoxLayout*hbox=newQHBoxLayout(this);

QCheckBox*cb=newQCheckBox(«Show Title»,this);

cb->setCheckState(Qt::Checked);

hbox->addWidget(cb,,Qt::AlignLeft|Qt::AlignTop);

connect(cb,&QCheckBox::stateChanged,this,&CheckBox::showTitle);

}  

voidCheckBox::showTitle(intstate){

if(state==Qt::Checked){

setWindowTitle(«QCheckBox»);

}else{

setWindowTitle(» «);

}

}

Флажок устанавливается при запуске примера:

cb->setCheckState(Qt::Checked);

1 cb->setCheckState(Qt::Checked);

Определяем состояние флажка и вызываем метод setWindowTitle():

void CheckBox::showTitle(int state) {

if (state == Qt::Checked) { setWindowTitle(«QCheckBox»); } else { setWindowTitle(» «); } }

1 2 3 4 5 6 7 8

voidCheckBox::showTitle(intstate){

if(state==Qt::Checked){

setWindowTitle(«QCheckBox»);

}else{

setWindowTitle(» «);

}

}

Главный файл программы — main.cpp:

#include <QApplication> #include «checkbox.h»

int main(int argc, char *argv[]) { QApplication app(argc, argv); CheckBox window;

window.resize(250, 150); window.setWindowTitle(«QCheckBox»); window.show();

return app.exec(); }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

#include <QApplication> #include «checkbox.h»  

intmain(intargc,char*argv){

QApplication app(argc,argv);

CheckBox window;

window.resize(250,150);

window.setWindowTitle(«QCheckBox»);

window.show();

returnapp.exec();

}

Результат:


С этим читают