1

I'm trying to group a few QPushButton`s into QButtonGroup with exclusive checking, but after launch i'm still able to check multiple buttons.

// pen toggle button
penB.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
penB.setIconSize(QSize(ICON_SIZE,ICON_SIZE));
penB.setCheckable(true);
penB.toggle();
penB.setIcon(QIcon(":icons/pen.png"));
// circle toggle button
circleB.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
circleB.setIconSize(QSize(ICON_SIZE,ICON_SIZE));
circleB.setCheckable(true);
circleB.setIcon(QIcon(":icons/circle.png"));

figureBox.addButton(&penB);
figureBox.addButton(&circleB);
figureBox.setExclusive(true);
// add buttons to grid
layoutG.addWidget(&openB,1,1);
layoutG.addWidget(&saveB,1,2);
layoutG.addWidget(&penB,1,3);
layoutG.addWidget(&circleB,2,3);

I think, its just need to procees some events. If its true, which events exactly? Thanks in advance.

Ivan
  • 876
  • 1
  • 8
  • 22
  • Can you show how the buttons are created? I suspect they don't have a common parent – tinkertime Nov 18 '14 at 22:32
  • Also, probably best to create youre Qt objects on the heap, as the ownership should be transferred to the parent – tinkertime Nov 18 '14 at 22:33
  • @yankee2905, QPushButton penB; QPushButton circleB; As a private field in .h file of the same class as .cpp code in question – Ivan Nov 18 '14 at 22:37
  • @yankee2905, tried to create in heap - useless. – Ivan Nov 18 '14 at 22:42
  • 1
    I wouldn't call it useless, adding a widget to a layout from your local stack isn't safe – tinkertime Nov 18 '14 at 22:44
  • @yankee2905 If you declare it as member there's nothing unsafe here. – Iuliu Nov 18 '14 at 22:49
  • @yankee2905, thank you, i`ll keep in mind that, but now i need QButtonGroup able to work – Ivan Nov 18 '14 at 22:50
  • @Ivan I think it would be helpful to you to mark an answer as accepted...otherwise next time you ask questions not so many people will be as eager to help you as they are now... – Jacob Krieg Nov 19 '14 at 08:57

2 Answers2

2

I've tried to replicate your example and for me it works OK:

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QButtonGroup>
#include <QGridLayout>

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    QGridLayout layoutG;
    QButtonGroup figureBox;
    QPushButton openB;
    QPushButton saveB;
    QPushButton penB;
    QPushButton circleB;
};

#endif // WIDGET_H

widget.cpp:

#include "widget.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    // pen toggle button
    penB.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    penB.setIconSize(QSize(16, 16));
    penB.setCheckable(true);
    penB.toggle();

    // circle toggle button
    circleB.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    circleB.setIconSize(QSize(16, 16));
    circleB.setCheckable(true);

    figureBox.addButton(&penB);
    figureBox.addButton(&circleB);
    figureBox.setExclusive(true);

    // add buttons to grid
    layoutG.addWidget(&openB,1,1);
    layoutG.addWidget(&saveB,1,2);
    layoutG.addWidget(&penB,1,3);
    layoutG.addWidget(&circleB,2,3);

    setLayout(&layoutG);
}

Widget::~Widget()
{
}

The single thing that could be not ok in your code is that I don't see where you set the layout, but maybe you didn't add the code for simplicity reasons. Another thing is that the only excluding buttons would be penB and circleB.

Check my example, see what you're doing wrong and maybe come back with a feedback.

Iuliu
  • 4,001
  • 19
  • 31
-1

Sorry, guys. Its time to go to the bed. Im forgot to declare QButtonGroup in .h file, it was declared at the local function, so just memory leak.

Ivan
  • 876
  • 1
  • 8
  • 22