赞助

Qt 自定义QToolButton 自己互斥同时工具按钮之间实现互斥

工作中没有小事:点石成金,滴水成河,只有认真对待自己所做的一切事情,才能克服万难,取得成功。

一、自定义QToolButton

  QEditToolButton.h

class QEditToolButton : public QToolButton
{
    Q_OBJECT
public:
    QEditToolButton(QWidget* parent);
    ~QEditToolButton();

    void SetToolName(const std::string& sEditToolName);


public Q_SLOTS:
    void OnSlotTooButtonChanged(std::string& sEditToolName);
    void OnSlotResetTooButton(std::string& sEditToolName);

private:
    //! Tool associated with this button.
    std::string m_ToolName;

};

QEditToolButton.cpp

#include "QEditToolButton.h"

QEditToolButton::QEditToolButton(QWidget* parent)
    : QToolButton(parent)
{
    setCheckable(true);
}

QEditToolButton::~QEditToolButton()
{

}

void QEditToolButton::SetToolName(const std::string& sEditToolName)
{
    m_ToolName = sEditToolName;
}


void QEditToolButton::OnSlotTooButtonChanged(std::string& sEditToolName)
{
    if (sEditToolName != m_ToolName)
    {
        if (isChecked())
            setChecked(false);
    }
    else
    {
        if (!isChecked())
            setChecked(true);
    }
}

void QEditToolButton::OnSlotResetTooButton(std::string& sEditToolName)
{
    if (sEditToolName == m_ToolName)
    {
        if (isChecked())
            setChecked(false);
        return;
    }
}

二、使用自定义QToolButton

class QMyWidget : public QWidget
{
    Q_OBJECT
public:
    QMyWidget(QWidget *parent = Q_NULLPTR);
signals:
    void signalToolButtonChanged(std::string& strName);
public slots:
    void OnSlotTool1(bool bChecked);
    void OnSlotTool2(bool bChecked);
    void OnSlotTool3(bool bChecked);
};

 

#include "QMyWidget.h"
#include <QToolButton>
#include <QHBoxLayout>
#include "QEditToolButton.h"

QMyWidget::QMyWidget(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(400, 600);
    const QSize btnSize(90, 40);
    QHBoxLayout* pHBox = new QHBoxLayout();
    QEditToolButton* pToolBtn1 = new QEditToolButton(this);
    pToolBtn1->setObjectName("test1");
    pToolBtn1->setText(QStringLiteral("测试1"));
    pToolBtn1->setCheckable(true);
    pToolBtn1->setFixedSize(btnSize);
    pToolBtn1->SetToolName("test1");

    QEditToolButton* pToolBtn2 = new QEditToolButton(this);
    pToolBtn2->setObjectName("test2");
    pToolBtn2->setText(QStringLiteral("测试2"));
    pToolBtn2->setCheckable(true);
    pToolBtn2->setFixedSize(btnSize);
    pToolBtn2->SetToolName("test2");

    QEditToolButton* pToolBtn3 = new QEditToolButton(this);
    pToolBtn3->setObjectName("test3");
    pToolBtn3->setText(QStringLiteral("测试3"));
    pToolBtn3->setCheckable(true);
    pToolBtn3->setFixedSize(btnSize);
    pToolBtn3->SetToolName("test3");

    pHBox->addWidget(pToolBtn1);
    pHBox->addWidget(pToolBtn2);
    pHBox->addWidget(pToolBtn3);
    setLayout(pHBox);


    connect(pToolBtn1, &QToolButton::clicked, this, &QMyWidget::OnSlotTool1);
    connect(pToolBtn2, &QToolButton::clicked, this, &QMyWidget::OnSlotTool2);
    connect(pToolBtn3, &QToolButton::clicked, this, &QMyWidget::OnSlotTool3);

    connect(this, &QMyWidget::signalToolButtonChanged, pToolBtn1, &QEditToolButton::OnSlotTooButtonChanged);
    connect(this, &QMyWidget::signalToolButtonChanged, pToolBtn2, &QEditToolButton::OnSlotTooButtonChanged);
    connect(this, &QMyWidget::signalToolButtonChanged, pToolBtn3, &QEditToolButton::OnSlotTooButtonChanged);
}

void QMyWidget::OnSlotTool1(bool bChecked)
{
    if (bChecked)
    {
        QEditToolButton* pToolBtn = (QEditToolButton*)sender();
        if (pToolBtn)
        {
            QString qstrname = pToolBtn->objectName();
            std::string strname = qstrname.toLocal8Bit();
            emit signalToolButtonChanged(strname);
        }
    }
}

void QMyWidget::OnSlotTool2(bool bChecked)
{
    if (bChecked)
    {
        QEditToolButton* pToolBtn = (QEditToolButton*)sender();
        if (pToolBtn)
        {
            QString qstrname = pToolBtn->objectName();
            std::string strname = qstrname.toLocal8Bit();
            emit signalToolButtonChanged(strname);
        }
    }
}

void QMyWidget::OnSlotTool3(bool bChecked)
{
    if (bChecked)
    {
        QEditToolButton* pToolBtn = (QEditToolButton*)sender();
        if (pToolBtn)
        {
            QString qstrname = pToolBtn->objectName();
            std::string strname = qstrname.toLocal8Bit();
            emit signalToolButtonChanged(strname);
        }
    }
}

效果:

 

posted @ 2022-01-29 22:58  车臣  阅读(847)  评论(0编辑  收藏  举报