PDA

View Full Version : Help with Docked Widget!



drake1983
18th May 2007, 23:04
Hi everybody,
If I make a Widget in QtDesigner, how can I put it into a MainWindow, like a Docked Widget ?
Suppose the Widget has other components like Buttons, lineEdits.

I have read and tried using the QDockWidget with simple samples, adding a textedit, a lineEdit, but I cant do what I want, using a widget previously made in a separate class.

Thanks and sorry, my english writing is not the best.

wysota
19th May 2007, 01:00
So maybe first clearly state what you want and then we'll try to help you. Do you actually want to use a dock widget or is it just an example of some behaviour you want?

drake1983
22nd May 2007, 02:58
Thanks for your answer wysota.
Well, I want to make an application with a textedit and a docket widget to the right side of the Window, the docked will contain a widget with some buttons, and the user will be able to hide, move, and close the docket.

wysota
22nd May 2007, 10:10
I suggest you implement the two widgets (main window and the contents of the dock) in Designer and them create a dock widget in code and attach it to your main window.

steg90
22nd May 2007, 13:55
Hi,

I use a few docked widgets in my MDI application. This is the code I wrote to do a listbox dock widget and dock it at the bottom of the main window :



m_plistWidget = new QCustomListWidget;
QDockWidget* dockwidget = new QDockWidget( tr("Information"), this);
dockwidget->setWidget(m_plistWidget);
dockwidget->setAllowedAreas(Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea);
this->addDockWidget(Qt::BottomDockWidgetArea, dockwidget);


QCustomListWidget is a widget I did that is derived from QListWidget but has buttons as well.

Regards,
Steve

drake1983
22nd May 2007, 16:43
Thanks for your answers wysota and Steve.

I made the MainWindow and the other Widget using Qt Desginer.
Now, I have MainWindow.ui and Editor.ui.

The MainWindow have a Menu, and it will contain a dock widget with the editor widget.

This the MainWindow.h code:



#include <QMainWindow>
#include <QDockWidget>
#include "ui_MainWindow.h"
#include "editor.h"

class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
QDockWidget* dock; // the dock that will contain the editor widget
Editor* editor; //the widget with the textedit
MainWindow(QWidget* parent = 0, Qt::WFlags fl = 0 );
~MainWindow();
public slots:


};


And this is the MainWindow.cpp




#include "mainwindow.h"

MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl)
: QMainWindow( parent, fl ), Ui::MainWindow()
{
setupUi(this);
dock = new QDockWidget(tr("Editor"),this);
editor = new Editor(dock);
dock->setWidget(editor);
addDockWidget(Qt::RightDockWidgetArea, dock);
}


This the code of the Editor, may be will be useful.


#include <QWidget>
#include "ui_editor.h"
class Editor : public QWidget, private Ui::Form
{
Q_OBJECT
public:
Editor(QWidget* parent = 0, Qt::WFlags fl = 0 );
~Editor();
public slots:
};


Editor.cpp



#include "editor.h"

Editor::Editor(QWidget* parent, Qt::WFlags fl)
: QWidget( parent, fl ), Ui::Form()
{
setupUi(this);
}


When I compile the project it works fine, without errors, but the MainWindow doesn't have the docked widget inside. What I'm doing wrong?
I only got the MainWindow with the Menu. The dock widget doesn't exist.

thanks!

wysota
22nd May 2007, 20:32
You might need to call show() on the dock widget, although I'm not sure of that. I don't remember doing that with my dock widgets.

drake1983
22nd May 2007, 21:54
Thanks, I tried with that, but the dock doesn't show it.

Chuk
5th June 2007, 05:10
And this is the MainWindow.cpp




#include "mainwindow.h"

MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl)
: QMainWindow( parent, fl ), Ui::MainWindow()
{
setupUi(this);
dock = new QDockWidget(tr("Editor"),this);
editor = new Editor(dock);
dock->setWidget(editor);
addDockWidget(Qt::RightDockWidgetArea, dock);
}


I use something more like the following, and it works just fine. I even pass the dock pointer thru plugins and it works great. Mostly, I don't use "this" anywhere.



#include "mainwindow.h"

MainWindow::MainWindow(QWidget* parent, Qt::WFlags fl)
: QMainWindow( parent, fl )
{
setupUi(this);
dock = new QDockWidget(tr("Editor"));
editor = new Editor();
dock->setWidget(editor);
addDockWidget(Qt::RightDockWidgetArea, dock);
}


And then the MainWindow.h looks more like :



class MainWindow : public QMainWindow, public Ui::MainWindow


Note that Ui::MainWindow is public.