PDA

View Full Version : Help with QDockWidget



mindbender
9th March 2010, 21:53
I'm sure this question has been answered somewhere in this forum but I can't seem to find exactly what I need so I'm going to ask you guys for a little help. I'm trying to build an app using the QDockWidget. My problem is getting things working properly. Let me start by asking, is it proper protocol to design my "QDockWidget" in designer or am I supposed to to design a std widget in designer and sub-class it as a QDockWidget? I've been trying to design my QDockWidget and then use it directly. (see code below). It just seems I'm making things more complicated than they should be in this IDE.

Pls note that I deleted some of what I thought to be non-essential code for brevity. If you need everything just ping me on skype or gmail and I'll send it over.

Thanks for you help!!!

Chris L.

Slick Systems Development
Phone: 561-253-3755
Fax: 561-253-3754
Toll Free: 1-800-513-4525
Email: qtinfo@slicksys.net
Website: http://www.slicksys.net



---------- Begin Code ------------------

----- main.cpp -----
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

------------ mainwindow.cpp -----------

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
setWindowTitle("s");
ui->setupUi(this);
textEdit = new QTextEdit;
setCentralWidget(textEdit);
createDockWindows();
}

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

void MainWindow::createDockWindows()
{

cbuilder = new CriteriaBuilder(this);
cbuilder->setBaseSize(QSize(140,600));
cbuilder->setAllowedAreas(Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea,cbuilder);

/*
summ=new Summary(this);
summ->setBaseSize(QSize(140,600));
summ->setAllowedAreas(Qt::LeftDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea,summ);
*/
return;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

------------ criteriabuilder.cpp ------------
#include "criteriabuilder.h"

CriteriaBuilder::CriteriaBuilder(QWidget *parent) : QDockWidget(parent), ui(new Ui::CriteriaBuilder)
{
ui->setupUi(this);
}

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

void CriteriaBuilder::changeEvent(QEvent *e)
{
QDockWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}


--------

#ifndef CRITERIABUILDER_H
#define CRITERIABUILDER_H

#include "ui_criteriaBuilder.h"

namespace Ui {
class CriteriaBuilder;
}

class CriteriaBuilder : public QDockWidget
{
Q_OBJECT

public:

CriteriaBuilder(QWidget *parent = 0);
~CriteriaBuilder();

protected:
void changeEvent(QEvent *e);

private:
Ui::CriteriaBuilder *ui;
};


#endif // CRITERIABUILDER_H



---------------- mainwindow.h-------------

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui/QApplication>
#include <QDockWidget>
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QCheckBox>
#include <QtGui/QComboBox>
#include <QtGui/QDockWidget>
#include <QtGui/QFrame>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QSlider>
#include <QtGui/QSpacerItem>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>

#include "ui_mainwindow.h"
#include "criteriabuilder.h"
#include "summary.h"


//QT_BEGIN_NAMESPACE
//class QTextEdit;
//QT_END_NAMESPACE

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
void createDockWindows();

Ui::MainWindow *ui;
CriteriaBuilder *cbuilder;
Summary *summ;
QTextEdit *textEdit;
QMenu *fileMenu;
QMenu *editMenu;
QMenu *viewMenu;
QMenu *helpMenu;
QToolBar *fileToolBar;
QToolBar *editToolBar;
QAction *newLetterAct;
QAction *saveAct;
QAction *printAct;
QAction *undoAct;
QAction *aboutAct;
QAction *aboutQtAct;
QAction *quitAct;


};

#endif // MAINWINDOW_H


--------------------------------------

#ifndef UI_CRITERIABUILDER_H
#define UI_CRITERIABUILDER_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QCheckBox>
#include <QtGui/QComboBox>
#include <QtGui/QDockWidget>
#include <QtGui/QFrame>
#include <QtGui/QGridLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QSlider>
#include <QtGui/QSpacerItem>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_CriteriaBuilder
{
public:
QWidget *dockWidgetContents;
QGridLayout *gridLayout_9;

... deleted code for brevity ...


void setupUi(QDockWidget *CriteriaBuilder)
{
if (CriteriaBuilder->objectName().isEmpty())
CriteriaBuilder->setObjectName(QString::fromUtf8("CriteriaBuilder"));

... deleted code for brevity ....

QMetaObject::connectSlotsByName(CriteriaBuilder);
} // setupUi

void retranslateUi(QDockWidget *CriteriaBuilder)
{
CriteriaBuilder->setWindowTitle(QApplication::translate("CriteriaBuilder", "Option Selection Criteria Builder", 0, QApplication::UnicodeUTF8));
checkBox_38->setText(QString());
} // retranslateUi

};

namespace Ui {
class CriteriaBuilder: public Ui_CriteriaBuilder {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_CRITERIABUILDER_H

Lykurg
9th March 2010, 22:59
Let me start by asking, is it proper protocol to design my "QDockWidget" in designer or am I supposed to to design a std widget in designer and sub-class it as a QDockWidget? I've been trying to design my QDockWidget and then use it directly. (see code below). It just seems I'm making things more complicated than they should be in this IDE.
It doesn't matter which way you use. You can use your main window ui and add the dock widget right there, or you can make a own class "dock widget" at the designer and add it the the main window by calling addDockWidget().
I personally like the last approach since it separates your code nicely. All the code belonging to the dock widget is bundled at one place.

(And we have nice [CODE] tags at this forum;))

mindbender
9th March 2010, 23:38
My apologies, I didn't think about the code tags. Is it possible you or someone could post a simple example of what the code would look like
with a qdockwidget created in the designer coded into a main window. I've been fighting it for awhile and I'd like to see what i'm not doing right.

Thanks again! I'll be sure and use the code tags from now on too. :-)

Lykurg
10th March 2010, 00:05
Just a quick build...

mindbender
10th March 2010, 01:10
Thank You Lykurg for your kindness!

mindbender
10th March 2010, 15:35
Ah looks like my code was correct. The problem was that I had renamed the "DockWidget.ui" file to "criteriabuilder.ui" without changing the name of the class in QDesigner.
When I try to do anything with class "CriteriaBuilder" it did a lot of complaining. Once I found the naming issue in the UI file everthing was as you said it should be. I knew
I was somehow making things overly complicated.

...again I appreciate your help.

Lykurg
10th March 2010, 16:13
...again I appreciate your help.
You are welcome! Good you find that error.