[Interest] QActionGroup vs QButtonGroup

Bill Crocker william.crocker at analog.com
Mon Mar 11 12:47:33 CET 2013


Wow. A non QML posting. How refreshing.

Did you remember to add "/images/console.png" etc. to
your resources file?

Also, QIcon is light weight, so you should use
QIcon and not Pointer-To-QIcon for things like
your consoleIcon_.

Bill

On 03/11/2013 04:55 AM, Sensei wrote:
> 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_);
> }
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>




More information about the Interest mailing list