查看完整版本: [-- 关于QCheckBox勾选无效的问题 --]

QTCN开发网 -> Qt基础编程 -> 关于QCheckBox勾选无效的问题 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

lwei24 2021-01-27 16:37

关于QCheckBox勾选无效的问题

各位大神,在使用QCheckBox控件時,碰到這樣的問題:定義了4個複選框,但最後一個複選框可以勾選。代碼如下:

    m_checkBoxTip1 = new QCheckBox(this);
    m_checkBoxTip1->setCheckable(true);
    m_checkBoxTip1->resize(this->width()*30/100, this->height()*20/100);
    m_checkBoxTip1->move(this->width()*10/100, this->height()*52/100);
    connect(m_checkBoxTip1, SIGNAL(stateChanged(int)), this, SLOT(slotBtnClickedCheckBox(int)));

    m_labelTip1 = new QLabel(QString("注意1"), this);
    m_labelTip1->setAlignment(Qt::AlignLeft);
    m_labelTip1->setStyleSheet("QLabel{border-radius:0px;background-repeat:no-repeat;border:none;color:black;font:12px ""SimHei"";}");
    m_labelTip1->resize(this->width()*30/100, this->height()*20/100);
    m_labelTip1->move(this->width()*15/100, this->height()*60/100);

    m_checkBoxTip2 = new QCheckBox(this);
    m_checkBoxTip2->resize(this->width()*30/100, this->height()*20/100);
    m_checkBoxTip2->move(this->width()*10/100, this->height()*57/100);

    m_labelTip2 = new QLabel(QString("注意2"), this);
    m_labelTip2->setAlignment(Qt::AlignLeft);
    m_labelTip2->setStyleSheet("QLabel{border-radius:0px;background-repeat:no-repeat;border:none;color:black;font:12px ""SimHei"";}");
    m_labelTip2->resize(this->width()*70/100, this->height()*20/100);
    m_labelTip2->move(this->width()*15/100, this->height()*65/100);

    m_checkBoxTip3 = new QCheckBox(this);
    m_checkBoxTip3->resize(this->width()*30/100, this->height()*20/100);
    m_checkBoxTip3->move(this->width()*10/100, this->height()*62/100);

    m_labelTip3 = new QLabel(QString("注意3"), this);
    m_labelTip3->setAlignment(Qt::AlignLeft);
    m_labelTip3->setStyleSheet("QLabel{border-radius:0px;background-repeat:no-repeat;border:none;color:black;font:12px ""SimHei"";}");
    m_labelTip3->resize(this->width()*70/100, this->height()*20/100);
    m_labelTip3->move(this->width()*15/100, this->height()*70/100);

    m_checkBoxTip4 = new QCheckBox(this);
    m_checkBoxTip4->resize(this->width()*30/100, this->height()*20/100);
    m_checkBoxTip4->move(this->width()*10/100, this->height()*67/100);

    m_labelTip4 = new QLabel(QString("注意4"), this);
    m_labelTip4->setAlignment(Qt::AlignLeft);
    m_labelTip4->setStyleSheet("QLabel{border-radius:0px;background-repeat:no-repeat;border:none;color:black;font:12px ""SimHei"";}");
    m_labelTip4->resize(this->width()*85/100, this->height()*20/100);
    m_labelTip4->move(this->width()*15/100, this->height()*75/100);

    m_btnCheckBoxGroup = new QButtonGroup;
    m_btnCheckBoxGroup->setExclusive(true);//設置互斥
    m_btnCheckBoxGroup->addButton(m_checkBoxTip1,1);
    m_btnCheckBoxGroup->addButton(m_checkBoxTip2,2);
    m_btnCheckBoxGroup->addButton(m_checkBoxTip3,3);
    m_btnCheckBoxGroup->addButton(m_checkBoxTip4,4);
//如果不用佈局,只能勾選最後一個複選框。如果使用以下布局代码,就可以勾选所有复选框。但之前设置空间的位置又会因为布局而打乱了。

    QVBoxLayout *pMainLayout = new QVBoxLayout;

    pMainLayout->addWidget(m_checkBoxTip1);
    pMainLayout->addWidget(m_checkBoxTip2);
    pMainLayout->addWidget(m_checkBoxTip3);
    pMainLayout->addWidget(m_checkBoxTip4);
    setLayout(pMainLayout);

以上所有的代碼都是在QDialog的構造函數中。請教一下:有没有办法,早保持原有设置空间的位置情况下,做到复选框可以勾选所有的,而不是只勾选最后一个呢?


yuyu414 2021-01-28 13:14
你都知道设置互斥了,自然是只能勾选一个

lwei24 2021-01-28 14:47
yuyu414:你都知道设置互斥了,自然是只能勾选一个 (2021-01-28 13:14) 

刚开始没有设置互斥,定义了四个复选框,但只能选择最后一个。


查看完整版本: [-- 关于QCheckBox勾选无效的问题 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled