parsecer
parsecer

Reputation: 5106

The difference between isDown() isChecked()

What's the difference? I have code which makes the button both checked and down (pushed). Having done these things separately on the same button I have not noticed a difference. The visual effect is the same (it becomes blue on windows and stays like this after the mouse is unclicked).

Also, I have another question concerning checking buttons. Suppose I have one button and at the moment it's not in the group1 (it's commented out) and is connected to SLOT which makes it downed. There is a text area in the same window, so when I press the button it changes it's name on "checked" and is blue. So when I put a cursor into the text area to type something, it's still blue, checked. But if I click on it again, it becomes "unchecked" but still blue. But if after having done that I again type something in my text area, the button is white, not down and still "unchecked".

So I conclude from this, that you can change button's state simply by clicking on it, without even using setChecked(true/false) in code?

But then comes another thing. If I uncomment the two lines with group1 in code and add the button1 in the grou, I suddenly lose the ability to check/uncheck the button through the mouse click. It stays "checked" all the time and I guess the only way to change it is via code. Why does this happen?

   Window5::Window5(QWidget * parent) :QWidget(parent)
  {
    QPushButton * button1=new QPushButton("button1",this);
    connect(button1, SIGNAL(clicked()), this, SLOT(make_pushed()));

    //QButtonGroup * group1=new QButtonGroup(); //currently not in the group
  // group1->addButton(button1);

    QLineEdit * line_area=new QLineEdit(this);
    line_area->setGeometry(500,500,70,20);
    button1->setCheckable(true);
   }

void Window5::make_pushed()
   {


QObject* sender = QObject::sender();
QPushButton* button = qobject_cast<QPushButton*>(sender);
button->setDown(true);

if (button->isChecked())
{
    button->setText("checked");
}
else
{
    button->setText("unchecked");
}

}

Upvotes: 1

Views: 787

Answers (1)

Francesco Argese
Francesco Argese

Reputation: 656

The difference between isDown() and isChecked() is as follows. When the user clicks a button to check it, the button is first pressed then released into the checked state.

When the user clicks it again (to uncheck it), the button moves first to the pressed state, then to the unchecked state (isChecked() and isDown() are both false).

More details on official Qt documentation of QAbstractButton.

Upvotes: 3

Related Questions