Qt to write a custom control area of the control panel 47-

I. Introduction

On many web pages you can often see a device corresponds to a panel, or some equivalent type of information all on a panel, which can also be dragged, and the controls for the first time in a smart visitor management platform , a panel such as identity information, a visitor information panel, a panel information interviewees, this is equivalent to classification shows, also provides a corresponding title bar text has shown that the use of this control of the scene is very much, as well as sub You can set the title, expanding the flashing alarm interface.

Second, the realization of functions

  • 1: supports all widget subclass object, automatically generated scroll bars
  • 2: Support automatic stretch autofill
  • 3: providing interface to obtain pointers to all objects in the container
  • 4: can set whether automatic stretch Width Height
  • 5: spacing and margins may be provided between the device panel

Third, renderings

Fourth, the header file code

#ifndef PANELWIDGET_H
#define PANELWIDGET_H

/**
 * 面板容器控件 作者:feiyangqingyun(QQ:517216493) 2016-11-20
 * 1:支持所有widget子类对象,自动产生滚动条
 * 2:支持自动拉伸自动填充
 * 3:提供接口获取容器内的所有对象的指针
 * 4:可设置是否自动拉伸宽度高度
 * 5:可设置设备面板之间的间距和边距
 */

#include <QWidget>

class QScrollArea;
class QFrame;
class QVBoxLayout;
class QGridLayout;

#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif

class QDESIGNER_WIDGET_EXPORT PanelWidget : public QWidget
#else
class PanelWidget : public QWidget
#endif

{
    Q_OBJECT
    Q_PROPERTY(int margin READ getMargin WRITE setMargin)
    Q_PROPERTY(int space READ getSpace WRITE setSpace)
    Q_PROPERTY(bool autoWidth READ getAutoWidth WRITE setAutoWidth)
    Q_PROPERTY(bool autoHeight READ getAutoHeight WRITE setAutoHeight)

public:
    explicit PanelWidget(QWidget *parent = 0);

protected:
    void resizeEvent(QResizeEvent *);

private:
    QScrollArea *scrollArea;            //滚动区域
    QWidget *scrollAreaWidgetContents;  //滚动区域载体
    QFrame *frame;                      //放置设备的框架,自动变宽变高
    QVBoxLayout *verticalLayout;        //设备面板总布局
    QGridLayout *gridLayout;            //设备表格布局

    int margin;                         //边距
    int space;                          //设备之间的间隔
    bool autoWidth;                     //宽度自动拉伸
    bool autoHeight;                    //高度自动拉伸

    QList<QWidget *> widgets;           //设备面板对象集合
    int columnCount;                    //面板列数

public:
    QSize sizeHint()                const;
    QSize minimumSizeHint()         const;

    int getMargin()                 const;
    int getSpace()                  const;
    bool getAutoWidth()             const;
    bool getAutoHeight()            const;

    QList<QWidget *> getWidgets();
    int getColumnCount();

public Q_SLOTS:
    void setWidget(QList<QWidget *> widgets, int columnCount);
    void setMargin(int left, int top, int right, int bottom);
    void setMargin(int margin);
    void setSpace(int space);
    void setAutoWidth(bool autoWidth);
    void setAutoHeight(bool autoHeight);

};

#endif // PANELWIDGET_H

Fifth, the core code

void PanelWidget::setWidget(QList<QWidget *> widgets, int columnCount)
{
    this->widgets = widgets;
    this->columnCount = columnCount;

    int row = 0;
    int column = 0;
    int index = 0;

    //先把之前的所有移除并不可见
    foreach (QWidget *widget, widgets) {
        gridLayout->removeWidget(widget);
        widget->setVisible(false);
    }

    //重新添加到布局中并可见
    foreach (QWidget *widget, widgets) {
        gridLayout->addWidget(widget, row, column);
        widget->setVisible(true);

        column++;
        index++;
        if (index % columnCount == 0) {
            row++;
            column = 0;
        }
    }

    row++;

    //设置右边弹簧
    if (!autoWidth) {
        QSpacerItem *hSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
        gridLayout->addItem(hSpacer, 0, gridLayout->columnCount());
    }

    //设置底边弹簧
    if (!autoHeight) {
        QSpacerItem *vSpacer = new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding);
        gridLayout->addItem(vSpacer, row, 0);
    }
}

