[Solved] QToolBox title click events? Cycling through QToolBox pages?



  • Hello,

    I am hoping to implement a behaviour in QToolBox so that when you click on the titlebar of the top page it will cycle through the other pages, but there doesn't seem to be an event called when this happens. Does anyone have a suggestion for a work around or solution?

    The background is that I have a QToolBox in a QDockWidget on the right side of the window, which is usually maximized. If you change the page in the QToolBox, you have to send the mouse all the way to the bottom of the screen to get that page back, and then move your mouse all the way back up to the top of the screen to access the page, so the user ends up having to traverse the entire height of the screen twice.

    Here is an animated gif demonstrating the behaviour I am talking about in a quick demo project:

    http://i.imgur.com/YkeOZCp.gif

    See how switching between pages requires the user to traverse twice the height of the screen with the mouse?

    I am hoping that I can trigger the pages of the QToolBox to cycle by clicking repeatedly on the top-most page's toolBar.

    Cheers,

    Bob.

    Edit: Thanks to the suggestion below this functionality is now working. The code is added in a reply below. Here is a gif of it in action.

    !http://i.imgur.com/QNl8Fjy.gif(QToolBox cycling)!


  • Moderators

    i think you can only do some dirty "hacking".

    That should work to get the buttons (titles to click):
    @
    foreach( QWidget* w, toolBox->findChildren<QWidget*>() )
    {
    if( w->inherits("QToolBoxButton") )
    {
    QAbstractButton* button = qobject_cast<QAbstractButton*>(w);
    ...
    }
    }
    @
    Of course you need to redo this when you add/remove a page from the QToolBox widget.

    But the problem is how you do the mapping of the buttons to the indexes. I currently don't have an idea how to do this though.

    Maybe this helps you heading into the right direction to solve your problem.



  • Hi, thanks for the suggestion, and it gives me a direction to pursue.

    What I have done is add a single page to the toolBox, and gotten a pointer to the QAbstractButton on this page, as you have suggested, and then I connect a SLOT to the clicked() signal coming from the Button, and it gets called when the Title button is clicked, great.

    Now I am experiencing a very strange problem:

    From this SLOT that is called when the clicked() signal is sent, ui->toolBox->currentIndex() is always 0, no matter which page is active.

    If I call ui->toolBox->currentIndex() from other slots/actions in the mainWindow class, currentIndex() returns the correct page index. But from the SLOT that I have attached to the clicked() event, it is always 0.

    I have setup multiple qDebug() statements, and breakpoints to ensure that the currentIndex() is not 0 when a different page is selected and currentIndex() is called from different functions/actions inside the QMainWindow class (which owns ui and the QToolBox).

    From within this SLOT in question, the QToolBox::count() method returns the correct number of pages.

    I am extremely puzzled by this behaviour, and I've tried in both Qt 5.1.1, and Qt 5.2.0 beta.

    Any suggestions?

    Thanks again for pointing me in this direction of querying the children of the QToolBox

    Cheers,

    Bob



  • Ok, I got it. It took me longer than I care to admit. I read the code for QToolBox and everytime the button is clicked in the QToolBox, it switches to that page. So, what was happening in my example above, was every click to the top button was setting it to that page, so no wonder the currentIndex was always 0.

    What I ended up doing was connecting to the QToolBox::currentChanged(int) signal, and the QAbstractButton::clicked signal()

    The code looks like this:

    @ connect(ui->toolBox,SIGNAL(currentChanged(int)),this,SLOT(toolBoxChanged(int)));

    foreach( QWidget* w, ui->toolBox->findChildren<QWidget*>() )
    {
    if( w->inherits("QToolBoxButton") )
    {
    QAbstractButton* button = qobject_cast<QAbstractButton*>(w);
    connect(button,SIGNAL(clicked(bool)), this, SLOT(toolBoxTitleClicked(bool)));
    }
    }

    void MainWindow::toolBoxChanged(int index)
    {
    m_toolBarChanged = true;
    }

    void MainWindow::toolBoxTitleClicked(bool checked)
    {
    if (m_toolBarChanged) {
    m_toolBarChanged = false;
    return;
    }

    ui->toolBox->setCurrentIndex(1); // hard coded page number for the example
    }
    @

    and this works!

    Thanks for the suggestion about getting the QAbstractButton from the QToolBox,

    Cheers,

    Bob


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.