2016-01-23 27 views
5

在我的Qt程序中,我想捕獲其中一個複選框的信號以知道它是否被選中。我做到了:Qt捕獲複選框的信號以執行操作

#include <iostream> 
#include <QVBoxLayout> 
#include <QtGui> 
#include "Window.h" 

Window::Window() : QWidget() 
{ 
    setFixedSize(400, 800); 

    m_bouton = new QPushButton("Module 1"); 
    m_bouton->setCursor(Qt::PointingHandCursor); 

    m_bouton2 = new QPushButton("Module 2"); 
    m_bouton2->setCursor(Qt::PointingHandCursor); 

    m_bouton3 = new QPushButton("Module 3"); 
    m_bouton3->setCursor(Qt::PointingHandCursor); 

    m_bouton4 = new QPushButton("Module 4"); 
    m_bouton4->setCursor(Qt::PointingHandCursor); 

    m_check4 = new QCheckBox(this); 
    m_check4->move(300, 50); 
    m_check4->setChecked(true); 

    m_check3 = new QCheckBox(this); 
    m_check3->move(200, 50); 
    m_check3->setChecked(true); 

    m_check2 = new QCheckBox(this); 
    m_check2->move(100, 50); 
    m_check2->setChecked(true); 

    m_check1 = new QCheckBox(this); 
    m_check1->move(0, 50); 
    m_check1->setChecked(true); 

    QVBoxLayout *layout = new QVBoxLayout; 
    layout->addWidget(m_bouton); 
    layout->addWidget(m_bouton2); 
    layout->addWidget(m_bouton3); 
    layout->addWidget(m_bouton4); 
    this->setLayout(layout); 
    this->setStyleSheet("border: 1px solid black; border-radius: 10px;"); 

    this->setWindowOpacity(0.5); 
    QWidget::setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); 
    QWidget::setWindowTitle("Monitor Core"); 
    QObject::connect(m_bouton4, SIGNAL(clicked()), this, SLOT(SizeCHange())); 
    QObject::connect(m_check4, SIGNAL(clicked()), this, SLOT(SizeCHange())); 
} 

void Window::SizeCHange() 
{ 
    setFixedSize(400, 800 - 200); 
    m_bouton4->setVisible(false); 
} 

,這是我的.h:

#ifndef _WINDOW_H_ 
#define _WINDOW_H_ 

#include <QApplication> 
#include <QWidget> 
#include <QPushButton> 
#include <QCheckBox> 

class Window : public QWidget 
{ 
    Q_OBJECT 

public: 
    Window(); 

    public slots: 
    void SizeCHange(); 

private: 
    QPushButton *m_bouton; 
    QPushButton *m_bouton2; 
    QPushButton *m_bouton3; 
    QPushButton *m_bouton4; 
    QCheckBox *m_check1; 
    QCheckBox *m_check2; 
    QCheckBox *m_check3; 
    QCheckBox *m_check4; 
}; 

#endif 

這是工作,但只是一個時間。我想知道它是否被檢查,然後我可以調整窗口大小,並根據需要隱藏一些東西。

+0

奇怪的行爲。這段代碼適合我。你可以給一個更大的一段代碼嗎?如何定義m_check4和m_bouton4? –

+0

編輯過的第一篇文章 – Michael003

+0

這段代碼適合我。你做了**重建項目嗎?你使用什麼IDE,OS和Qt版本? –

回答

1

你只需要使用一個布爾參數,它表示的複選框的狀態:

public slots: 
    void SizeCHange(bool checked); //using bool parameter 

//... 
QObject::connect(m_check4, SIGNAL(clicked(bool)), this, SLOT(SizeCHange(bool))); 
//... 
void Window::SizeCHange(bool checked) 
{ 
    if(checked) 
    { 
     setFixedSize(400, 800 - 200); 
    } 
    else 
    { 
     // removing fixed size 
     setMinimumSize(QSize(0, 0)); 
     setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)); 
    } 
    m_bouton4->setVisible(checked); 
} 

The docs for a signal clicked()

+0

這是想法,只有一個小問題,第一次你不選中,它不會將窗口大小調整爲(400,800-200)。而當我們檢查它時,它應該重新調整m_button4並設置它以前的大小 – Michael003