May 3, 2012

l3e0wulf l3e0wulf
Hobby Entomologist
126 posts

Panel (hide/show)

 

Is there a component/widget to a panel with a button to hide and show?

Example:

 Signature 

—0x00

6 replies

May 3, 2012

Wilk Wilk
Ant Farmer
120 posts

Hello again.
I think you can achive desired behaviour by using QDockWidget [qt-project.org].
How to:

  1. Add a tool bar or another widget as container for buttons.
  2. Add a dockwidget, make it closable.
  3. Add an action, returned by toggleViewAction [qt-project.org] method of dockwidget to you toolbar.

That is it.

Screenshot:
dock_widget_example

Code snippet:

  1.   QAction *action = findChild<QDockWidget *>()->toggleViewAction();
  2.   action ->setText ("Dock");
  3.   findChild<QToolBar *>()->addAction(action);

I used Designer to create UI, but of course you are free to implement the same in any other way. If so, you won’t probably need to call findChild.
Also, if you want to repeat behaviour of IDE on your screen shot, you should also add some button that enables/disables you dockwidget in order to add or remove action, associated with it.

May 3, 2012

l3e0wulf l3e0wulf
Hobby Entomologist
126 posts

Thank you very much.

I do not want my QDockWidget floating. How i remove it?

 Signature 

—0x00

May 3, 2012

Wilk Wilk
Ant Farmer
120 posts

First, more complicated example (sorry for bad code):

  1. void MainWindow::control_dock(bool flag) {
  2.   if (flag) {
  3.     QDockWidget *dock = new QDockWidget (this);
  4.     dock->setWidget (new QWidget());
  5.     dock->setObjectName ("dock");
  6.     addDockWidget (Qt::RightDockWidgetArea, dock,Qt::Vertical);
  7.  
  8.     QAction *action = dock->toggleViewAction();
  9.     action ->setText ("Dock");
  10.     findChild<QToolBar *>()->addAction(action);
  11.   }
  12.   else {
  13.     QDockWidget *dock = findChild<QDockWidget *>("dock");
  14.     delete dock;
  15.   }
  16. }

Here control_dock(bool flag) is a public slot. I’ve created a checkable menu item, which enables/disables dockwidget, and connected it’s signal triggered to control_dock slot. That is it.
AFAIK you don’t have to worry about deleting of action – it’s performed automaticaly.

Screenshot:
menu_control_dock
(I’ve moved toolBar at the bottom to make you see the button on it while menu is shown)

To make QDockWidget not floating, use setFloating [qt-project.org] method of it.

May 4, 2012

l3e0wulf l3e0wulf
Hobby Entomologist
126 posts

Thank you very much.

Is there a way to make a sidebar like the Qt Creator?

 Signature 

—0x00

May 4, 2012

Wilk Wilk
Ant Farmer
120 posts

I’m sorry, but I don’t know. Bt you may look in sources of Qt Creator for the answer.

May 4, 2012

l3e0wulf l3e0wulf
Hobby Entomologist
126 posts

It has a lot of code and I can not find.

 Signature 

—0x00

 
  ‹‹ JMS with Qt      update question ››

You must log in to post a reply. Not a member yet? Register here!