zoukankan      html  css  js  c++  java
  • QT批量实现单选按钮组

    自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

    https://www.cnblogs.com/bclshuai/p/11380657.html

    QT批量实现单选按钮组

     

    目录

    1      应用场景... 1

    2      实现方案... 1

    1       应用场景

    筛选条件过多时,会有多个条件筛选,选择样式是按钮形式,不是那种勾选。如下图所示,单选条件,有多选项, 每一个条件只能选择一个, 选中之后颜色加深,没选中的自动变为浅灰色。

     

    2       实现方案

    如果一个一个的在界面上去拼,会造成大量的工作量,而且如果要修改一条,则需要挪动多个按钮的位置,所以可以通过代码批量实现,自动排序位置。

    每一行用一个类来封装,设置名称和选项值,点击按钮时需要保证选项的唯一性。

    1)头文件定义接口
    #pragma once
    #include <QWidget>
    #include<QPushButton>
    struct SelectItem
    {
        int value = -1;
        QString name = "不限";
        QPushButton* pButtton = NULL;
        SelectItem(int v,QString n)
        {
            value = v;
            name = n;
            pButtton = NULL;
        }
        SelectItem()
        {
            value = -1;
            name = "不限";
            pButtton = NULL;
        }
        
    };
    class SelectButton : public QWidget {
        Q_OBJECT
    
    public:
        SelectButton(QWidget * parent = Q_NULLPTR);
        ~SelectButton();
        /*
        输入显示的数据
        strName:条件的名称
        Items  :条件的选项
        */
        void SetData(QString strName,QVector<SelectItem> Items);
        /*
        获取选中的按钮对应的值
        */
        int GetValue();
        /*
        重置按钮
        */
        void Reset();
    private:
        QVector<SelectItem> m_Items;//按钮对应的数值
        int m_currentIndex=0;//选中按钮的索引值
    };
    
    (2)源文件实现接口
    #include "SelectButton.hpp"
    #include<QLabel>
    SelectButton::SelectButton(QWidget * parent) {
        if (parent != NULL)
        {
            setParent(parent);
        }
        setFixedHeight(40);
    }
    
    SelectButton::~SelectButton() {
        for (int j = 0; j < m_Items.size(); j++)
        {
            if (m_Items[j].pButtton != NULL)
            {
                delete m_Items[j].pButtton;
                m_Items[j].pButtton = NULL;
            }
        }
    }
    
    void SelectButton::SetData(QString strName, QVector<SelectItem> Items)
    {
        m_Items = Items;
        QLabel* pName = new QLabel(this);
        pName->setText(strName);
        pName->setGeometry(24, 0, 100, 40);
        int namewidth = 100;
        int Itemwidth = 70;
        for (int i = 0; i < m_Items.size(); i++)
        {
            QPushButton* pButton = new QPushButton(this);
            if (pButton != NULL)
            {
                if (i==0)
                {//默认选择第0个
                    pButton->setStyleSheet(QLatin1String("QPushButton
    "
                        "{
    "
                        "    background:rgb(79, 79, 79);
    "
                        "    border:0px;
    "
                        "}
    "
                        "QPushButton::hover
    "
                        "{
    "
                        "    background:rgb(79, 79, 79);
    "
                        "}"));//设置为未选中状态
                }
                else
                {
                    pButton->setStyleSheet(QLatin1String("QPushButton
    "
                        "{
    "
                        "    background:rgb(177, 177, 177);
    "
                        "    border:0px;
    "
                        "}
    "
                        "QPushButton::hover
    "
                        "{
    "
                        "    background:rgb(79, 79, 79);
    "
                        "}"));//设置为未选中状态
                }
                
                m_Items[i].pButtton = pButton;
                pButton->setGeometry(i * Itemwidth + namewidth, 0, 40, 40);
                pButton->setText(m_Items[i].name);
                connect(pButton, &QPushButton::clicked, this, [=]() {
                    if (m_currentIndex != i)//选择变化
                    {    
                        pButton->setStyleSheet(QLatin1String("QPushButton
    "
                        "{
    "
                        "    background:rgb(79, 79, 79);
    "
                        "    border:0px;
    "
                        "}
    "
                        "QPushButton::hover
    "
                        "{
    "
                        "    background:rgb(79, 79, 79);
    "
                        "}"));//设置为选中状态
                        if (m_currentIndex<m_Items.size()&& m_currentIndex>=0)
                        {
                            if (m_Items[m_currentIndex].pButtton!=NULL)
                            {
                                m_Items[m_currentIndex].pButtton->setStyleSheet(QLatin1String("QPushButton
    "
                                    "{
    "
                                    "    background:rgb(177, 177, 177);
    "
                                    "    border:0px;
    "
                                    "}
    "
                                    "QPushButton::hover
    "
                                    "{
    "
                                    "    background:rgb(79, 79, 79);
    "
                                    "}"));//设置为没选中状态
                            }                    
                        }    
                        m_currentIndex = i;//更新当前值
                    }
                    
                
                    for (int j = 0; j < m_Items.size(); j++)
                    {
                        if (i != j)//设置其他按钮为未选中状态
                        {
                            
                        }
                    }
                });
            }
        }
        this->setFixedWidth(m_Items.size() * Itemwidth + namewidth);//设置宽度
    }
    
    int SelectButton::GetValue()
    {
        if (m_currentIndex<0 || m_currentIndex>=m_Items.size())
        {
            return -1;
        }
        else
        {
            return m_Items[m_currentIndex].value;
        }
        return 0;
    }
    
    void SelectButton::Reset()
    {
        if (m_currentIndex < m_Items.size() && m_currentIndex >= 0)
        {
            if (m_Items[m_currentIndex].pButtton != NULL)
            {
                m_Items[m_currentIndex].pButtton->setStyleSheet(QLatin1String("QPushButton
    "
                    "{
    "
                    "    background:rgb(177, 177, 177);
    "
                    "    border:0px;
    "
                    "}
    "
                    "QPushButton::hover
    "
                    "{
    "
                    "    background:rgb(79, 79, 79);
    "
                    "}"));//设置为没选中状态
            }
        }
        m_currentIndex = 0;
        if (m_Items[m_currentIndex].pButtton != NULL)
        {
            m_Items[m_currentIndex].pButtton->setStyleSheet(QLatin1String("QPushButton
    "
                "{
    "
                "    background:rgb(79, 79, 79);
    "
                "    border:0px;
    "
                "}
    "
                "QPushButton::hover
    "
                "{
    "
                "    background:rgb(79, 79, 79);
    "
                "}"));//设置为选中状态
        }
    }
    
    (3)主程序中创建选择项对象
    //人体属性界面
        xpos = 0;
        ypos = 8;
        yindex = 0;
        SelectButton* pBodySex = new SelectButton(ui.pageBody);
        pBodySex->SetData("性别", sexItem);
        pBodySex->move(xpos, ypos + yindex * 50);
        yindex++;
        SelectButton* pBodyAge = new SelectButton(ui.pageBody);
        pBodyAge->SetData("年龄", ageStageItem);
        pBodyAge->move(xpos, ypos + yindex * 50);
        yindex++;
    
        SelectButton* pBodyGlass = new SelectButton(ui.pageBody);
        pBodyGlass->SetData("戴眼镜", glassItem);
        pBodyGlass->move(xpos, ypos + yindex * 50);
        yindex++;
    
        SelectButton* pBodyMask = new SelectButton(ui.pageBody);
        pBodyMask->SetData("戴口罩", glassItem);
        pBodyMask->move(xpos, ypos + yindex * 50);
        yindex++;
    
        SelectButton* pBodyHat = new SelectButton(ui.pageBody);
        pBodyHat->SetData("戴帽子", hatItem);
        pBodyHat->move(xpos, ypos + yindex * 50);
        yindex++;
    
        SelectButton* pBodyUpClotherColor = new SelectButton(ui.pageBody);
        pBodyUpClotherColor->SetData("上衣颜色", clotherColorItem);
        pBodyUpClotherColor->move(xpos, ypos + yindex * 50);
        yindex++;
    
        SelectButton* pBodyDownClotherColor = new SelectButton(ui.pageBody);
        pBodyDownClotherColor->SetData("下衣颜色", clotherColorItem);
        pBodyDownClotherColor->move(xpos, ypos + yindex * 50);
        yindex++;
    
        SelectButton* pBodyBag= new SelectButton(ui.pageBody);
        pBodyBag->SetData("背包", maskItem);
        pBodyBag->move(xpos, ypos + yindex * 50);
        yindex++;
    
        SelectButton* pBodyTake = new SelectButton(ui.pageBody);
        pBodyTake->SetData("拎东西", maskItem);
        pBodyTake->move(xpos, ypos + yindex * 50);
        yindex++;
    
        ui.pBtBodySerch->move(ui.pBtBodySerch->x(), ypos + yindex * 50);
        ui.pBtBodyReset->move(ui.pBtBodyReset->x(), ypos + yindex * 50);

    如果有多个筛选界面,可以再封装一层。

    自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取: https://www.cnblogs.com/bclshuai/p/11380657.html
  • 相关阅读:
    Tensorflow CIFAR10 (二分类)
    2018年阿里巴巴重要开源项目汇总
    环境变量备份
    ubuntu Qt5 librealsense opencv
    ubuntu16.04 qt opencv3.4
    时间作为文件名
    ubuntu16.04 安装opencv3
    Visual studio 2015/2017 opencv3.4 kineck2.0 osg realsense配置
    开源监控系统整合Nagios+Cacti+Nconf详解
    nagios系列(八)之nagios通过nsclient监控windows主机
  • 原文地址:https://www.cnblogs.com/bclshuai/p/14345842.html
Copyright © 2011-2022 走看看