Qt to write custom color drop-down box 52-

I. Introduction

Control wrote this for a long time, one of the veteran-level control, early stage of development is their main use to several projects, such as providing a color drop-down box to set the corresponding time curve or color, video surveillance project frequently used OSD tab settings, degree of difficulty of this control close to zero, beginners will, in fact, the internet search also provides a method to draw a lot of people, that is to enumerate QColor :: colorNames () to get all of the built-in color, and then generate the corresponding picture as the icon provided to the drop-down box to the item, the corresponding icon width and height determined by the width and height of the control itself, the present control inherits from qcombobox control, completely retains all the characteristics of the control, while adding the color change signal, so that the user use.

Second, the realization of functions

  • 1: drop-down box element may be provided height
  • 2: drop-down box element widths may be provided
  • 3: can set whether to automatically adjust the width of the drop-down box element, automatically adjusted according to the width and height element

Third, renderings

Fourth, the header file code

#ifndef COMBOBOX_H
#define COMBOBOX_H

/**
 * 自定义宽高下拉框控件 作者:feiyangqingyun(QQ:517216493) 2017-4-11
 * 1:可设置下拉框元素高度
 * 2:可设置下拉框元素宽度
 * 3:可设置是否自动调整下拉框元素宽度,根据元素宽高自动调整
 */

#include <QComboBox>

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

class QDESIGNER_WIDGET_EXPORT ComboBox : public QComboBox
#else
class ComboBox : public QComboBox
#endif

{
    Q_OBJECT
    Q_PROPERTY(int itemWidth READ getItemWidth WRITE setItemWidth)
    Q_PROPERTY(int itemHeight READ getItemHeight WRITE setItemHeight)
    Q_PROPERTY(bool autoWidth READ getAutoWidth WRITE setAutoWidth)

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

protected:
    void showEvent(QShowEvent *);

private:
    int itemWidth;                  //元素宽度
    int itemHeight;                 //元素高度
    bool autoWidth;                 //是否自动调整元素宽度
    int maxItemWidth;               //最大元素宽度

public:
    int getItemWidth()              const;
    int getItemHeight()             const;
    bool getAutoWidth()             const;

public Q_SLOTS:
    void setItemWidth(int itemWidth);
    void setItemHeight(int itemHeight);
    void setAutoWidth(bool autoWidth);
};

#endif // COMBOBOX_H

Fifth, the core code

#pragma execution_character_set("utf-8")

#include "combobox.h"
#include "qlistview.h"
#include "qdebug.h"

ComboBox::ComboBox(QWidget *parent) : QComboBox(parent)
{
    itemWidth = 5;
    itemHeight = 20;
    autoWidth = true;
    this->setView(new QListView());
}

void ComboBox::showEvent(QShowEvent *)
{
    if (autoWidth) {
        //自动计算所有元素,找到最长的元素
        QFontMetrics fm = this->fontMetrics();
        int count = this->count();
        for (int i = 0; i < count; i++) {
            int textWidth = fm.width(this->itemText(i));
            itemWidth = textWidth > itemWidth ? textWidth : itemWidth;
        }

        //宽度增加像素,因为有边距
        this->view()->setFixedWidth(itemWidth + 20);
    }
}

int ComboBox::getItemWidth() const
{
    return this->itemWidth;
}

int ComboBox::getItemHeight() const
{
    return this->itemHeight;
}

bool ComboBox::getAutoWidth() const
{
    return this->autoWidth;
}

void ComboBox::setItemWidth(int itemWidth)
{
    if (this->itemWidth != itemWidth) {
        this->itemWidth = itemWidth;
        if (!autoWidth) {
            this->view()->setFixedWidth(itemWidth);
        }
    }
}

void ComboBox::setItemHeight(int itemHeight)
{
    if (this->itemHeight != itemHeight) {
        this->itemHeight = itemHeight;
        this->setStyleSheet(QString("QComboBox QAbstractItemView::item{min-height:%1px;}").arg(itemHeight));
    }
}

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

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 dynamic library files (dll or so, etc.) 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.
  15. Custom plug-in open dynamic library (permanent free), and the back door without any restrictions, ease of use.
  16. 26 now available version dll, which includes qt5.12.3 msvc2017 32 + 64 mingw 32 + 64 in.
  17. From time to time to increase control and improve controls, regularly updated SDK, to welcome all suggestions, thank you!
  18. Qt introductory books recommended Huo Yafei of "Qt Creator Quick Start" "Qt5 programming entry", Qt official Advanced book recommendations "C ++ GUI Qt4 programming."
  19. Highly recommended programmer self-discipline and planning book series "lying programmer" "Programmer's growth course", "grief programmer", benefited from a lifetime!
  20. SDK download link: https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ extraction code: 877p

Guess you like

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