void PanelWidget::setMargin(int left, int top, int right, int bottom)
{
    gridLayout->setContentsMargins(left, top, right, bottom);
}

void PanelWidget::setMargin(int margin)
{
    if (this->margin != margin) {
        setMargin(margin, margin, margin, margin);
    }
}

void PanelWidget::setSpace(int space)
{
    if (this->space != space) {
        gridLayout->setSpacing(space);
    }
}

void PanelWidget::setAutoWidth(bool autoWidth)
{
    if (this->autoWidth != autoWidth) {
        this->autoWidth = autoWidth;
    }
}

void PanelWidget::setAutoHeight(bool autoHeight)
{
    if (this->autoHeight != autoHeight) {
        this->autoHeight = autoHeight;
    }
}

Sixth, the controls described

  1. More than 150 exquisite control, covers a variety of dashboards, progress bar, the progress of the ball, compass, graphs, scales, thermometers, navigation bar, navigation bar, flatui, highlight the button, slide the selector, the lunar calendar and so on. Qwt far more than the number of controls integration.
  2. Each class can be independently as a separate control, zero coupling each control file and a header file to achieve a code amount, independent of other files to facilitate individual control integrated into the project source code form, less. qwt interlocking control class, highly coupled, want to use one of the controls, must contain all the code.
  3. Write all pure Qt, QWidget + QPainter to draw, to support any Qt version Qt4.6 Qt5.12, support for mingw, msvc, gcc compiler, etc., support any operating system such as windows + linux + mac + embedded linux, which does not garbled can be directly integrated into Qt Creator, a built-in controls and use the same, most of the effects can be as long as several properties are set, very convenient.
  4. DEMO separate source containing the control corresponding to each control has a convenient reference. It also provides integrated use of all controls a DEMO.
  5. Source code for each control has detailed Chinese annotation, are prepared in accordance with unified design specifications, easy to learn to write custom controls.
  6. Each control default color and demo corresponding color is very beautiful.
  7. More than 130 visible control, six invisible control.
  8. Portion control provides a variety of styles style selection, multiple choice style indicator.
  9. All controls changes adaptive stretched form.
  10. Integrated design custom attribute that supports drag design, WYSIWYG support the import and export in xml format.
  11. Activex control that comes with demo, all controls can be run directly in the browser ie.
  12. Fontawesome integrated graphics font + Alibaba iconfont collection of hundreds of graphic fonts, font fun graphic brings.
  13. All controls and finally generate a dll dynamic library files, can be integrated directly into qtcreator designed for use in drag.
  14. Already qml version, the latter will consider a pyqt version, if the user is in great demand then.

Seven, SDK download

  • SDK download link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p
  • Download link is included in the various versions of the dynamic library files, header files of all the controls, the use of demo, custom controls + properties designer.
  • Open plug-in custom dynamic library dll use (permanent free), and the back door without any restrictions, ease of use.
  • 26 now available version dll, which includes qt5.12.3 msvc2017 32 + 64 mingw 32 + 64 in.
  • From time to time to increase control and improve controls, regularly updated SDK, to welcome all suggestions, thank you!
  • widget versions (QQ: 517216493) qml versions (QQ: 373955953) sambong camel (QQ: 278969898).
  • Qt's advanced column know almost Taoge road https://zhuanlan.zhihu.com/TaoQt
  • Welcome concern public micro-channel number] [efficient programmers, content C ++ / Python, learning, writing skills, popular technology, career development, a lot of dry goods, benefits a lot!
  • Qt introductory books recommended Huo Yafei of "Qt Creator Quick Start" "Qt5 programming entry", Qt official Advanced book recommendations "C ++ GUI Qt4 programming."
  • Highly recommended programmer self-discipline and planning book series "lying programmer" "Programmer's growth course", "grief programmer", benefited from a lifetime!

Guess you like

Origin www.cnblogs.com/feiyangqingyun/p/11330770.html