QPushButton setAutoExclusive(true) setChecked(false)



  • If i set three buttons on one common widget to
    @ setCheckable(true)
    setAutoExclusive(true)
    @
    i can't set ALL buttons to unchecked state by
    @ setChecked(false)
    @
    The only workaround i found ist to set the buttons
    @ setCheckable(false)
    setCheckable(true)
    @
    for all buttons, to reset in initial, all unchecked state.

    Is this a bug?

    ReHa1



  • This is the expected behavior. When the autoExclusive property is enabled, checkable buttons that belong to the same parent widget behave as if they were part of the same exclusive button group. In an exclusive group, the user cannot uncheck the currently checked button by clicking on it; instead, another button in the group must be clicked to set the new checked button for that group. The same is true when unchecking the button programmatically. The following links to the documentation contain some information on this:

    http://doc.qt.nokia.com/4.7/qabstractbutton.html#autoExclusive-prop
    http://doc.qt.nokia.com/4.7/qbuttongroup.html#exclusive-prop



  • Ok, but the initial situation (bevor a user has clicked at one of the button group) doesn't follow this definition: In this situation NO button is checked.

    How to restore this situation correct, programmatically?



  • In the initial situation, no button has been clicked/checked yet and hence none are set as checked at this point. Once a button has been checked however, there will always be one button that is checked when using auto-exclusive. If you wish to get around this, you can e.g enable and disable the autoexclusive property for the currently checked button, i.e as illustrated here:

    @#include <QtGui>
    class Widget : public QWidget
    {
    Q_OBJECT
    public:
    Widget()
    {
    QVBoxLayout *layout = new QVBoxLayout(this);
    button1 = new QPushButton("First", this);
    button2 = new QPushButton("Second", this);
    button3 = new QPushButton("Third", this);
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);

    button1->setCheckable(true);
    button1->setAutoExclusive(true);

    button2->setCheckable(true);
    button2->setAutoExclusive(true);

    button3->setCheckable(true);
    button3->setAutoExclusive(true);
    button3->setChecked(true);
    QTimer::singleShot(1000, this, SLOT(testSlot()));

    }
    public slots:
    void testSlot()
    {
    button1->setChecked(false);
    button2->setChecked(false);
    button3->setAutoExclusive(false);
    button3->setChecked(false);
    button3->setAutoExclusive(true);
    }
    private:
    QPushButton *button1;
    QPushButton button2;
    QPushButton button3;
    };
    #include "main.moc"
    int main(int argc, char
    argv)
    {
    QApplication app(argc, argv);
    Widget window;
    window.show();
    return app.exec();
    }@



  • Maybe you are looking something like this. It is a partial code I used for drawing figures on a canvas.

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    QToolButton* drawLineButton; //Better declare it on header
    QToolButton* drawEllipseButton; //Better declare it on header
    QToolButton* drawPolygonButton; //Better declare it on header

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QToolBar* newToolBar = new QToolBar(this);
    newToolBar->setObjectName(QString::fromUtf8("newToolBar"));
    addToolBar(Qt::TopToolBarArea, newToolBar);
    
    drawLineButton = new QToolButton(this);
    drawLineButton->setCheckable(true);
    drawLineButton->setToolTip("Draw Line");
    
    drawEllipseButton = new QToolButton(this);
    drawEllipseButton->setCheckable(true);
    drawEllipseButton->setToolTip("Draw Ellipse");
    
    drawPolygonButton = new QToolButton(this);
    drawPolygonButton->setCheckable(true);
    drawPolygonButton->setToolTip("Draw Polygon");
    
    ui->mainToolBar->addWidget(drawLineButton);
    ui->mainToolBar->addWidget(drawEllipseButton);
    ui->mainToolBar->addWidget(drawPolygonButton);
    
    connect(drawLineButton, SIGNAL(toggled(bool)), this, SLOT(drawLineButtonToggled(bool)));
    connect(drawEllipseButton, SIGNAL(toggled(bool)), this, SLOT(drawEllipseButtonToggled(bool)));
    connect(drawPolygonButton, SIGNAL(toggled(bool)), this, SLOT(drawPolygonButtonToggled(bool)));
    connect(drawLineButton, SIGNAL(pressed()), this, SLOT(drawLineButtonPressed()));
    connect(drawEllipseButton, SIGNAL(pressed()), this, SLOT(drawEllipseButtonPressed()));
    connect(drawPolygonButton, SIGNAL(pressed()), this, SLOT(drawPolygonButtonPressed()));
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::drawLineButtonToggled(bool in_Checked)
    {
    if (in_Checked)
    {
    // Draw the line
    }
    else
    {
    // Draw nothing
    }
    }

    void MainWindow::drawEllipseButtonToggled(bool in_Checked)
    {
    if (in_Checked)
    {
    // Draw the ellipse
    }
    else
    {
    // Draw nothing
    }
    }

    void MainWindow::drawPolygonButtonToggled(bool in_Checked)
    {
    if (in_Checked)
    {
    // Draw the polygon
    }
    else
    {
    // Draw nothing
    }
    }

    void MainWindow::drawLineButtonPressed()
    {
    if (drawLineButton->isChecked())
    {
    drawLineButton->setAutoExclusive(false);
    }
    else
    {
    drawLineButton->setAutoExclusive(true);
    }
    }

    void MainWindow::drawEllipseButtonPressed()
    {
    if (drawEllipseButton->isChecked())
    {
    drawEllipseButton->setAutoExclusive(false);
    }
    else
    {
    drawEllipseButton->setAutoExclusive(true);
    }
    }

    void MainWindow::drawPolygonButtonPressed()
    {
    if (drawPolygonButton->isChecked())
    {
    drawPolygonButton->setAutoExclusive(false);
    }
    else
    {
    drawPolygonButton->setAutoExclusive(true);
    }
    }
    @



  • A better way to unpress the buttons would be creating a unique slot connected to all pressed signals.

    it would look like this:

    void MainWindow::drawButtonPressed()
    {
       QAbstractButton* button = static_cast<QAbstractButton*>(sender())
       if (button->isChecked()) {
          button->setAutoExclusive(false);
       } else {
          button->setAutoExclusive(true);
       }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.