PDA

View Full Version : Strange performance of QDockWindow?



luffy27
18th April 2007, 06:38
I have a MainWindow subclassed by QMainWindow, and also a QDockWindow on the bottom of the MainWindow. Then I add a QWidgetStack into the QDockWindow. The QWidgetStack contains two QHBox. Each QHBox is a toolbar. Finally, I want to switch on/off between the two toolbar by clicking a toolbutton, that is why I use a QWidgerStack in the QDockWindow.

However, despite of the swicth on/off functionality, the QDockWindow has a ridiculous presentation or performance, it can not show correctly!

Below is my code, Please Help Me!


#include <qapplication.h>
#include <qstring.h>
#include <qmainwindow.h>
#include <qdockwindow.h>
#include <qtextedit.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qpopupmenu.h>
#include <qwidgetstack.h>
#include <qhbox.h>
#include <qlayout.h>


class MainWindow:public QMainWindow
{
public:
MainWindow(const QString& filename );
QDockWindow *toolBarDockWindow;
QWidgetStack *toolBarStack;
QWidgetStack *mainWindowStack;
QToolButton* fileButton;
QToolButton* editButton;
QToolButton* graphicButton;
QTextEdit *textEdit;

private:
void init();
QString m_filename;
};

MainWindow::MainWindow(const QString& filename)
:QMainWindow(0,0,WDestructiveClose)
{
int screenW;
int screenH;
screenW = QApplication::desktop()->width();
screenH = QApplication::desktop()->height();
this->resize(screenW,screenH);
m_filename = QString::null;
init();
}

void MainWindow::init()
{
toolBarDockWindow = new QDockWindow (QDockWindow::InDock,this,"toolbardockwindow",0);
toolBarDockWindow->setOrientation(Qt::Horizontal);
toolBarDockWindow->setHorizontallyStretchable(TRUE);
toolBarDockWindow->setVerticallyStretchable(TRUE);
toolBarDockWindow->setMovingEnabled(FALSE);
QColor tbBkgColor;
tbBkgColor.setHsv(205,167,189);
toolBarDockWindow->setPaletteBackgroundColor(tbBkgColor);
this->moveDockWindow(toolBarDockWindow,Qt::DockBottom);

toolBarStack = new QWidgetStack(toolBarDockWindow);
QHBox* editToolBar = new QHBox(toolBarStack);
editToolBar->setSpacing(6);
editToolBar->setMargin(6);
QHBox* graphicToolBar = new QHBox(toolBarStack);
graphicToolBar->setSpacing(6);
graphicToolBar->setMargin(6);
toolBarStack->addWidget(editToolBar,0);
toolBarStack->addWidget(graphicToolBar,1);

fileButton = new QToolButton(editToolBar,"filebutton");
fileButton->setUsesTextLabel(TRUE);
fileButton->setTextLabel("File",FALSE);
fileButton->setFont(QFont("Times",20,QFont::Bold));
fileButton->setPopupDelay(10);

QPopupMenu *fileMenu = new QPopupMenu(this);
fileMenu->insertItem("New File",this,SLOT(fileNew()),Qt::Key_F1);
fileMenu->insertItem("Load File",this,SLOT(fileOpen()),Qt::Key_F2);
fileMenu->insertItem("Save File",this,SLOT(fileSave()),Qt::Key_F3);
fileMenu->insertItem("Save File As...",this,SLOT(fileSaveAs()),Qt::Key_F4);
fileButton->setPopup(fileMenu);

editButton = new QToolButton(editToolBar,"editbutton");
editButton->setUsesTextLabel(TRUE);
editButton->setTextLabel("Edit",FALSE);
editButton->setFont(QFont("Times",20,QFont::Bold));

graphicButton = new QToolButton(editToolBar,"graphicButton");
graphicButton->setUsesTextLabel(TRUE);
graphicButton->setTextLabel("Graphic",FALSE);
graphicButton->setFont(QFont("Times",20,QFont::Bold));

QSpacerItem* editToolBarSpacer = new QSpacerItem(0,0 ,QSizePolicy::Expanding,QSizePolicy::Minimum);
editToolBar->layout()->addItem(editToolBarSpacer);

toolBarStack->raiseWidget(0);

textEdit = new QTextEdit(this,"textedit");
textEdit->setTextFormat(Qt::PlainText);
textEdit->setFocus();
this->setCentralWidget(textEdit);
}

int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QString filename = QString::null;
MainWindow *mainWindow = new MainWindow(filename);
app.setMainWidget(mainWindow);
mainWindow->show();

return app.exec();
}

wysota
18th April 2007, 08:59
Wouldn't it be simpler to use regular toolbars? QToolBar inherits QDockWindow so you can use it just like a dock window and you can use show() and hide() to switch between toolbars without a widget stack.

luffy27
18th April 2007, 13:20
Is there any functions like show() or hide() supplied by QToolBar?
Ok, I will try it.

But by the way, I have solved the problem by adding this:
toolBarDockWindow->setWidget(toolBarStack);

Thanks a lot!

wysota
18th April 2007, 18:02
Is there any functions like show() or hide() supplied by QToolBar?
Yes, QToolBar inherits QWidget.


But by the way, I have solved the problem by adding this:
toolBarDockWindow->setWidget(toolBarStack);

Using show/hide will give you much cleaner code, especially if you use setShown()/setHidden() which are slots, so that you'll be able to connect them to actions that can trigger those changes.

luffy27
18th April 2007, 19:10
All right, I will try it.
Thanks a lot!