Saturday, 15 February 2014

widget - showing hidden dockwidget in qt -



widget - showing hidden dockwidget in qt -

hi create dockwidget starting hidden. problem afterwards cannot show it, while can status correctly ishidden() function. weird thing if start dockwidget not hidden, works perfect. including illustration reproduces unusual behaviour.

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); qdialog *dockdialog = new qdialog(this); // <---------edit: need create parent widget 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); // <------edit: set dock parent window 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(); // <------------ comment line , work, edit: not need anymore, working nicely 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); // <-----edit: need add together these lines in order able see dialog contains dock widget, not know why need 1 time again since in initialization specifying dock floatable qabstractbutton *floatbutton = dock->findchild<qabstractbutton*>("qt_dockwidget_floatbutton"); // <---------edit: add together lines in order rid off floating button if(floatbutton) floatbutton->hide(); // <----edit: same previous dock->show(); } } if(!button->ischecked()){ if(!dock->ishidden()){ qdebug() << "not hidden"; dock->hide(); } } }

as above code dockwidget not appearing in screen. if seek comment line specifying in mainwindow.cpp works, point want start dockwidget hidden. have idea, of happening.

thanks.

parent of qdock command should window widget, , not object of mainwindow class.

so, need replace this:

dock = new qdockwidget(this);

with this:

qmainwindow *window = new qmainwindow(0); // smell potential leak here (set parent!) dock = new qdockwidget(window);

control visible when press show button in first case, too, somewhere outside window, may not see it. also, there no need create separate instance of qmainwindow within class inheriting qmainwindow.

qt widget hide show dock

No comments:

Post a Comment