[Qt-interest] Change Background color of Qaction.Qt, GUI, QAction, Menu, QToolBar

Irfan Omair irfan.omair.qt at gmail.com
Wed Aug 4 20:06:32 CEST 2010


Harris, It is Good idea to care for those who filters on some key word.

Actually There are a couple of ways you can do this.
 Either you can use a QWidgetAction in which case you can give it the widget
yourself and as
such just set the icon you want.
Otherwise you can subclass the ToolBar and check when a new button is
added and then query for the newly created button.

I have just written an example for you which I m sure will help you, have a
look:

class ToolBar : public QToolBar
{
    Q_OBJECT
public:
    ToolBar(const QString &title, QWidget *parent = 0)
        : QToolBar(title, parent)
    {
        extensionButton = qFindChild<QToolButton*>(this,
"qt_toolbar_ext_button");
    }
    void actionEvent(QActionEvent *e)
    {
        QToolBar::actionEvent(e);
        if (e->type() == QEvent::ActionAdded) {
            QSet<QToolButton*> buttons =
qFindChildren<QToolButton*>(this).toSet();
            buttons.remove(extensionButton);
            Q_ASSERT(buttons.size() - 1 == actions.size());
            QSet<QToolButton*> added = buttons - actions.values().toSet();
            Q_ASSERT(added.size() == 1);
            actions[e->action()] = *added.begin();
        } else if (e->type() == QEvent::ActionRemoved) {
            actions.remove(e->action());
        }
    }
    QToolButton *button(QAction *action) const
    {
        return actions.value(action);
    }
private:
    QMap<QAction *, QToolButton*> actions;
    QToolButton *extensionButton;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0)
        : QMainWindow(parent)
    {
        addToolBar(tb = new ToolBar("foo", this));
        one = tb->addAction("One");
        two = tb->addAction("Two");
        three = tb->addAction("Three");
        setCentralWidget(new QTextEdit(this));
        tb->button(one)->setStyleSheet("background: red");
        tb->button(two)->setStyleSheet("background: blue");
        tb->button(three)->setStyleSheet("background: green");
    }
public slots:
private:
    ToolBar *tb;
    QAction *one, *two, *three;
};
#include "main.moc"

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


Regards,

Irfan Omair
Qt Developer


On Wed, Aug 4, 2010 at 10:59 AM, Efan... <efanharris at gmail.com> wrote:

> made change in subject line for those who filter on Tags
>
> On Wed, Aug 4, 2010 at 10:51 AM, Efan... <efanharris at gmail.com> wrote:
>
>> How do I change a background color of a QAction? I created a toolbar and
>> added several actions into it using the method:QAction discoverAction
>> =toolBar->addAction(QIcon(":/Resources/search.png"), "Discover"); It appears
>> that all actions that I added are displayed as toolButtons
>> within the toolbar. How do I modify the background color of it? I would
>> like to highlight my discoverAction, but I do not know how to do
>> this. It does not appear that this uses a palette. I can modify the
>> background of the toolbar itself, but not individual buttons.
>>
>> --
>> Harris
>>
>
>
>
>
Irfan Omair
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100804/b43df1e0/attachment.html 


More information about the Qt-interest-old mailing list