Tips - How to sync several QSplitter



  • Hello everyone,
    Today I'm delivering a small tip, taht allows me to sync two horizontal QSplitter.
    So let's begin by the problem, QSplitter can emit a signal when is moved : QSplitter::splitterMoved(int, int)
    But we don't have a method that allow us to set the new position, QSplitter:moveSplitter(int,int) is protected.

    So let's write a new class and call it like you want, in my case QsplitterExt

    #ifndef QSPLITTEREXT_H
    #define QSPLITTEREXT_H
    
    #include <QSplitter>
    
    class QSplitterExt : public QSplitter
    {
        Q_OBJECT
    
    public slots:
        void moveSplitter(int pos, int index)
        {
            if( index >= sizes().size() )
            {
                return;
            }
            bool oldState = blockSignals(true);
            QSplitter::moveSplitter(pos, index);
            blockSignals(oldState);
        }
    };
    
    #endif // QSPLITTEREXT_H
    

    Now we got an external slot to receive a signal.

    Let connect splitters:

    QSplitterExt* h1Splitter = new QSplitterExt;
    QSplitterExt* h2Splitter = new QSplitterExt;
    connect(h1Splitter,SIGNAL(splitterMoved(int, int)),h2Splitter,SLOT(moveSplitter(int,int)));
    connect(h2Splitter,SIGNAL(splitterMoved(int, int)),h1Splitter,SLOT(moveSplitter(int,int)));
    

    It work like a charm!

    Best regard!
    Romain


Log in to reply
 

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