QSplitter becoming undistinguishable between QWidget and QTabWidget

前端 未结 7 1263
庸人自扰
庸人自扰 2021-02-04 06:30

I am puting a QWidget and a QTabWidget next to each other in one horisontal splitter. And the splitter loses it\'s shape, you can know that there is a splitter only by hovering

7条回答
  •  忘掉有多难
    2021-02-04 07:08

    Call splitter_handles {} with the QSplitter you want to add handles to:

    #include "splitter_handles.h"
    ...
    QSplitter spl {};
    ... // widgets added to 'spl'
    plitter_handles {spl}; // adding handles
    ...
    

    Result:

    splitter_handles.h

    #ifndef SPLITTER_HANDLES_H
    #define SPLITTER_HANDLES_H
    
    #include 
    #include 
    #include 
    
    class handle : public QWidget
    {
        Q_OBJECT
    
    protected:
        void paintEvent(QPaintEvent *e) {
            Q_UNUSED(e);
            QPainter painter {this};
            painter.setPen(Qt::NoPen);
            painter.setBrush(Qt::Dense4Pattern);
            painter.drawRect(this->rect());
        }
    };
    
    class splitter_handles {
    public:
        splitter_handles(QSplitter * spl) {
            const int width {7};
            spl->setHandleWidth(width);
            for(int h_idx {1}; h_idx < spl->count(); h_idx++) {
                auto l_handle {new handle {}};
                l_handle->setMaximumSize(width*2, width*2);
    
                auto layout {new QHBoxLayout {spl->handle(h_idx)}};
                layout->setSpacing(0);
                layout->setMargin(1);
                layout->addWidget(l_handle);
            }
        }
    };
    
    #endif // SPLITTER_HANDLES_H
    

    main.c

    #include 
    #include 
    #include 
    #include 
    
    #include "splitter_handles.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        auto spl_v {new QSplitter {Qt::Vertical}};
        spl_v->addWidget(new QGroupBox {"box 1"});
        spl_v->addWidget(new QGroupBox {"box 2"});
        spl_v->addWidget(new QGroupBox {"box 3"});
        splitter_handles {spl_v}; // set handles
    
        auto wdg  {new QWidget {}};
        auto v_lt {new QVBoxLayout {wdg}};
        v_lt->addWidget(spl_v);
        v_lt->setMargin(0);
    
        auto spl_h {new QSplitter {}};
        spl_h->addWidget(wdg);
        spl_h->addWidget(new QGroupBox {"box 4"});
        spl_h->addWidget(new QGroupBox {"box 5"});
        splitter_handles {spl_h};
    
        auto h_lt {new QHBoxLayout {}};
        h_lt->addWidget(spl_h);
    
        QWidget w {};
        w.setLayout(h_lt);
        w.setGeometry(100,100,500,300);
        w.show();
    
        return a.exec();
    }
    

提交回复
热议问题