[Interest] QActionGroup vs QButtonGroup

Sensei senseiwa at gmail.com
Mon Mar 11 09:55:12 CET 2013


Hi all,

I am adding to a toolbar a new set of buttons, checkable and exclusive, 
but I found a strange behavior.

Using a QButtonGroup, buttons are added but NO ICON is showed, while 
employing the very same icons in a QActionGroup, everything works just fine.

I must add, the code using a QButtonGroup works *perfectly* on another 
project, where I create a custom widget and add a new toolbar there 
using a v-layout.

Any hints? I am under OSX 10.8, Qt SDK 4.8.1.

Thanks & Cheers!


// Setup toolbar actions on the QMainWindow
void hMainWindow::setupToolbar()
{
     QAction *paction;

     // Init toolbar
     toolbar_ = new QToolBar(tr("Tools"), this);

     // Basic toolbar options
     toolbar_->setMovable(false);
     toolbar_->setFloatable(false);
     toolbar_->setIconSize(QSize(16, 16));
     toolbar_->setToolButtonStyle(Qt::ToolButtonIconOnly);

     // Add the toolbar
     addToolBar(toolbar_);

     // Create view icons and texts
     consoleIcon_   = new QIcon(":/images/console.png");
     consoleString_ = tr("Change view to console view.");
     view3dIcon_    = new QIcon(":/images/view3d.png");
     view3dString_  = tr("Change view to 3D view.");

     // Add change view icon
     toggleAction_ = new QAction(this);
     toggleAction_->setIcon(*consoleIcon_);
     toggleAction_->setCheckable(true);
     toggleAction_->setStatusTip(consoleString_);
     connect(toggleAction_, SIGNAL(toggled(bool)), this, 
SLOT(changeView(bool)));
     toolbar_->addAction(toggleAction_);
     toolbarActions_.push_back(toggleAction_);

     toolbar_->addSeparator();

     // Add an exclusive action group
     QActionGroup *g = new QActionGroup(this);
     g->setExclusive(true);
     QAction* a1 = new QAction(QIcon(":/images/console.png"), "Hello", 
this);
     QAction* a2 = new QAction(QIcon(":/images/view3d.png"), "Hello", this);
     QAction* a3 = new QAction(QIcon(":/images/console.png"), "Hello", 
this);
     QAction* a4 = new QAction(QIcon(":/images/view3d.png"), "Hello", this);
     a1->setCheckable(true);
     a2->setCheckable(true);
     a3->setCheckable(true);
     a4->setCheckable(true);
     a1->setChecked(true);
     g->addAction(a1);
     g->addAction(a2);
     g->addAction(a3);
     g->addAction(a4);
     toolbar_->addAction(a1);
     toolbar_->addAction(a2);
     toolbar_->addAction(a3);
     toolbar_->addAction(a4);

     toolbar_->addSeparator();

     QButtonGroup* buttons_ = new QButtonGroup(toolbar_);
     buttons_->setExclusive(true);
     QIcon* icnProject_  = new QIcon(":/icons/console.png");
     QIcon* icnSymbols_  = new QIcon(":/icons/view3d.png");
     QIcon* icnFind_     = new QIcon(":/icons/console.png");
     QIcon* icnProblems_ = new QIcon(":/icons/view3d.png");
     QIcon* icnLog_      = new QIcon(":/icons/console.png");
     QToolButton* navProject_  = new QToolButton(this);
     QToolButton* navSymbols_  = new QToolButton(this);
     QToolButton* navFind_     = new QToolButton(this);
     QToolButton* navProblems_ = new QToolButton(this);
     QToolButton* navLog_      = new QToolButton(this);
     navProject_->setIcon(*icnProject_);
     navSymbols_->setIcon(*icnSymbols_);
     navFind_->setIcon(*icnFind_);
     navProblems_->setIcon(*icnProblems_);
     navLog_->setIcon(*icnLog_);
     navProject_->setCheckable(true);
     navSymbols_->setCheckable(true);
     navFind_->setCheckable(true);
     navProblems_->setCheckable(true);
     navLog_->setCheckable(true);
     navProject_->setChecked(true);
     buttons_->addButton(navProject_, 0);
     buttons_->addButton(navSymbols_, 1);
     buttons_->addButton(navFind_, 2);
     buttons_->addButton(navProblems_, 3);
     buttons_->addButton(navLog_, 4);
     toolbar_->addWidget(navProject_);
     toolbar_->addWidget(navSymbols_);
     toolbar_->addWidget(navFind_);
     toolbar_->addWidget(navProblems_);
     toolbar_->addWidget(navLog_);
}



More information about the Interest mailing list