Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QPushButton setDown on click

When a QPushButton is clicked, I want it to remain pressed down until clicked again.

void MainWindow::itemClicked(){

    QPushButton *clickedItem = qobject_cast<QPushButton *>(sender());

    qDebug() << clickedItem->isDown();

    if(!clickedItem->isDown())
        clickedItem->setDown(true);
    else
        clickedItem->setDown(false);
}

This doesn't seem to work. It will cause the button to be pressed down indefinitely.

clickedItem->isDown() is always false.

like image 760
Quaxton Hale Avatar asked Sep 28 '26 14:09

Quaxton Hale


1 Answers

isDown always returns false because you are checking it in a slot connected to the clicked signal. The clicked signal is emited when you press down the push button and release it. So every time the button is pressed and released the clicked signal is emited.

setCheckable() would work for you. It will make the button toggle. So when youu click, it'll stay in down state until you click it again.

like image 183
Nejat Avatar answered Oct 01 '26 05:10

Nejat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!