Solved Restrict hidding of qsplitterhandle in qsplitter
-
Hello all,
PROBLEM STATMENT :
I wanted to give clickable feature to QSplitterHandle of QSplitter. as I wanted to perform show or hide some of the widgets in QSplitter.
Something like the button between pdf and bookmark in pdf readers, which is drag-able ( to incress/decrees widget size ratio) and clickable too( to show/hide bookmarks) in the below screenshot..
I have extended the QSplitterHandle to add a signal which is emited when handle is clicked (mouseReleaseEvent()). and also extended qsplitter to use above QSplitterHandle in the createHandle() of qsplitter.
Below is the sample code of the same:class MySplitterHandle : public QSplitterHandle { public: explicit MySplitterHandle(Qt::Orientation o, QSplitter *parent); void mouseReleaseEvent(QMouseEvent *) override; signal: void clicked(); }; class MySplitter : public QSplitter { public: explicit MySplitter(QWidget* parent = nullptr); protected: QSplitterHandle *createHandle() { return new MySplitterHandle(this->orientation(),this); } };
I added 2 widgets to MySplitter and made MySplitterHandle widget as 15.and I have connected the emit signal of the QSplitterHandle to a slot which hide/show (toggle) the first widget.
Now my GUI is something like first widget, then 15 pxl QSplitterHandle and then second widget.ISSUE:
when I try to hide the first widget, QSplitterHandle also get hidden along with first widget. And I am not able to get it back.
How can I restrict QSplitterHandle to hide when hiding the first widget.I have tried my best to give all details so that you all can understand the issue well and guide me. Any initiative is appreciated.
-
Something like this :
QList<int> savedSize; MySplitterHandle *splitterHandle=qobject_cast<MySplitterHandle*>(splitter.handle(1)); connect(splitterHandle,SIGNAL(Clicked()),this,SLOT(handleClickedSot())); void handleClickedSot() //slot { QTimer::singleShot(50,this, &Widget2::showHideWidget); } void Widget2::showHideWidget() { m_ShowHideFlag=!m_ShowHideFlag; if(m_ShowHideFlag) { savedSize=splitter.sizes(); QList<int> newSize(savedSize); newSize[0]=0; //hide first widget splitter.setSizes(newSize); } else { splitter.setSizes(savedSize); } }
-
Hi,
Do you mean like if you would have use QSplitter::setCollapsible on that widget ?
-
Yes I tried using setCollapsible false also but this also did not helped.
In that case also when I try to hide the first widget, qsplitterhandle next to it also gets hidden. That's strange to me. -
This may be out of context of this issue but I found something stranger.
I was playing around with qsplitter and setOpaqueResize as false with a intention that resized event should not get called for added widgets in qsplitter. When I click on the handle ( without moving the handle), mouse click event is emitted and splitterMoved() signal is also emitted (even though handle is not moved).But on the contrary, when I set setOpaqueResize as true , on click of the handle without moving it, only mouse click event is triggered.
This is very surprising to me. I am using qt5.12.
-
It's indeed a bit surprising but there might be some filtering done on the mouse with regard to that.
As for my question, I think I miswrote it. From your description, it seems that you are re-implementing collapsible widgets but I may have misunderstood you. Is that the case ?
-
@SGaist
I am not trying to re-implement collapsible widgets.My intention is to use the custum qsplitterhandle hence I have overridden the 'QSplitterHandle *createHandle()' of qsplitter baseclass. -
I used setSizes() to hide my first widget in my onSplitterHandleClicked slot. And in that slot I am also saving the splitter sizes to restore back widgets sizes when the handle is clicked again. This works fine now.
-
Something like this :
QList<int> savedSize; MySplitterHandle *splitterHandle=qobject_cast<MySplitterHandle*>(splitter.handle(1)); connect(splitterHandle,SIGNAL(Clicked()),this,SLOT(handleClickedSot())); void handleClickedSot() //slot { QTimer::singleShot(50,this, &Widget2::showHideWidget); } void Widget2::showHideWidget() { m_ShowHideFlag=!m_ShowHideFlag; if(m_ShowHideFlag) { savedSize=splitter.sizes(); QList<int> newSize(savedSize); newSize[0]=0; //hide first widget splitter.setSizes(newSize); } else { splitter.setSizes(savedSize); } }