Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]showing and hiding QDockWidget problem
QtWS25 Last Chance

[solved]showing and hiding QDockWidget problem

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 17.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    theodr
    wrote on last edited by
    #1

    i am creating a floating dockwidget with a qtoolbar as a child widget
    @ toolsDock = new QDockWidget(this);
    toolsBar = new QToolBar(this);

    toolsBar->setOrientation(Qt::Vertical);
    toolsBar->addAction(ui->actionSelect);
    toolsBar->addAction(ui->actionPolygon);
    toolsBar->addAction(ui->actionBrush);
    toolsBar->addAction(ui->actionErazer);
    toolsBar->addSeparator();
    toolsBar->addAction(ui->actionMark);
    toolsBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    
    toolsDock->setStyleSheet("QDockWidget { font: bold }");
    toolsDock->setFloating(true);
    toolsDock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
    QAbstractButton *floatButton = toolsDock->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");
    if(floatButton)
        floatButton->hide();
    
    toolsDock->setAllowedAreas( Qt::NoDockWidgetArea );
    toolsDock->setWindowTitle("Tools");
    toolsDock->setWidget(toolsBar);
    toolsDock->close();@
    

    the dockwidget starts as hiden. The problem then is that if afterwards try to show the dockwidget with

    @toolsDock->show();@

    i can't it is all the time hidden, but if i start the dockwidget as not hidden then i can show/hide it as i demand.
    could someone explain me what i am missing
    thanks

    1 Reply Last reply
    0
    • B Offline
      B Offline
      broadpeak
      wrote on last edited by
      #2

      the close() is too strong for you (close() is mainly used for closing the app with Qt::WA_DeleteOnClose enum)

      use these pair: hide() and show()

      1 Reply Last reply
      0
      • T Offline
        T Offline
        theodr
        wrote on last edited by
        #3

        @broadpeak i have tried all these combinations as well (i.e. hide()/show(), sethidden(true)/sethidden(false), setvisible(true)/setvisible(false)) and all these are working only if i do not start my dockwidget as hidden, otherwise the dockwidget does not appear although that i can see with the isheaden() that the dockwidget is hidden

        i am missing something else but i cannot understand what.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          My solution which is working:
          (I use in my app 9 dockwidget, and I have only dockwidgets in the mainwindow)

          @

          void MainWindow::createDockWindows()
          {

          // invisible centralwidget! this is very important if you have only dockwidgets without central widget !!!!!!!!
          QWidget* cw = new QWidget(this);
          setCentralWidget(cw);
          this->centralWidget()->hide();
          
          
          videoMonitorGridDW = new VMGridDockWidget();
          videoMonitorGridDW->setAllowedAreas(Qt::AllDockWidgetAreas);
          videoMonitorGridDW->setFeatures(VMGridDockWidget::NoDockWidgetFeatures);
          videoMonitorGridDW->setParent(this);
          addDockWidget(Qt::LeftDockWidgetArea, videoMonitorGridDW);
          

          ...
          skinDW = new SkinDockWidget();
          skinDW->setAllowedAreas(Qt::AllDockWidgetAreas);
          skinDW->setFeatures(SkinDockWidget::NoDockWidgetFeatures);
          skinDW->setParent(this);
          addDockWidget(Qt::LeftDockWidgetArea, skinDW);
          skinDW->hide();

          this->tabifyDockWidget(videoMonitorGridDW, displayListDW);

          videoMonitorGridDW->raise();
          }

          void MainWindow::setCameraPerspective()
          {

          skinDW->hide();
          displayListDW->show();
          

          ...
          exportDW->show();
          eventDW->raise();
          }

          @

          1 Reply Last reply
          0
          • T Offline
            T Offline
            theodr
            wrote on last edited by
            #5

            the strange thing is that i am getting the state of the dockwidget with the isHeaden() and it seems to work properly but the dock is not appearing on the screen while it should be...

            i am calling the dockwidget->show() through a slot, if that makes any difference

            1 Reply Last reply
            0
            • B Offline
              B Offline
              broadpeak
              wrote on last edited by
              #6

              According to QDockWidget doc:
              try to look after that your dockwidget has a parent or child widget
              (
              http://qt-project.org/doc/qt-4.8/qwidget.html#isVisibleTo
              or
              http://qt-project.org/doc/qt-4.8/qwidget.html#isHidden
              )

              And yes, your dockwidget's behaviour is very strange....
              (I use QT 4.8.3 and works perfectly with dockwidgets)

              Question, this: toolsDock->setWidget(toolsBar); is real? There is a setTitleBarWidget function too...

              1 Reply Last reply
              0
              • T Offline
                T Offline
                theodr
                wrote on last edited by
                #7

                what do you mean by if this: toolsDock->setWidget(toolsBar); is real?
                with that way i am adding the QToolBar widget to the QDockWidget, so the toolbar is a child widget of the dock window is it a wrong way to do it?

                i tried also to modify my code and instead of using toolsDock->setWidget(toolsBar); i change it to toolsDock->setTitleBarWidget(toolsBar); and that works as it is supposed to but the dock window is bigger, without title, and in general not fixed as it was before.

                Any suggestion is welcome, sorry for my naive questions but i am a newbie in qt.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  theodr
                  wrote on last edited by
                  #8

                  to show you the behavior try to run this example:

                  mainwindow.h

                  @#ifndef MAINWINDOW_H
                  #define MAINWINDOW_H

                  #include <QMainWindow>
                  #include <QtGui>

                  namespace Ui {
                  class MainWindow;
                  }

                  class MainWindow : public QMainWindow
                  {
                  Q_OBJECT

                  public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();
                  private slots:

                  void showDock();
                  

                  private:
                  Ui::MainWindow *ui;

                  QDockWidget *dock;
                  QPushButton *button;
                  

                  };

                  #endif // MAINWINDOW_H@

                  mainwindow.cpp

                  @#include "mainwindow.h"
                  #include "ui_mainwindow.h"

                  MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
                  {
                  ui->setupUi(this);

                  // QMainWindow mainWindow;
                  // QDockWidget *dock = new QDockWidget(&mainWindow);
                  dock = new QDockWidget(this);

                  dock->setStyleSheet("QDockWidget { font: bold }");
                  dock->setFloating(true);
                  dock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
                  QAbstractButton *floatButton = dock->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");
                  if(floatButton)
                      floatButton->hide();
                  
                  dock->setAllowedAreas( Qt::NoDockWidgetArea );
                  dock->setWindowTitle("Tools");
                  
                  this->addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
                  
                  QMainWindow *window = new QMainWindow(0);
                  window->setWindowFlags(Qt::Widget);
                  QToolBar *bar = new QToolBar(window);
                  
                  bar->setMovable(false);
                  bar->addAction("Select");
                  bar->addAction("Polygon");
                  bar->addAction("Brush");
                  bar->addAction("Erazer");
                  bar->addSeparator();
                  bar->addAction("Mark");
                  bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
                  bar->setOrientation(Qt::Vertical);
                  
                  window->addToolBar(Qt::LeftToolBarArea, bar);
                  window->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
                  
                  window->setParent(dock);
                  dock->setWidget(window);
                  

                  // dock->hide();

                  button = new QPushButton("show", this);
                  button->setCheckable(true);
                  
                  QObject::connect(button, SIGNAL(clicked()), this, SLOT(showDock()));
                  

                  }

                  MainWindow::~MainWindow()
                  {
                  delete ui;
                  }

                  void MainWindow::showDock()
                  {
                  // qDebug() << "hello";
                  if(button->isChecked()){
                  if(dock->isHidden()){
                  qDebug() << "hidden";
                  dock->show();
                  }
                  }

                  if(!button->isChecked()){
                      if(!dock->isHidden()){
                          qDebug() << "not hidden";
                          dock->hide();
                      }
                  }
                  

                  }@

                  and try to run it with commented and uncomment the line 45, i do not understand why it is not working if i start the dockwidget hidden....

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    theodr
                    wrote on last edited by
                    #9

                    i modified the mainwindow.cpp file and now seems to work properly

                    mainwindow.cpp

                    @#include "mainwindow.h"
                    #include "ui_mainwindow.h"

                    MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                    {
                    ui->setupUi(this);

                    // QMainWindow mainWindow;
                    // QDockWidget *dock = new QDockWidget(&mainWindow);
                    QDialog *dockDialog = new QDialog(this); // <-----added a parent widget for the dock
                    dock = new QDockWidget(dockDialog);

                    dock->setStyleSheet("QDockWidget { font: bold }");
                    dock->setFloating(true);
                    dock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
                    QAbstractButton *floatButton = dock->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");
                    if(floatButton)
                        floatButton->hide();
                    
                    dock->setAllowedAreas( Qt::NoDockWidgetArea );
                    dock->setWindowTitle("Tools");
                    
                    this->addDockWidget(Qt::TopDockWidgetArea, dock, Qt::Vertical);
                    
                    QMainWindow *window = new QMainWindow(dock); // <--- set as parent widget for the window the dock
                    window->setWindowFlags(Qt::Widget);
                    QToolBar *bar = new QToolBar(window);
                    
                    bar->setMovable(false);
                    bar->addAction("Select");
                    bar->addAction("Polygon");
                    bar->addAction("Brush");
                    bar->addAction("Erazer");
                    bar->addSeparator();
                    bar->addAction("Mark");
                    bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
                    bar->setOrientation(Qt::Vertical);
                    
                    window->addToolBar(Qt::LeftToolBarArea, bar);
                    window->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
                    
                    window->setParent(dock);
                    dock->setWidget(window);
                    dock->hide();
                    
                    button = new QPushButton("show", this);
                    button->setCheckable(true);
                    
                    QObject::connect(button, SIGNAL(clicked()), this, SLOT(showDock()));
                    

                    }

                    MainWindow::~MainWindow()
                    {
                    delete ui;
                    }

                    void MainWindow::showDock()
                    {
                    // qDebug() << "hello";
                    if(button->isChecked()){
                    if(dock->isHidden()){
                    qDebug() << "hidden";
                    dock->setFloating(true); // <---- setting some features again, although i have specified that in the initiliazation, otherwise it is shown inside the mainwindow
                    QAbstractButton floatButton = dock->findChild<QAbstractButton>("qt_dockwidget_floatbutton"); // <--- discarding the floating button, again
                    if(floatButton)
                    floatButton->hide();

                            dock->show();
                        }
                    }
                    
                    if(!button->isChecked()){
                        if(!dock->isHidden()){
                            qDebug() << "not hidden";
                            dock->hide();
                        }
                    }
                    

                    }@

                    this does what i want and it seems to work properly, i do not know if it is the proper way to do it, but it works. If someone has a better solution, i would be glad to see it.

                    Thanks

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved