qt - How do I make a splitter with a set of buttons on it?

2.3k views Asked by At

Did anyone face a problem of putting buttons (or any other widgets) on the top of a splitter handle? I've tried to get the handle, set its layout and put the controls there, but I got some weird look.

My main goal is to create a selector dialog as depicted below:Dialog example

What I get:
enter image description here

Code:

QDialog dialog = new QDialog();
dialog.setLayout(new QVBoxLayout());
QSplitter splitter = new QSplitter();

splitter.setSizePolicy(new QSizePolicy(QSizePolicy.Policy.MinimumExpanding, QSizePolicy.Policy.Fixed));
splitter.addWidget(new QListWidget());
splitter.addWidget(new QTableWidget());
dialog.layout().addWidget(splitter);

QSplitterHandle h = splitter.handle(1);
h.setFixedWidth(30); // I added this line to show the splitter handle
QVBoxLayout lt = new QVBoxLayout();
lt.setSpacing(0);
lt.setMargin(0);
lt.addWidget(new QPushButton("Hello", dialog));
lt.addWidget(new QPushButton("Good bye", dialog));
h.setLayout(lt);

dialog.show();
1

There are 1 answers

6
graphite On BEST ANSWER

This gives better result:

// h.setFixedWidth(30);
splitter.setHandleWidth(30);

For better result I'd suggest to subclass QSplitterHandle and reimplement sizeHint(). Look through QSplitterHandle documentation.

You can change layout setting to get exact button positions. For example:

lt.addStrecth();

To remove "relief"-bar you must subclass from QSplitterHandle and reimplement paintEvent().