5

I want to switch from one QToolButton to another in QToolBar. I have used QStackedWidget, their it is too simple to move from one widget to other but here I am not able to get how to move via using keyReleaseEvent.

mywindow::mywindow() : QMainWindow()
{   
    widget = new QWidget();
    setCentralWidget(widget);

    tool = new QToolBar();
    vertical = new QVBoxLayout();
    button1 = new QToolButton();
    connect( button1, SIGNAL(clicked()), this, SLOT(fileNew()) );

    button2 = new QToolButton();
    button3 = new QToolButton();

    button1->setIcon(QIcon("download.jpg"));
    button1->setGeometry(0,0,100,200);
    button2->setIcon(QIcon("images.jpg"));
    button3->setIcon(QIcon("settings-icon.jpg"));

    //stack1->addWidget(button1);
    //stack1->addWidget(button2);
    //stack1->addWidget(button3);

    tool->addWidget(button1);
    tool->addWidget(button2);
    tool->addWidget(button3);
    //tool->addWidget(stack1);

    vertical->addWidget(tool);
    widget->setLayout(vertical);
}

void mywindow::keyReleaseEvent(KeyEvent *event)
{
    switch(event->key())
    {
        case:Qt::Key_Left:


    }
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
Suresh
  • 745
  • 4
  • 11
  • 25
  • Isn't it easier to add buttons to the tool bar with ` QToolBar::addAction()`? – vahancho Jan 14 '14 at 10:02
  • ok i will add buttons using addAction. But how to navigate from one button to other? – Suresh Jan 14 '14 at 10:09
  • For navigation, or triggering particular button you can assign a shortcut to each of them with `QAction::setShortcut()` function. – vahancho Jan 14 '14 at 10:12
  • I dont want to add shortcut. I want to navigate by using KeyReleaseEvent – Suresh Jan 14 '14 at 10:14
  • Well, than handle key events in the QToolBar or QMainWindow subclass. – vahancho Jan 14 '14 at 10:27
  • That what i am asking. How exactly to navigate while pressing KeyLeft key.. What should i write – Suresh Jan 14 '14 at 10:35
  • Clarify what`s the problem? Do you want to know how to switch between buttons without QStackedWidget? Well, then you could just hide every button except the active one. Or you want to know how to do it while key pressed, or what? – dvvrd Jan 14 '14 at 17:11
  • 1
    It's called focus. e.g. `if (button1->hasFocus()) { button2->setFocus(); }` – Pavel Strakhov Jan 14 '14 at 20:56

1 Answers1

3

You need to check against the focus, and shift that as appropriate. I would write something like this:

void mywindow::keyReleaseEvent(KeyEvent *event)
{
    switch(event->key())
    {
        case:Qt::Key_Left:
            if (button3->hasFocus())
                button2->setFocus();
            else if (button2->hasFocus())
                button1->setFocus();
            break;
        case:Qt::Key_Right:
            if (button1->hasFocus())
                button2->setFocus();
            else if (button2->hasFocus())
                button3->setFocus();
            break;
    }
}

Note that this code can go tedious easily if you keep adding further buttons. I would place them into a container. Then, I would iterate through that container forward and reverse order depending on the focus switching logic.

See the documentation for further details:

focus : const bool

This property holds whether this widget (or its focus proxy) has the keyboard input focus.

By default, this property is false.

Note: Obtaining the value of this property for a widget is effectively equivalent to checking whether QApplication::focusWidget() refers to the widget.

Access functions: bool hasFocus() const

Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Now automatically it is shifting two times when pressing one time Key_left or Key_Right. How to control it? – Suresh Jan 15 '14 at 05:33