diff options
author | Tor Arne Vestbø <[email protected]> | 2023-06-26 14:28:44 +0200 |
---|---|---|
committer | Tor Arne Vestbø <[email protected]> | 2023-06-28 09:56:40 +0200 |
commit | c3fd8e911ea361df7f1e6fe971f8835d2093db7c (patch) | |
tree | b9d46d533872f4222953491fc93a0c0805d70b52 /examples/widgets | |
parent | 95595d68bee7ef9e58100e4230f71d7f065eacb7 (diff) |
Move icons example to manual test
Pick-to: 6.5 6.6
Change-Id: I08f44448f96a61f780c21d628954879c7b28dce3
Reviewed-by: Volker Hilsheimer <[email protected]>
Diffstat (limited to 'examples/widgets')
28 files changed, 0 insertions, 1784 deletions
diff --git a/examples/widgets/doc/src/icons.qdoc b/examples/widgets/doc/src/icons.qdoc deleted file mode 100644 index 8ec062ea434..00000000000 --- a/examples/widgets/doc/src/icons.qdoc +++ /dev/null @@ -1,805 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example widgets/icons - \title Icons Example - \ingroup examples-widgets - \brief The Icons example shows how QIcon can generate pixmaps reflecting - an icon's state, mode and size. - - These pixmaps are generated from the set of pixmaps made available to the - icon, and are used by Qt widgets to show an icon representing a particular - action. - - \image icons-example.png Screenshot of the Icons example - - Contents: - - \tableofcontents - - \section1 QIcon Overview - - The QIcon class provides scalable icons in different modes and - states. An icon's state and mode are depending on the intended use - of the icon. Qt currently defines four modes: - - \table - \header \li Mode \li Description - \row - \li QIcon::Normal - \li Display the pixmap when the user is not interacting with the - icon, but the functionality represented by the icon is - available. - \row - \li QIcon::Active - \li Display the pixmap when the functionality represented by the - icon is available and the user is interacting with the icon, - for example, moving the mouse over it or clicking it. - \row - \li QIcon::Disabled - \li Display the pixmap when the functionality represented by - the icon is not available. - \row - \li QIcon::Selected - \li Display the pixmap when the icon is selected. - \endtable - - QIcon's states are QIcon::On and QIcon::Off, which will display - the pixmap when the widget is in the respective state. The most - common usage of QIcon's states are when displaying checkable tool - buttons or menu entries (see QAbstractButton::setCheckable() and - QAction::setCheckable()). When a tool button or menu entry is - checked, the QIcon's state is \l{QIcon::}{On}, otherwise it's - \l{QIcon::}{Off}. You can, for example, use the QIcon's states to - display differing pixmaps depending on whether the tool button or - menu entry is checked or not. - - A QIcon can generate smaller, larger, active, disabled, and - selected pixmaps from the set of pixmaps it is given. Such - pixmaps are used by Qt widgets to show an icon representing a - particular action. - - \section1 Overview of the Icons Application - - With the Icons application you get a preview of an icon's - generated pixmaps reflecting its different states, modes and size. - - When an image is loaded into the application, it is converted into - a pixmap and becomes a part of the set of pixmaps available to the - icon. An image can be excluded from this set by checking off the - related checkbox. The application provides a sub directory - containing sets of images explicitly designed to illustrate how Qt - renders an icon in different modes and states. - - The application allows you to manipulate the icon size with some - predefined sizes and a spin box. The predefined sizes are style - dependent, but most of the styles have the same values. Only the - \macos style differs by using 32 pixels instead of 16 pixels - for toolbar buttons. You can navigate between the available styles - using the \uicontrol View menu. - - \image icons-view-menu.png Screenshot of the View menu - - The \uicontrol View menu also provide the option to make the application - guess the icon state and mode from an image's file name. The \uicontrol - File menu provide the options of adding an image and removing all - images. These last options are also available through a context - menu that appears if you press the right mouse button within the - table of image files. In addition, the \uicontrol File menu provide an - \uicontrol Exit option, and the \uicontrol Help menu provide information about - the example and about Qt. - - \image icons_find_normal.png Screenshot of the Find Files - - The screenshot above shows the application with one image file - loaded. The \uicontrol {Guess Image Mode/State} is enabled and the - style is Plastique. - - When QIcon is provided with only one available pixmap, that - pixmap is used for all the states and modes. In this case the - pixmap's icon mode is set to normal, and the generated pixmaps - for the normal and active modes will look the same. But in - disabled and selected mode, Qt will generate a slightly different - pixmap. - - The next screenshot shows the application with an additional file - loaded, providing QIcon with two available pixmaps. Note that the - new image file's mode is set to disabled. When rendering the \uicontrol - Disabled mode pixmaps, Qt will now use the new image. We can see - the difference: The generated disabled pixmap in the first - screenshot is slightly darker than the pixmap with the originally - set disabled mode in the second screenshot. - - \image icons_find_normal_disabled.png Screenshot of the Find Files - - When Qt renders the icon's pixmaps it searches through the set of - available pixmaps following a particular algorithm. The algorithm - is documented in QIcon, but we will describe some particular cases - below. - - \image icons_monkey_active.png Screenshot of the Find Files - - In the screenshot above, we have set \c monkey_on_32x32 to be an - Active/On pixmap and \c monkey_off_64x64 to be Normal/Off. To - render the other six mode/state combinations, QIcon uses the - search algorithm described in the table below: - - \table 100% - \header \li{2,1} Requested Pixmap \li {8,1} Preferred Alternatives (mode/state) - \header \li Mode \li State \li 1 \li 2 \li 3 \li 4 \li 5 \li 6 \li 7 \li 8 - \row \li{1,2} Normal \li Off \li \b N0 \li A0 \li N1 \li A1 \li D0 \li S0 \li D1 \li S1 - \row \li On \li N1 \li \b A1 \li N0 \li A0 \li D1 \li S1 \li D0 \li S0 - \row \li{1,2} Active \li Off \li A0 \li \b N0 \li A1 \li N1 \li D0 \li S0 \li D1 \li S1 - \row \li On \li \b A1 \li N1 \li A0 \li N0 \li D1 \li S1 \li D0 \li S0 - \row \li{1,2} Disabled \li Off \li D0 \li \b {N0'} \li A0' \li D1 \li N1' \li A1' \li S0' \li S1' - \row \li On \li D1 \li N1' \li \b {A1'} \li D0 \li N0' \li A0' \li S1' \li S0' - \row \li{1,2} Selected \li Off \li S0 \li \b {N0''} \li A0'' \li S1 \li N1'' \li A1'' \li D0'' \li D1'' - \row \li On \li S1 \li N1'' \li \b {A1''} \li S0 \li N0'' \li A0'' \li D1'' \li D0'' - \endtable - - In the table, "0" and "1" stand for "Off" and "On", respectively. - Single quotes indicates that QIcon generates a disabled ("grayed - out") version of the pixmap; similarly, double quuote indicate - that QIcon generates a selected ("blued out") version of the - pixmap. - - The alternatives used in the screenshot above are shown in bold. - For example, the Disabled/Off pixmap is derived by graying out - the Normal/Off pixmap (\c monkey_off_64x64). - - In the next screenshots, we loaded the whole set of monkey - images. By checking or unchecking file names from the image list, - we get different results: - - \table - \row - \li \inlineimage icons_monkey.png Screenshot of the Monkey Files - \li \inlineimage icons_monkey_mess.png Screenshot of the Monkey Files - \endtable - - For any given mode/state combination, it is possible to specify - several images at different resolutions. When rendering an - icon, QIcon will automatically pick the most suitable image - and scale it down if necessary. (QIcon never scales up images, - because this rarely looks good.) - - The screenshots below shows what happens when we provide QIcon - with three images (\c qt_extended_16x16.png, \c qt_extended_32x32.png, \c - qt_extended_48x48.png) and try to render the QIcon at various - resolutions: - - \table - \row - \li - \li \inlineimage icons_qt_extended_8x8.png Qt Extended icon at 8 x 8 - \li \inlineimage icons_qt_extended_16x16.png Qt Extended icon at 16 x 16 - \li \inlineimage icons_qt_extended_17x17.png Qt Extended icon at 17 x 17 - \row - \li - \li 8 x 8 - \li \b {16 x 16} - \li 17 x 17 - \row - \li \inlineimage icons_qt_extended_32x32.png Qt Extended icon at 32 x 32 - \li \inlineimage icons_qt_extended_33x33.png Qt Extended icon at 33 x 33 - \li \inlineimage icons_qt_extended_48x48.png Qt Extended icon at 48 x 48 - \li \inlineimage icons_qt_extended_64x64.png Qt Extended icon at 64 x 64 - \row - \li \b {32 x 32} - \li 33 x 33 - \li \b {48 x 48} - \li 64 x 64 - \endtable - - For sizes up to 16 x 16, QIcon uses \c qt_extended_16x16.png and - scales it down if necessary. For sizes between 17 x 17 and 32 x - 32, it uses \c qt_extended_32x32.png. For sizes above 32 x 32, it uses - \c qt_extended_48x48.png. - - \section1 Line-by-Line Walkthrough - - The Icons example consists of four classes: - - \list - \li \c MainWindow inherits QMainWindow and is the main application - window. - \li \c IconPreviewArea is a custom widget that displays all - combinations of states and modes for a given icon. - \li \c IconSizeSpinBox is a subclass of QSpinBox that lets the - user enter icon sizes (e.g., "48 x 48"). - \li \c ImageDelegate is a subclass of QStyledItemDelegate that - provides comboboxes for letting the user set the mode and state - associated with an image. - \endlist - - We will start by reviewing the \c IconPreviewArea class before we - take a look at the \c MainWindow class. Finally, we will review the - \c IconSizeSpinBox and \c ImageDelegate classes. - - \section2 IconPreviewArea Class Definition - - An \c IconPreviewArea widget consists of a group box containing a grid of - QLabel widgets displaying headers and pixmaps. - - \image icons_preview_area.png Screenshot of IconPreviewArea. - - \snippet widgets/icons/iconpreviewarea.h 0 - - The \c IconPreviewArea class inherits QWidget. It displays the - generated pixmaps corresponding to an icon's possible states and - modes at a given size. - - \snippet widgets/icons/iconpreviewarea.cpp 42 - - We would like the table columns to be in the order QIcon::Normal, - QIcon::Active, QIcon::Disabled, QIcon::Selected and the rows in the order - QIcon::Off, QIcon::On, which does not match the enumeration. The above code - provides arrays allowing to map from enumeration value to row/column - (by using QList::indexOf()) and back by using the array index and lists - of the matching strings. Qt's containers can be easily populated by - using C++ 11 initializer lists. - - We need two public functions to set the current icon and the - icon's size. In addition the class has three private functions: We - use the \c createHeaderLabel() and \c createPixmapLabel() - functions when constructing the preview area, and we need the \c - updatePixmapLabels() function to update the preview area when - the icon or the icon's size has changed. - - The \c NumModes and \c NumStates constants reflect \l{QIcon}'s - number of currently defined modes and states. - - \section2 IconPreviewArea Class Implementation - - \snippet widgets/icons/iconpreviewarea.cpp 0 - - In the constructor we create the labels displaying the headers and - the icon's generated pixmaps, and add them to a grid layout. - - When creating the header labels, we make sure the enums \c - NumModes and \c NumStates defined in the \c .h file, correspond - with the number of labels that we create. Then if the enums at - some point are changed, the \c Q_ASSERT() macro will alert that this - part of the \c .cpp file needs to be updated as well. - - If the application is built in debug mode, the \c Q_ASSERT() - macro will expand to - - \code - if (!condition) - qFatal("ASSERT: "condition" in file ..."); - \endcode - - In release mode, the macro simply disappear. The mode can be set - in the application's \c .pro file. One way to do so is to add an - option to \c qmake when building the application: - - \code - qmake "CONFIG += debug" icons.pro - \endcode - - or - - \code - qmake "CONFIG += release" icons.pro - \endcode - - Another approach is to add this line directly to the \c .pro - file. - - \snippet widgets/icons/iconpreviewarea.cpp 1 - \codeline - \snippet widgets/icons/iconpreviewarea.cpp 2 - - The public \c setIcon() and \c setSize() functions change the icon - or the icon size, and make sure that the generated pixmaps are - updated. - - \snippet widgets/icons/iconpreviewarea.cpp 3 - \codeline - \snippet widgets/icons/iconpreviewarea.cpp 4 - - We use the \c createHeaderLabel() and \c createPixmapLabel() - functions to create the preview area's labels displaying the - headers and the icon's generated pixmaps. Both functions return - the QLabel that is created. - - \snippet widgets/icons/iconpreviewarea.cpp 5 - - We use the private \c updatePixmapLabel() function to update the - generated pixmaps displayed in the preview area. - - For each mode, and for each state, we retrieve a pixmap using the - QIcon::pixmap() function, which generates a pixmap corresponding - to the given state, mode and size. We pass the QWindows instance - obtained by calling QWidget::windowHandle() on the top level - widget (QWidget::nativeParentWidget()) in order to retrieve - the pixmap that matches best. - We format a tooltip displaying size, actual size and device pixel - ratio. - - \section2 MainWindow Class Definition - - The \c MainWindow widget consists of three main elements: an - images group box, an icon size group box and a preview area. - - \image icons-example.png Screenshot of the Icons example - - \snippet widgets/icons/mainwindow.h 0 - - The MainWindow class inherits from QMainWindow. We reimplement the - constructor, and declare several private slots: - - \list - \li The \c about() slot simply provides information about the example. - \li The \c changeStyle() slot changes the application's GUI style and - adjust the style dependent size options. - \li The \c changeSize() slot changes the size of the preview area's icon. - \li The \c changeIcon() slot updates the set of pixmaps available to the - icon displayed in the preview area. - \li The \c addSampleImages() slot allows the user to load a new image - from the samples provided into the application. - \li The \c addOtherImages() slot allows the user to load a new image from - the directory obtained by calling - QStandardPaths::standardLocations(QStandardPaths::PicturesLocation). - \li The \c screenChanged() updates the display in the \uicontrol{High DPI} - group box to correctly display the parameters of the current screen - the window is located on. - \endlist - - In addition we declare several private functions to simplify the - constructor. - - \section2 MainWindow Class Implementation - - \snippet widgets/icons/mainwindow.cpp 0 - - In the constructor we first create the main window's central - widget and its child widgets, and put them in a grid layout. Then - we create the menus with their associated entries and actions. - - We set the window title and determine the current style for the - application. We also enable the icon size spin box by clicking the - associated radio button, making the current value of the spin box - the icon's initial size. - - \snippet widgets/icons/mainwindow.cpp 1 - - The \c about() slot displays a message box using the static - QMessageBox::about() function. In this example it displays a - simple box with information about the example. - - The \c about() function looks for a suitable icon in four - locations: It prefers its parent's icon if that exists. If it - doesn't, the function tries the top-level widget containing - parent, and if that fails, it tries the active window. As a last - resort it uses the QMessageBox's Information icon. - - \snippet widgets/icons/mainwindow.cpp 2 - - In the \c changeStyle() slot we first check the slot's - parameter. If it is false we immediately return, otherwise we find - out which style to change to, i.e. which action that triggered the - slot, using the QObject::sender() function. - - This function returns the sender as a QObject pointer. Since we - know that the sender is a QAction object, we can safely cast the - QObject. We could have used a C-style cast or a C++ \c - static_cast(), but as a defensive programming technique we use a - \l qobject_cast(). The advantage is that if the object has the - wrong type, a null pointer is returned. Crashes due to null - pointers are much easier to diagnose than crashes due to unsafe - casts. - - \snippet widgets/icons/mainwindow.cpp 3 - \snippet widgets/icons/mainwindow.cpp 4 - - Once we have the action, we extract the style name using - QAction::data(). Then we create a QStyle object using the static - QStyleFactory::create() function. - - Although we can assume that the style is supported by the - QStyleFactory: To be on the safe side, we use the \c Q_ASSERT() - macro to check if the created style is valid before we use the - QApplication::setStyle() function to set the application's GUI - style to the new style. QApplication will automatically delete - the style object when a new style is set or when the application - exits. - - The predefined icon size options provided in the application are - style dependent, so we need to update the labels in the icon size - group box and in the end call the \c changeSize() slot to update - the icon's size. - - \snippet widgets/icons/mainwindow.cpp 5 - - The \c changeSize() slot sets the size for the preview area's - icon. - - It is invoked by the QButtonGroup whose members are radio buttons for - controlling the icon size. In \c createIconSizeGroupBox(), each button is - assigned a QStyle::PixelMetric value as an id, which is passed as a - parameter to the slot. - - The special value \c OtherSize indicates that the spin box is - enabled. If it is, we extract the extent of the new size from the - box. If it's not, we query the style for the metric. Then we create - a QSize object based on the extent, and use that object to set the - size of the preview area's icon. - - \snippet widgets/icons/mainwindow.cpp 12 - - The function \c addImages() is called by the slot addSampleImages() - passing the samples directory, or by the slot addOtherImages() - passing the directory obtained by querying - QStandardPaths::standardLocations(). - - The first thing we do is to show a file dialog to the user. - We initialize it to show the filters returned by - QImageReader::supportedMimeTypes(). - - For each of the files the file dialog returns, we add a row to the - table widget. The table widget is listing the images the user has - loaded into the application. - - \snippet widgets/icons/mainwindow.cpp 13 - - We retrieve the image name using the QFileInfo::baseName() - function that returns the base name of the file without the path, - and create the first table widget item in the row. - We check if a high resolution version of the image exists (identified by - the suffix \c @2x on the base name) and display that along with the size - in the tooltip. - - We add the file's complete name to the item's data. Since an item can - hold several information pieces, we need to assign the file name a role - that will distinguish it from other data. This role can be Qt::UserRole - or any value above it. - - We also make sure that the item is not editable by removing the - Qt::ItemIsEditable flag. Table items are editable by default. - - \snippet widgets/icons/mainwindow.cpp 15 - - Then we create the second and third items in the row making the - default mode Normal and the default state Off. But if the \uicontrol - {Guess Image Mode/State} option is checked, and the file name - contains "_act", "_dis", or "_sel", the modes are changed to - Active, Disabled, or Selected. And if the file name contains - "_on", the state is changed to On. The sample files in the - example's \c images subdirectory respect this naming convention. - - \snippet widgets/icons/mainwindow.cpp 18 - - In the end we add the items to the associated row, and use the - QTableWidget::openPersistentEditor() function to create - comboboxes for the mode and state columns of the items. - - Due to the connection between the table widget's \l - {QTableWidget::itemChanged()}{itemChanged()} signal and the \c - changeIcon() slot, the new image is automatically converted into a - pixmap and made part of the set of pixmaps available to the icon - in the preview area. So, corresponding to this fact, we need to - make sure that the new image's check box is enabled. - - \snippet widgets/icons/mainwindow.cpp 6 - - The \c changeIcon() slot is called when the user alters the set - of images listed in the QTableWidget, to update the QIcon object - rendered by the \c IconPreviewArea. - - We first create a QIcon object, and then we run through the - QTableWidget, which lists the images the user has loaded into the - application. - - \snippet widgets/icons/mainwindow.cpp 8 - - We also extract the image file's name using the - QTableWidgetItem::data() function. This function takes a - Qt::DataItemRole as an argument to retrieve the right data - (remember that an item can hold several pieces of information) - and returns it as a QVariant. Then we use the - QVariant::toString() function to get the file name as a QString. - - To create a pixmap from the file, we need to first create an - image and then convert this image into a pixmap using - QPixmap::fromImage(). Once we have the final pixmap, we add it, - with its associated mode and state, to the QIcon's set of - available pixmaps. - - \snippet widgets/icons/mainwindow.cpp 11 - - After running through the entire list of images, we change the - icon of the preview area to the one we just created. - - \snippet widgets/icons/mainwindow.cpp 20 - - In the \c removeAllImages() slot, we simply set the table widget's - row count to zero, automatically removing all the images the user - has loaded into the application. Then we update the set of pixmaps - available to the preview area's icon using the \c changeIcon() - slot. - - \image icons_images_groupbox.png Screenshot of the images group box - - The \c createImagesGroupBox() function is implemented to simplify - the constructor. The main purpose of the function is to create a - QTableWidget that will keep track of the images the user has - loaded into the application. - - \snippet widgets/icons/mainwindow.cpp 21 - - First we create a group box that will contain the table widget. - Then we create a QTableWidget and customize it to suit our - purposes. - - We call QAbstractItemView::setSelectionMode() to prevent the user - from selecting items. - - The QAbstractItemView::setItemDelegate() call sets the item - delegate for the table widget. We create a \c ImageDelegate that - we make the item delegate for our view. - - The QStyledItemDelegate class can be used to provide an editor for an item view - class that is subclassed from QAbstractItemView. Using a delegate - for this purpose allows the editing mechanism to be customized and - developed independently from the model and view. - - In this example we derive \c ImageDelegate from QStyledItemDelegate. - QStyledItemDelegate usually provides line editors, while our subclass - \c ImageDelegate, provides comboboxes for the mode and state - fields. - - \snippet widgets/icons/mainwindow.cpp 22 - - Then we customize the QTableWidget's horizontal header, and hide - the vertical header. - - \snippet widgets/icons/mainwindow.cpp 24 - - At the end, we connect the QTableWidget::itemChanged() signal to - the \c changeIcon() slot to ensure that the preview area is in - sync with the image table. - - \image icons_size_groupbox.png Screenshot of the icon size group box - - The \c createIconSizeGroupBox() function is called from the - constructor. It creates the widgets controlling the size of the - preview area's icon. - - \snippet widgets/icons/mainwindow.cpp 26 - - First we create a group box that will contain all the widgets; - then we create the radio buttons and the spin box. We add the - radio buttons to an instance of QButtonGroup, using the value - of the QStyle::PixelMetric they represent as an integer id. - - \snippet widgets/icons/mainwindow.cpp 40 - - We introduce an enumeration constant \c OtherSize to represent - a custom size. - - The spin box is not a regular QSpinBox but an \c IconSizeSpinBox. - The \c IconSizeSpinBox class inherits QSpinBox and reimplements - two functions: QSpinBox::textFromValue() and - QSpinBox::valueFromText(). The \c IconSizeSpinBox is designed to - handle icon sizes, e.g., "32 x 32", instead of plain integer - values. - - \snippet widgets/icons/mainwindow.cpp 27 - - Then we connect all of the radio buttons - \l{QRadioButton::toggled()}{toggled()} signals and the spin box's - \l {QSpinBox::valueChanged()}{valueChanged()} signal to the \c - changeSize() slot to make sure that the size of the preview - area's icon is updated whenever the user changes the icon size. - In the end we put the widgets in a layout that we install on the - group box. - - \snippet widgets/icons/mainwindow.cpp 28 - - In the \c createActions() function we create and customize all the - actions needed to implement the functionality associated with the - menu entries in the application. - - In particular we create the \c styleActionGroup based on the - currently available GUI styles using - QStyleFactory. QStyleFactory::keys() returns a list of valid keys, - typically including "windows" and "fusion". Depending on the platform, - "windowsvista" and "macos" may be available. - - We create one action for each key, and adds the action to the - action group. Also, for each action, we call QAction::setData() - with the style name. We will retrieve it later using - QAction::data(). - - As we go along, we create the \uicontrol File, \uicontrol View and - \uicontrol Help menus and add the actions to them. - - The QMenu class provides a menu widget for use in menu bars, - context menus, and other popup menus. We put each menu in the - application's menu bar, which we retrieve using - QMainWindow::menuBar(). - - \snippet widgets/icons/mainwindow.cpp 30 - - QWidgets have a \l{QWidget::contextMenuPolicy}{contextMenuPolicy} - property that controls how the widget should behave when the user - requests a context menu (e.g., by right-clicking). We set the - QTableWidget's context menu policy to Qt::ActionsContextMenu, - meaning that the \l{QAction}s associated with the widget should - appear in its context menu. - - Then we add the \uicontrol{Add Image} and \uicontrol{Remove All Images} - actions to the table widget. They will then appear in the table - widget's context menu. - - \snippet widgets/icons/mainwindow.cpp 31 - - In the \c checkCurrentStyle() function we go through the group of - style actions, looking for the current GUI style. - - For each action, we first extract the style name using - QAction::data(). Since this is only a QStyleFactory key (e.g., - "macos"), we cannot compare it directly to the current - style's class name. We need to create a QStyle object using the - static QStyleFactory::create() function and compare the class - name of the created QStyle object with that of the current style. - As soon as we are done with a QStyle candidate, we delete it. - - For all QObject subclasses that use the \c Q_OBJECT macro, the - class name of an object is available through its - \l{QObject::metaObject()}{meta-object}. - - We can assume that the style is supported by - QStyleFactory, but to be on the safe side we use the \c - Q_ASSERT() macro to make sure that QStyleFactory::create() - returned a valid pointer. - - \snippet widgets/icons/mainwindow.cpp 44 - - We overload the show() function to set up the updating of the - current screen in \c screenChanged(). After calling QWidget::show(), - the QWindow associated with the QWidget is created and we can - connect to its QWindow::screenChanged() signal. - - \section2 IconSizeSpinBox Class Definition - - \snippet widgets/icons/iconsizespinbox.h 0 - - The \c IconSizeSpinBox class is a subclass of QSpinBox. A plain - QSpinBox can only handle integers. But since we want to display - the spin box's values in a more sophisticated way, we need to - subclass QSpinBox and reimplement the QSpinBox::textFromValue() - and QSpinBox::valueFromText() functions. - - \image icons_size_spinbox.png Screenshot of the icon size spinbox - - \section2 IconSizeSpinBox Class Implementation - - \snippet widgets/icons/iconsizespinbox.cpp 0 - - The constructor is trivial. - - \snippet widgets/icons/iconsizespinbox.cpp 2 - - QSpinBox::textFromValue() is used by the spin box whenever it - needs to display a value. The default implementation returns a - base 10 representation of the \c value parameter. - - Our reimplementation returns a QString of the form "32 x 32". - - \snippet widgets/icons/iconsizespinbox.cpp 1 - - The QSpinBox::valueFromText() function is used by the spin box - whenever it needs to interpret text typed in by the user. Since - we reimplement the \c textFromValue() function we also need to - reimplement the \c valueFromText() function to interpret the - parameter text and return the associated int value. - - We parse the text using a regular expression (a QRegularExpression). We - define an expression that matches one or several digits, - optionally followed by whitespace, an "x" or the times symbol, - whitespace and one or several digits again. - - The first digits of the regular expression are captured using - parentheses. This enables us to use the QRegularExpressionMatch::captured() - or QRegularExpressionMatch::capturedTexts() functions to extract the matched - characters. If the first and second numbers of the spin box value - differ (e.g., "16 x 24"), we use the first number. - - When the user presses \uicontrol Enter, QSpinBox first calls - QSpinBox::valueFromText() to interpret the text typed by the - user, then QSpinBox::textFromValue() to present it in a canonical - format (e.g., "16 x 16"). - - \section2 ImageDelegate Class Definition - - \snippet widgets/icons/imagedelegate.h 0 - - The \c ImageDelegate class is a subclass of QStyledItemDelegate. The - QStyledItemDelegate class provides display and editing facilities for - data items from a model. A single QStyledItemDelegate object is - responsible for all items displayed in a item view (in our case, - a QTableWidget). - - A QStyledItemDelegate can be used to provide an editor for an item view - class that is subclassed from QAbstractItemView. Using a delegate - for this purpose allows the editing mechanism to be customized and - developed independently from the model and view. - - \snippet widgets/icons/imagedelegate.h 1 - - The default implementation of QStyledItemDelegate creates a QLineEdit. - Since we want the editor to be a QComboBox, we need to subclass - QStyledItemDelegate and reimplement the QStyledItemDelegate::createEditor(), - QStyledItemDelegate::setEditorData() and QStyledItemDelegate::setModelData() - functions. - - \snippet widgets/icons/imagedelegate.h 2 - - The \c emitCommitData() slot is used to emit the - QImageDelegate::commitData() signal with the appropriate - argument. - - \section2 ImageDelegate Class Implementation - - \snippet widgets/icons/imagedelegate.cpp 0 - - The constructor is trivial. - - \snippet widgets/icons/imagedelegate.cpp 1 - - The default QStyledItemDelegate::createEditor() implementation returns - the widget used to edit the item specified by the model and item - index for editing. The parent widget and style option are used to - control the appearance of the editor widget. - - Our reimplementation creates and populates a combobox instead of - the default line edit. The contents of the combobox depends on - the column in the table for which the editor is requested. Column - 1 contains the QIcon modes, whereas column 2 contains the QIcon - states. - - In addition, we connect the combobox's \l - {QComboBox::activated()}{activated()} signal to the \c - emitCommitData() slot to emit the - QAbstractItemDelegate::commitData() signal whenever the user - chooses an item using the combobox. This ensures that the rest of - the application notices the change and updates itself. - - \snippet widgets/icons/imagedelegate.cpp 2 - - The QStyledItemDelegate::setEditorData() function is used by - QTableWidget to transfer data from a QTableWidgetItem to the - editor. The data is stored as a string; we use - QComboBox::findText() to locate it in the combobox. - - Delegates work in terms of models, not items. This makes it - possible to use them with any item view class (e.g., QListView, - QListWidget, QTreeView, etc.). The transition between model and - items is done implicitly by QTableWidget; we don't need to worry - about it. - - \snippet widgets/icons/imagedelegate.cpp 3 - - The QStyledItemDelegate::setEditorData() function is used by QTableWidget - to transfer data back from the editor to the \l{QTableWidgetItem}. - - \snippet widgets/icons/imagedelegate.cpp 4 - - The \c emitCommitData() slot simply emit the - QAbstractItemDelegate::commitData() signal for the editor that - triggered the slot. This signal must be emitted when the editor - widget has completed editing the data, and wants to write it back - into the model. - - \section2 The Implementation of the Function main() - - \snippet widgets/icons/main.cpp 45 - - We use QCommandLineParser to handle any command line options or parameters - passed to the application. Then, we resize the main window according - to the available screen geometry and show it. -*/ diff --git a/examples/widgets/widgets/CMakeLists.txt b/examples/widgets/widgets/CMakeLists.txt index a8432644f9e..76240d487c4 100644 --- a/examples/widgets/widgets/CMakeLists.txt +++ b/examples/widgets/widgets/CMakeLists.txt @@ -5,7 +5,6 @@ qt_internal_add_example(analogclock) qt_internal_add_example(calculator) qt_internal_add_example(calendarwidget) qt_internal_add_example(groupbox) -qt_internal_add_example(icons) qt_internal_add_example(imageviewer) qt_internal_add_example(lineedits) if(QT_FEATURE_movie) diff --git a/examples/widgets/widgets/icons/CMakeLists.txt b/examples/widgets/widgets/icons/CMakeLists.txt deleted file mode 100644 index 4dc3fb09eed..00000000000 --- a/examples/widgets/widgets/icons/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(icons LANGUAGES CXX) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/widgets/icons") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) - -qt_standard_project_setup() - -qt_add_executable(icons - iconpreviewarea.cpp iconpreviewarea.h - iconsizespinbox.cpp iconsizespinbox.h - imagedelegate.cpp imagedelegate.h - main.cpp - mainwindow.cpp mainwindow.h -) - -set_target_properties(icons PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_compile_definitions(icons PRIVATE - SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}" -) - -target_link_libraries(icons PRIVATE - Qt6::Core - Qt6::Gui - Qt6::Widgets -) - -install(TARGETS icons - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/widgets/widgets/icons/iconpreviewarea.cpp b/examples/widgets/widgets/icons/iconpreviewarea.cpp deleted file mode 100644 index 42a5fa7914a..00000000000 --- a/examples/widgets/widgets/icons/iconpreviewarea.cpp +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "iconpreviewarea.h" - -#include <QGridLayout> -#include <QLabel> -#include <QWindow> - -//! [0] -IconPreviewArea::IconPreviewArea(QWidget *parent) - : QWidget(parent) -{ - QGridLayout *mainLayout = new QGridLayout(this); - - for (int row = 0; row < NumStates; ++row) { - stateLabels[row] = createHeaderLabel(IconPreviewArea::iconStateNames().at(row)); - mainLayout->addWidget(stateLabels[row], row + 1, 0); - } - Q_ASSERT(NumStates == 2); - - for (int column = 0; column < NumModes; ++column) { - modeLabels[column] = createHeaderLabel(IconPreviewArea::iconModeNames().at(column)); - mainLayout->addWidget(modeLabels[column], 0, column + 1); - } - Q_ASSERT(NumModes == 4); - - for (int column = 0; column < NumModes; ++column) { - for (int row = 0; row < NumStates; ++row) { - pixmapLabels[column][row] = createPixmapLabel(); - mainLayout->addWidget(pixmapLabels[column][row], row + 1, column + 1); - } - } -} -//! [0] - -//! [42] -QList<QIcon::Mode> IconPreviewArea::iconModes() -{ - static const QList<QIcon::Mode> result = { QIcon::Normal, QIcon::Active, QIcon::Disabled, - QIcon::Selected }; - return result; -} - -QList<QIcon::State> IconPreviewArea::iconStates() -{ - static const QList<QIcon::State> result = { QIcon::Off, QIcon::On }; - return result; -} - -QStringList IconPreviewArea::iconModeNames() -{ - static const QStringList result = {tr("Normal"), tr("Active"), tr("Disabled"), tr("Selected")}; - return result; -} - -QStringList IconPreviewArea::iconStateNames() -{ - static const QStringList result = {tr("Off"), tr("On")}; - return result; -} -//! [42] - -//! [1] -void IconPreviewArea::setIcon(const QIcon &icon) -{ - this->icon = icon; - updatePixmapLabels(); -} -//! [1] - -//! [2] -void IconPreviewArea::setSize(const QSize &size) -{ - if (size != this->size) { - this->size = size; - updatePixmapLabels(); - } -} -//! [2] - -//! [3] -QLabel *IconPreviewArea::createHeaderLabel(const QString &text) -{ - QLabel *label = new QLabel(tr("<b>%1</b>").arg(text)); - label->setAlignment(Qt::AlignCenter); - return label; -} -//! [3] - -//! [4] -QLabel *IconPreviewArea::createPixmapLabel() -{ - QLabel *label = new QLabel; - label->setEnabled(false); - label->setAlignment(Qt::AlignCenter); - label->setFrameShape(QFrame::Box); - label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - label->setBackgroundRole(QPalette::Base); - label->setAutoFillBackground(true); - label->setMinimumSize(132, 132); - return label; -} -//! [4] - -//! [5] -void IconPreviewArea::updatePixmapLabels() -{ - for (int column = 0; column < NumModes; ++column) { - for (int row = 0; row < NumStates; ++row) { - const QPixmap pixmap = - icon.pixmap(size, devicePixelRatio(), IconPreviewArea::iconModes().at(column), - IconPreviewArea::iconStates().at(row)); - QLabel *pixmapLabel = pixmapLabels[column][row]; - pixmapLabel->setPixmap(pixmap); - pixmapLabel->setEnabled(!pixmap.isNull()); - QString toolTip; - if (!pixmap.isNull()) { - const QSize actualSize = icon.actualSize(size); - toolTip = - tr("Size: %1x%2\nActual size: %3x%4\nDevice pixel ratio: %5") - .arg(size.width()).arg(size.height()) - .arg(actualSize.width()).arg(actualSize.height()) - .arg(pixmap.devicePixelRatio()); - } - pixmapLabel->setToolTip(toolTip); - } - } -} -//! [5] diff --git a/examples/widgets/widgets/icons/iconpreviewarea.h b/examples/widgets/widgets/icons/iconpreviewarea.h deleted file mode 100644 index c59ebc42ba2..00000000000 --- a/examples/widgets/widgets/icons/iconpreviewarea.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef ICONPREVIEWAREA_H -#define ICONPREVIEWAREA_H - -#include <QIcon> -#include <QWidget> -#include <QStringList> -#include <QList> - -QT_BEGIN_NAMESPACE -class QLabel; -QT_END_NAMESPACE - -//! [0] -class IconPreviewArea : public QWidget -{ - Q_OBJECT - -public: - explicit IconPreviewArea(QWidget *parent = nullptr); - - void setIcon(const QIcon &icon); - void setSize(const QSize &size); - - static QList<QIcon::Mode> iconModes(); - static QList<QIcon::State> iconStates(); - static QStringList iconModeNames(); - static QStringList iconStateNames(); - -private: - QLabel *createHeaderLabel(const QString &text); - QLabel *createPixmapLabel(); - void updatePixmapLabels(); - - enum { NumModes = 4, NumStates = 2 }; - - QIcon icon; - QSize size; - QLabel *stateLabels[NumStates]; - QLabel *modeLabels[NumModes]; - QLabel *pixmapLabels[NumModes][NumStates]; -}; -//! [0] - -#endif diff --git a/examples/widgets/widgets/icons/icons.pro b/examples/widgets/widgets/icons/icons.pro deleted file mode 100644 index 243ce6b0928..00000000000 --- a/examples/widgets/widgets/icons/icons.pro +++ /dev/null @@ -1,20 +0,0 @@ -QT += widgets -requires(qtConfig(combobox)) - -HEADERS = iconpreviewarea.h \ - iconsizespinbox.h \ - imagedelegate.h \ - mainwindow.h -SOURCES = iconpreviewarea.cpp \ - iconsizespinbox.cpp \ - imagedelegate.cpp \ - main.cpp \ - mainwindow.cpp - -DEFINES += SRCDIR=\\\"$$PWD/\\\" - -EXAMPLE_FILES = images/* - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/icons -INSTALLS += target diff --git a/examples/widgets/widgets/icons/iconsizespinbox.cpp b/examples/widgets/widgets/icons/iconsizespinbox.cpp deleted file mode 100644 index 16c88f4dd07..00000000000 --- a/examples/widgets/widgets/icons/iconsizespinbox.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "iconsizespinbox.h" - -#include <QRegularExpression> - -//! [0] -IconSizeSpinBox::IconSizeSpinBox(QWidget *parent) - : QSpinBox(parent) -{ -} -//! [0] - -//! [1] -int IconSizeSpinBox::valueFromText(const QString &text) const -{ - static const QRegularExpression regExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?")); - Q_ASSERT(regExp.isValid()); - - const QRegularExpressionMatch match = regExp.match(text); - if (match.isValid()) - return match.captured(1).toInt(); - return 0; -} -//! [1] - -//! [2] -QString IconSizeSpinBox::textFromValue(int value) const -{ - return tr("%1 x %1").arg(value); -} -//! [2] diff --git a/examples/widgets/widgets/icons/iconsizespinbox.h b/examples/widgets/widgets/icons/iconsizespinbox.h deleted file mode 100644 index ac304b47941..00000000000 --- a/examples/widgets/widgets/icons/iconsizespinbox.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef ICONSIZESPINBOX_H -#define ICONSIZESPINBOX_H - -#include <QSpinBox> - -//! [0] -class IconSizeSpinBox : public QSpinBox -{ - Q_OBJECT - -public: - explicit IconSizeSpinBox(QWidget *parent = nullptr); - - int valueFromText(const QString &text) const override; - QString textFromValue(int value) const override; -}; -//! [0] - -#endif diff --git a/examples/widgets/widgets/icons/imagedelegate.cpp b/examples/widgets/widgets/icons/imagedelegate.cpp deleted file mode 100644 index ec3c227c713..00000000000 --- a/examples/widgets/widgets/icons/imagedelegate.cpp +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "imagedelegate.h" -#include "iconpreviewarea.h" - -#include <QComboBox> - -//! [0] -ImageDelegate::ImageDelegate(QObject *parent) - : QStyledItemDelegate(parent) -{} -//! [0] - -//! [1] -QWidget *ImageDelegate::createEditor(QWidget *parent, - const QStyleOptionViewItem & /* option */, - const QModelIndex &index) const -{ - QComboBox *comboBox = new QComboBox(parent); - if (index.column() == 1) - comboBox->addItems(IconPreviewArea::iconModeNames()); - else if (index.column() == 2) - comboBox->addItems(IconPreviewArea::iconStateNames()); - - connect(comboBox, &QComboBox::activated, - this, &ImageDelegate::emitCommitData); - - return comboBox; -} -//! [1] - -//! [2] -void ImageDelegate::setEditorData(QWidget *editor, - const QModelIndex &index) const -{ - QComboBox *comboBox = qobject_cast<QComboBox *>(editor); - if (!comboBox) - return; - - int pos = comboBox->findText(index.model()->data(index).toString(), - Qt::MatchExactly); - comboBox->setCurrentIndex(pos); -} -//! [2] - -//! [3] -void ImageDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, - const QModelIndex &index) const -{ - QComboBox *comboBox = qobject_cast<QComboBox *>(editor); - if (!comboBox) - return; - - model->setData(index, comboBox->currentText()); -} -//! [3] - -//! [4] -void ImageDelegate::emitCommitData() -{ - emit commitData(qobject_cast<QWidget *>(sender())); -} -//! [4] diff --git a/examples/widgets/widgets/icons/imagedelegate.h b/examples/widgets/widgets/icons/imagedelegate.h deleted file mode 100644 index 29707e68c65..00000000000 --- a/examples/widgets/widgets/icons/imagedelegate.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef IMAGEDELEGATE_H -#define IMAGEDELEGATE_H - -#include <QStyledItemDelegate> - -//! [0] -class ImageDelegate : public QStyledItemDelegate -{ - Q_OBJECT - -public: - explicit ImageDelegate(QObject *parent = nullptr); -//! [0] - -//! [1] - QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, - const QModelIndex &index) const override; - void setEditorData(QWidget *editor, const QModelIndex &index) const override; - void setModelData(QWidget *editor, QAbstractItemModel *model, - const QModelIndex &index) const override; - -//! [1] //! [2] -private slots: - void emitCommitData(); -//! [2] -}; - -#endif diff --git a/examples/widgets/widgets/icons/images/designer.png b/examples/widgets/widgets/icons/images/designer.png Binary files differdeleted file mode 100644 index 9f8578b49e6..00000000000 --- a/examples/widgets/widgets/icons/images/designer.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/find_disabled.png b/examples/widgets/widgets/icons/images/find_disabled.png Binary files differdeleted file mode 100644 index e85e33fe05a..00000000000 --- a/examples/widgets/widgets/icons/images/find_disabled.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/find_normal.png b/examples/widgets/widgets/icons/images/find_normal.png Binary files differdeleted file mode 100644 index 728c27f9051..00000000000 --- a/examples/widgets/widgets/icons/images/find_normal.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_off_128x128.png b/examples/widgets/widgets/icons/images/monkey_off_128x128.png Binary files differdeleted file mode 100644 index f8782672879..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_off_128x128.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_off_16x16.png b/examples/widgets/widgets/icons/images/monkey_off_16x16.png Binary files differdeleted file mode 100644 index 0ac57d7d4f9..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_off_16x16.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_off_32x32.png b/examples/widgets/widgets/icons/images/monkey_off_32x32.png Binary files differdeleted file mode 100644 index 1f7fab694c1..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_off_32x32.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_off_64x64.png b/examples/widgets/widgets/icons/images/monkey_off_64x64.png Binary files differdeleted file mode 100644 index 47d8f6de354..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_off_64x64.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_on_128x128.png b/examples/widgets/widgets/icons/images/monkey_on_128x128.png Binary files differdeleted file mode 100644 index 8e1c7468bf6..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_on_128x128.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_on_16x16.png b/examples/widgets/widgets/icons/images/monkey_on_16x16.png Binary files differdeleted file mode 100644 index 082e8527f1b..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_on_16x16.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_on_32x32.png b/examples/widgets/widgets/icons/images/monkey_on_32x32.png Binary files differdeleted file mode 100644 index bf9acc6caee..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_on_32x32.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/monkey_on_64x64.png b/examples/widgets/widgets/icons/images/monkey_on_64x64.png Binary files differdeleted file mode 100644 index 990f604d98c..00000000000 --- a/examples/widgets/widgets/icons/images/monkey_on_64x64.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/qt_extended_16x16.png b/examples/widgets/widgets/icons/images/qt_extended_16x16.png Binary files differdeleted file mode 100644 index 30bcb45ed28..00000000000 --- a/examples/widgets/widgets/icons/images/qt_extended_16x16.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/qt_extended_32x32.png b/examples/widgets/widgets/icons/images/qt_extended_32x32.png Binary files differdeleted file mode 100644 index d609c1e1e5b..00000000000 --- a/examples/widgets/widgets/icons/images/qt_extended_32x32.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/images/qt_extended_48x48.png b/examples/widgets/widgets/icons/images/qt_extended_48x48.png Binary files differdeleted file mode 100644 index 0e524fed5fa..00000000000 --- a/examples/widgets/widgets/icons/images/qt_extended_48x48.png +++ /dev/null diff --git a/examples/widgets/widgets/icons/main.cpp b/examples/widgets/widgets/icons/main.cpp deleted file mode 100644 index b7ed18ff94f..00000000000 --- a/examples/widgets/widgets/icons/main.cpp +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include <QApplication> -#include <QCommandLineParser> -#include <QScreen> - -#include "mainwindow.h" - -//! [45] -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - QCoreApplication::setApplicationName(MainWindow::tr("Icons")); - QCoreApplication::setApplicationVersion(QT_VERSION_STR); - QCommandLineParser commandLineParser; - commandLineParser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); - commandLineParser.addHelpOption(); - commandLineParser.addVersionOption(); - commandLineParser.addPositionalArgument(MainWindow::tr("[file]"), MainWindow::tr("Icon file(s) to open.")); - commandLineParser.process(QCoreApplication::arguments()); - - MainWindow mainWin; - if (!commandLineParser.positionalArguments().isEmpty()) - mainWin.loadImages(commandLineParser.positionalArguments()); - - const QRect availableGeometry = mainWin.screen()->availableGeometry(); - mainWin.resize(availableGeometry.width() / 2, availableGeometry.height() * 2 / 3); - mainWin.move((availableGeometry.width() - mainWin.width()) / 2, (availableGeometry.height() - mainWin.height()) / 2); - - mainWin.show(); - return app.exec(); -} -//! [45] diff --git a/examples/widgets/widgets/icons/mainwindow.cpp b/examples/widgets/widgets/icons/mainwindow.cpp deleted file mode 100644 index f893575f762..00000000000 --- a/examples/widgets/widgets/icons/mainwindow.cpp +++ /dev/null @@ -1,478 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "mainwindow.h" -#include "iconpreviewarea.h" -#include "iconsizespinbox.h" -#include "imagedelegate.h" - -#include <QActionGroup> -#include <QApplication> -#include <QButtonGroup> -#include <QCheckBox> -#include <QFileDialog> -#include <QHeaderView> -#include <QFormLayout> -#include <QGridLayout> -#include <QGroupBox> -#include <QImageReader> -#include <QLabel> -#include <QMenuBar> -#include <QMessageBox> -#include <QRadioButton> -#include <QScreen> -#include <QStandardPaths> -#include <QStyleFactory> -#include <QTableWidget> -#include <QWindow> - -//! [40] -enum { OtherSize = QStyle::PM_CustomBase }; -//! [40] - -//! [0] -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent) -{ - QWidget *centralWidget = new QWidget(this); - setCentralWidget(centralWidget); - - createActions(); - - QGridLayout *mainLayout = new QGridLayout(centralWidget); - - QGroupBox *previewGroupBox = new QGroupBox(tr("Preview")); - previewArea = new IconPreviewArea(previewGroupBox); - QVBoxLayout *previewLayout = new QVBoxLayout(previewGroupBox); - previewLayout->addWidget(previewArea); - - mainLayout->addWidget(previewGroupBox, 0, 0, 1, 2); - mainLayout->addWidget(createImagesGroupBox(), 1, 0); - QVBoxLayout *vBox = new QVBoxLayout; - vBox->addWidget(createIconSizeGroupBox()); - vBox->addWidget(createHighDpiIconSizeGroupBox()); - vBox->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding)); - mainLayout->addLayout(vBox, 1, 1); - createContextMenu(); - - setWindowTitle(tr("Icons")); - checkCurrentStyle(); - sizeButtonGroup->button(OtherSize)->click(); -} -//! [0] - -//! [44] -void MainWindow::show() -{ - QMainWindow::show(); - connect(windowHandle(), &QWindow::screenChanged, this, &MainWindow::screenChanged); - screenChanged(); -} -//! [44] - -//! [1] -void MainWindow::about() -{ - QMessageBox::about(this, tr("About Icons"), - tr("The <b>Icons</b> example illustrates how Qt renders an icon in " - "different modes (active, normal, disabled, and selected) and " - "states (on and off) based on a set of images.")); -} -//! [1] - -//! [2] -void MainWindow::changeStyle(bool checked) -{ - if (!checked) - return; - - const QAction *action = qobject_cast<QAction *>(sender()); -//! [2] //! [3] - QStyle *style = QStyleFactory::create(action->data().toString()); -//! [3] //! [4] - Q_ASSERT(style); - QApplication::setStyle(style); - - const QList<QAbstractButton*> buttons = sizeButtonGroup->buttons(); - for (QAbstractButton *button : buttons) { - const QStyle::PixelMetric metric = static_cast<QStyle::PixelMetric>(sizeButtonGroup->id(button)); - const int value = style->pixelMetric(metric); - switch (metric) { - case QStyle::PM_SmallIconSize: - button->setText(tr("Small (%1 x %1)").arg(value)); - break; - case QStyle::PM_LargeIconSize: - button->setText(tr("Large (%1 x %1)").arg(value)); - break; - case QStyle::PM_ToolBarIconSize: - button->setText(tr("Toolbars (%1 x %1)").arg(value)); - break; - case QStyle::PM_ListViewIconSize: - button->setText(tr("List views (%1 x %1)").arg(value)); - break; - case QStyle::PM_IconViewIconSize: - button->setText(tr("Icon views (%1 x %1)").arg(value)); - break; - case QStyle::PM_TabBarIconSize: - button->setText(tr("Tab bars (%1 x %1)").arg(value)); - break; - default: - break; - } - } - - triggerChangeSize(); -} -//! [4] - -//! [5] -void MainWindow::changeSize(QAbstractButton *button, bool checked) -{ - if (!checked) - return; - - const int index = sizeButtonGroup->id(button); - const bool other = index == int(OtherSize); - const int extent = other - ? otherSpinBox->value() - : QApplication::style()->pixelMetric(static_cast<QStyle::PixelMetric>(index)); - - previewArea->setSize(QSize(extent, extent)); - otherSpinBox->setEnabled(other); -} - -void MainWindow::triggerChangeSize() -{ - changeSize(sizeButtonGroup->checkedButton(), true); -} -//! [5] - -//! [6] -void MainWindow::changeIcon() -{ - QIcon icon; - - for (int row = 0; row < imagesTable->rowCount(); ++row) { - const QTableWidgetItem *fileItem = imagesTable->item(row, 0); - const QTableWidgetItem *modeItem = imagesTable->item(row, 1); - const QTableWidgetItem *stateItem = imagesTable->item(row, 2); - - if (fileItem->checkState() == Qt::Checked) { - const int modeIndex = IconPreviewArea::iconModeNames().indexOf(modeItem->text()); - Q_ASSERT(modeIndex >= 0); - const int stateIndex = IconPreviewArea::iconStateNames().indexOf(stateItem->text()); - Q_ASSERT(stateIndex >= 0); - const QIcon::Mode mode = IconPreviewArea::iconModes().at(modeIndex); - const QIcon::State state = IconPreviewArea::iconStates().at(stateIndex); -//! [6] - -//! [8] - const QString fileName = fileItem->data(Qt::UserRole).toString(); - QImage image(fileName); - if (!image.isNull()) - icon.addPixmap(QPixmap::fromImage(image), mode, state); -//! [8] - } - } -//! [11] - previewArea->setIcon(icon); -//! [11] -} - -void MainWindow::addSampleImages() -{ - addImages(QLatin1String(SRCDIR) + QLatin1String("/images")); -} - -void MainWindow::addOtherImages() -{ - static bool firstInvocation = true; - QString directory; - if (firstInvocation) { - firstInvocation = false; - directory = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QString()); - } - addImages(directory); -} - -//! [12] -void MainWindow::addImages(const QString &directory) -{ - QFileDialog fileDialog(this, tr("Open Images"), directory); - QStringList mimeTypeFilters; - const QList<QByteArray> mimeTypes = QImageReader::supportedMimeTypes(); - for (const QByteArray &mimeTypeName : mimeTypes) - mimeTypeFilters.append(mimeTypeName); - mimeTypeFilters.sort(); - fileDialog.setMimeTypeFilters(mimeTypeFilters); - fileDialog.selectMimeTypeFilter(QLatin1String("image/png")); - fileDialog.setAcceptMode(QFileDialog::AcceptOpen); - fileDialog.setFileMode(QFileDialog::ExistingFiles); - if (!nativeFileDialogAct->isChecked()) - fileDialog.setOption(QFileDialog::DontUseNativeDialog); - if (fileDialog.exec() == QDialog::Accepted) - loadImages(fileDialog.selectedFiles()); -//! [12] -} - -void MainWindow::loadImages(const QStringList &fileNames) -{ - for (const QString &fileName : fileNames) { - const int row = imagesTable->rowCount(); - imagesTable->setRowCount(row + 1); -//! [13] - const QFileInfo fileInfo(fileName); - const QString imageName = fileInfo.baseName(); - const QString fileName2x = fileInfo.absolutePath() - + QLatin1Char('/') + imageName + QLatin1String("@2x.") + fileInfo.suffix(); - const QFileInfo fileInfo2x(fileName2x); - const QImage image(fileName); - const QString toolTip = - tr("Directory: %1\nFile: %2\nFile@2x: %3\nSize: %4x%5") - .arg(QDir::toNativeSeparators(fileInfo.absolutePath()), fileInfo.fileName()) - .arg(fileInfo2x.exists() ? fileInfo2x.fileName() : tr("<None>")) - .arg(image.width()).arg(image.height()); - QTableWidgetItem *fileItem = new QTableWidgetItem(imageName); - fileItem->setData(Qt::UserRole, fileName); - fileItem->setIcon(QPixmap::fromImage(image)); - fileItem->setFlags((fileItem->flags() | Qt::ItemIsUserCheckable) & ~Qt::ItemIsEditable); - fileItem->setToolTip(toolTip); -//! [13] - -//! [15] - QIcon::Mode mode = QIcon::Normal; - QIcon::State state = QIcon::Off; - if (guessModeStateAct->isChecked()) { - if (imageName.contains(QLatin1String("_act"), Qt::CaseInsensitive)) - mode = QIcon::Active; - else if (imageName.contains(QLatin1String("_dis"), Qt::CaseInsensitive)) - mode = QIcon::Disabled; - else if (imageName.contains(QLatin1String("_sel"), Qt::CaseInsensitive)) - mode = QIcon::Selected; - - if (imageName.contains(QLatin1String("_on"), Qt::CaseInsensitive)) - state = QIcon::On; -//! [15] - } - -//! [18] - imagesTable->setItem(row, 0, fileItem); - QTableWidgetItem *modeItem = - new QTableWidgetItem(IconPreviewArea::iconModeNames().at(IconPreviewArea::iconModes().indexOf(mode))); - modeItem->setToolTip(toolTip); - imagesTable->setItem(row, 1, modeItem); - QTableWidgetItem *stateItem = - new QTableWidgetItem(IconPreviewArea::iconStateNames().at(IconPreviewArea::iconStates().indexOf(state))); - stateItem->setToolTip(toolTip); - imagesTable->setItem(row, 2, stateItem); - imagesTable->openPersistentEditor(modeItem); - imagesTable->openPersistentEditor(stateItem); - - fileItem->setCheckState(Qt::Checked); -//! [18] - } -} - -//! [20] -void MainWindow::removeAllImages() -{ - imagesTable->setRowCount(0); - changeIcon(); -} -//! [20] - -//! [21] -QWidget *MainWindow::createImagesGroupBox() -{ - QGroupBox *imagesGroupBox = new QGroupBox(tr("Images")); - - imagesTable = new QTableWidget; - imagesTable->setSelectionMode(QAbstractItemView::NoSelection); - imagesTable->setItemDelegate(new ImageDelegate(this)); -//! [21] - -//! [22] - const QStringList labels({tr("Image"), tr("Mode"), tr("State")}); - - imagesTable->horizontalHeader()->setDefaultSectionSize(90); - imagesTable->setColumnCount(3); - imagesTable->setHorizontalHeaderLabels(labels); - imagesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); - imagesTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed); - imagesTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed); - imagesTable->verticalHeader()->hide(); -//! [22] - -//! [24] - connect(imagesTable, &QTableWidget::itemChanged, - this, &MainWindow::changeIcon); - - QVBoxLayout *layout = new QVBoxLayout(imagesGroupBox); - layout->addWidget(imagesTable); - return imagesGroupBox; -//! [24] -} - -//! [26] -QWidget *MainWindow::createIconSizeGroupBox() -{ - QGroupBox *iconSizeGroupBox = new QGroupBox(tr("Icon Size")); - - sizeButtonGroup = new QButtonGroup(this); - sizeButtonGroup->setExclusive(true); - - connect(sizeButtonGroup, &QButtonGroup::buttonToggled, - this, &MainWindow::changeSize); - - QRadioButton *smallRadioButton = new QRadioButton; - sizeButtonGroup->addButton(smallRadioButton, QStyle::PM_SmallIconSize); - QRadioButton *largeRadioButton = new QRadioButton; - sizeButtonGroup->addButton(largeRadioButton, QStyle::PM_LargeIconSize); - QRadioButton *toolBarRadioButton = new QRadioButton; - sizeButtonGroup->addButton(toolBarRadioButton, QStyle::PM_ToolBarIconSize); - QRadioButton *listViewRadioButton = new QRadioButton; - sizeButtonGroup->addButton(listViewRadioButton, QStyle::PM_ListViewIconSize); - QRadioButton *iconViewRadioButton = new QRadioButton; - sizeButtonGroup->addButton(iconViewRadioButton, QStyle::PM_IconViewIconSize); - QRadioButton *tabBarRadioButton = new QRadioButton; - sizeButtonGroup->addButton(tabBarRadioButton, QStyle::PM_TabBarIconSize); - QRadioButton *otherRadioButton = new QRadioButton(tr("Other:")); - sizeButtonGroup->addButton(otherRadioButton, OtherSize); - otherSpinBox = new IconSizeSpinBox; - otherSpinBox->setRange(8, 256); - const QString spinBoxToolTip = - tr("Enter a custom size within %1..%2") - .arg(otherSpinBox->minimum()).arg(otherSpinBox->maximum()); - otherSpinBox->setValue(64); - otherSpinBox->setToolTip(spinBoxToolTip); - otherRadioButton->setToolTip(spinBoxToolTip); -//! [26] - -//! [27] - connect(otherSpinBox, &QSpinBox::valueChanged, - this, &MainWindow::triggerChangeSize); - - QHBoxLayout *otherSizeLayout = new QHBoxLayout; - otherSizeLayout->addWidget(otherRadioButton); - otherSizeLayout->addWidget(otherSpinBox); - otherSizeLayout->addStretch(); - - QGridLayout *layout = new QGridLayout(iconSizeGroupBox); - layout->addWidget(smallRadioButton, 0, 0); - layout->addWidget(largeRadioButton, 1, 0); - layout->addWidget(toolBarRadioButton, 2, 0); - layout->addWidget(listViewRadioButton, 0, 1); - layout->addWidget(iconViewRadioButton, 1, 1); - layout->addWidget(tabBarRadioButton, 2, 1); - layout->addLayout(otherSizeLayout, 3, 0, 1, 2); - layout->setRowStretch(4, 1); - return iconSizeGroupBox; -//! [27] -} - -void MainWindow::screenChanged() -{ - devicePixelRatioLabel->setText(QString::number(devicePixelRatio())); - if (const QWindow *window = windowHandle()) { - const QScreen *screen = window->screen(); - const QString screenDescription = - tr("\"%1\" (%2x%3)").arg(screen->name()) - .arg(screen->geometry().width()).arg(screen->geometry().height()); - screenNameLabel->setText(screenDescription); - } - changeIcon(); -} - -QWidget *MainWindow::createHighDpiIconSizeGroupBox() -{ - QGroupBox *highDpiGroupBox = new QGroupBox(tr("High DPI Scaling")); - QFormLayout *layout = new QFormLayout(highDpiGroupBox); - devicePixelRatioLabel = new QLabel(highDpiGroupBox); - screenNameLabel = new QLabel(highDpiGroupBox); - layout->addRow(tr("Screen:"), screenNameLabel); - layout->addRow(tr("Device pixel ratio:"), devicePixelRatioLabel); - return highDpiGroupBox; -} - -//! [28] -void MainWindow::createActions() -{ - QMenu *fileMenu = menuBar()->addMenu(tr("&File")); - - addSampleImagesAct = new QAction(tr("Add &Sample Images..."), this); - addSampleImagesAct->setShortcut(tr("Ctrl+A")); - connect(addSampleImagesAct, &QAction::triggered, this, &MainWindow::addSampleImages); - fileMenu->addAction(addSampleImagesAct); - - addOtherImagesAct = new QAction(tr("&Add Images..."), this); - addOtherImagesAct->setShortcut(QKeySequence::Open); - connect(addOtherImagesAct, &QAction::triggered, this, &MainWindow::addOtherImages); - fileMenu->addAction(addOtherImagesAct); - - removeAllImagesAct = new QAction(tr("&Remove All Images"), this); - removeAllImagesAct->setShortcut(tr("Ctrl+R")); - connect(removeAllImagesAct, &QAction::triggered, - this, &MainWindow::removeAllImages); - fileMenu->addAction(removeAllImagesAct); - - fileMenu->addSeparator(); - - QAction *exitAct = fileMenu->addAction(tr("&Quit"), qApp, &QCoreApplication::quit); - exitAct->setShortcuts(QKeySequence::Quit); - - QMenu *viewMenu = menuBar()->addMenu(tr("&View")); - - styleActionGroup = new QActionGroup(this); - const QStringList styleKeys = QStyleFactory::keys(); - for (const QString &styleName : styleKeys) { - QAction *action = new QAction(tr("%1 Style").arg(styleName), styleActionGroup); - action->setData(styleName); - action->setCheckable(true); - connect(action, &QAction::triggered, this, &MainWindow::changeStyle); - viewMenu->addAction(action); - } - - QMenu *settingsMenu = menuBar()->addMenu(tr("&Settings")); - - guessModeStateAct = new QAction(tr("&Guess Image Mode/State"), this); - guessModeStateAct->setCheckable(true); - guessModeStateAct->setChecked(true); - settingsMenu->addAction(guessModeStateAct); - - nativeFileDialogAct = new QAction(tr("&Use Native File Dialog"), this); - nativeFileDialogAct->setCheckable(true); - nativeFileDialogAct->setChecked(true); - settingsMenu->addAction(nativeFileDialogAct); - - QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); - helpMenu->addAction(tr("&About"), this, &MainWindow::about); - helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); -} -//! [28] - -//! [30] -void MainWindow::createContextMenu() -{ - imagesTable->setContextMenuPolicy(Qt::ActionsContextMenu); - imagesTable->addAction(addSampleImagesAct); - imagesTable->addAction(addOtherImagesAct); - imagesTable->addAction(removeAllImagesAct); -} -//! [30] - -//! [31] -void MainWindow::checkCurrentStyle() -{ - const QList<QAction *> actions = styleActionGroup->actions(); - for (QAction *action : actions) { - const QString styleName = action->data().toString(); - const std::unique_ptr<QStyle> candidate{QStyleFactory::create(styleName)}; - Q_ASSERT(candidate); - if (candidate->metaObject()->className() - == QApplication::style()->metaObject()->className()) { - action->trigger(); - return; - } - } -} -//! [31] diff --git a/examples/widgets/widgets/icons/mainwindow.h b/examples/widgets/widgets/icons/mainwindow.h deleted file mode 100644 index fb6e0493f74..00000000000 --- a/examples/widgets/widgets/icons/mainwindow.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef MAINWINDOW_H -#define MAINWINDOW_H - -#include <QIcon> -#include <QList> -#include <QMainWindow> -#include <QPixmap> - -QT_BEGIN_NAMESPACE -class QAction; -class QActionGroup; -class QLabel; -class QButtonGroup; -class QTableWidget; -class QAbstractButton; -QT_END_NAMESPACE -class IconPreviewArea; -class IconSizeSpinBox; - -//! [0] -class MainWindow : public QMainWindow -{ - Q_OBJECT - -public: - MainWindow(QWidget *parent = nullptr); - - void loadImages(const QStringList &fileNames); - - void show(); - -private slots: - void about(); - void changeStyle(bool checked); - void changeSize(QAbstractButton *button, bool); - void triggerChangeSize(); - void changeIcon(); - void addSampleImages(); - void addOtherImages(); - void removeAllImages(); - void screenChanged(); - -private: - QWidget *createImagesGroupBox(); - QWidget *createIconSizeGroupBox(); - QWidget *createHighDpiIconSizeGroupBox(); - void createActions(); - void createContextMenu(); - void checkCurrentStyle(); - void addImages(const QString &directory); - - IconPreviewArea *previewArea; - - QTableWidget *imagesTable; - - QButtonGroup *sizeButtonGroup; - IconSizeSpinBox *otherSpinBox; - - QLabel *devicePixelRatioLabel; - QLabel *screenNameLabel; - - QAction *addOtherImagesAct; - QAction *addSampleImagesAct; - QAction *removeAllImagesAct; - QAction *guessModeStateAct; - QAction *nativeFileDialogAct; - QActionGroup *styleActionGroup; -}; -//! [0] - -#endif diff --git a/examples/widgets/widgets/widgets.pro b/examples/widgets/widgets/widgets.pro index f67184ce63e..1d48debfd27 100644 --- a/examples/widgets/widgets/widgets.pro +++ b/examples/widgets/widgets/widgets.pro @@ -3,7 +3,6 @@ SUBDIRS = analogclock \ calculator \ calendarwidget \ groupbox \ - icons \ imageviewer \ lineedits \ movie \ |