PDA

View Full Version : QT designer 4.5



JD2000
26th October 2009, 16:40
Hello,

I'm a complete newbie when it comes to QT and although I can find plenty of examples of coding from scratch, can not seem to find much in the way of help when it comes to using QT Designer 4.5.

How do you implement a tree view (not tree widget) with this version of designer?

I am obviously missing something basic in the way QT Designer works.

for instance I know that a directory view of the local file system can be generated with the following fairly trivial class:

-------------------------------------------
directoryTreeView.h

#ifndef DIRECTOTYTREEVIEW_H
#define DIRECTOTYTREEVIEW_H

#include <QWidget>
#include <QTreeView>
#include <QModelIndex>
#include <QDirModel>

class directoryTreeView : public QWidget
{
public:
directoryTreeView();
};

#endif // DIRECTOTYTREEVIEW_H
-------------------------------------------

directoryTreeView.cpp


#include "directorytreeview.h"


directoryTreeView::directoryTreeView()
{
QDirModel *model = new QDirModel;
QTreeView *tree = new QTreeView();
tree->setModel(model);
tree->show();
}
--------------------------------------------


With QT Designer you can create a QU GUI application, drag a model based tree view onto the resulting mainwindow.ui form, but then what?

Any help would be most appreciated, thanks.

high_flyer
26th October 2009, 16:47
I am obviously missing something basic in the way QT Designer works.

Yes, and that would be - that designer is not intended for developing applications, but, for designing ui's.
Once you have your ui optically ready (you could also do some action integration with designer) you then use that ui in your project and PROGRAM the functionality for that ui.

JD2000
26th October 2009, 18:47
Thanks high_flyer,

Apologies for asking what is probably another stupid question, - but how?

In the example quoted above, the designer generates the following mainwindow class:


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
--------------------------------

mainwindow.cpp


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

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
// new directoryTreeView;

ui->setupUi(this);
}

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


I assume that I have to insert the QDirModel code from "directoryTreeView.cpp"
somewhere to add the functionality.

The QT Designer manual talks about a multiple inheritance method of doing this, which looks promising, but how would I adapt this class?

Thanks for the help.

squidge
26th October 2009, 20:43
QDirModel provides a data model for the local file system, if you want to place some other kind of data in your tree view then it's not the class to derive from. It would be better to subclass something like QAbstractItemModel and then tree->setModel(model); where 'model' is a pointer to that type of class.

high_flyer
27th October 2009, 09:28
The QT Designer manual talks about a multiple inheritance method of doing this, which looks promising, but how would I adapt this class?

I am sorry, but I have trouble following what it is you want...

You have made a class derived from QMainWindow, which has your designer made ui encapsulated in the member 'ui'.
From that member you can access all the ui elements in your form, including your tree view.

On the model issue see the post before this one.

JD2000
27th October 2009, 13:16
Sorry perhaps I'm not asking the question properly.

In my original query above I used QT Designer 4.5 to create a mainwindow containing a model based tree view.

The following version of mainwindow.cpp, when compiled and run generates the form with a placeholder for the treeview (as created by QT Designer)

and treeview (a directory browser in this case) which appears in a separate popup window.

How do you get the treeview to appear in the place holder on the mainwindow form, where it belongs?

The QT Designer manual discusses a multiple inheritance method of doing this, but I'm obviously missing something.

Thanks again for bearing with me!



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

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDirModel>
#include <QTreeView>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
// new clientTreeView;

ui->setupUi(this);
QDirModel *model = new QDirModel;
QTreeView *tree = new QTreeView();
tree->setModel(model);
tree->show();
}

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

squidge
27th October 2009, 16:04
Why are you creating a QTreeView object if you have one in designer already? It will create the object for you, and you can reference it via 'ui' object. Secondly, if you do create things yourself, you should give them at least a parent object.

JD2000
27th October 2009, 17:57
Hi fatjuicymole,

That in essence was my question, I could not work out how to connect the implementation code to that generated by QT Designer.

Now I finally understand what the Designer is doing, the solution is obvious.

My thanks to both you and high_flyer for your help,

For anyone else who has the same problem:

If the objectname you give your (model-based) treeview in QT Designer is 'directoryTreeView'
the following mainwindow.cpp class achieves what I was trying to do:




#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDirModel>
#include <QTreeView>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
// new directoryTreeView;

ui->setupUi(this);
QDirModel *model = new QDirModel;
ui->directoryTreeView->setModel(model);
ui->directoryTreeView->show();
}

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

Incidentally I chose the QDirModel to illustrate my question I was never really interested in creating a directory browser!

squidge
27th October 2009, 22:14
Nice to see you have it sorted. If you get fedup with using the 'ui' object, or just prefer to reference objects directly, you can use multiple inheritance to include the class directly so you don't need the 'ui' part, but it's best to keep it I always think as it makes things more modular.

JD2000
28th October 2009, 18:31
Just for the sake of completeness, the revised mainwindow class below does the same thing but uses multiple inheritance as suggested by fatjuicymole. I hope this helps other newbies experiencing the same problems I was having.


mainwindow.h



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include "ui_mainwindow.h"

class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT

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

private:

};

#endif // MAINWINDOW_H

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



#include "mainwindow.h"
#include <QDirModel>
#include <QTreeView>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// new directoryTreeView;

setupUi(this);
QDirModel *model = new QDirModel;
directoryTreeView->setModel(model);
directoryTreeView->show();
}

MainWindow::~MainWindow()
{
}

high_flyer
29th October 2009, 17:09
Multiple inheritance in this case has advantages, such as easier access.
But it has also the disadvantage of being not flexible.
If you use the first method, with the 'ui' object, you can have the same functionality run on different UI's, where all you need to do is change the ui object.
Both methods are valid, you have to choose what is bast for your case.

axeljaeger
30th October 2009, 07:08
iew of the local file system can be generated with the following fairly trivial class:

You missed the fact that you need proper parent/child relationships for your objects in order to avoid memory leaks. Who is gonna to delete your model?

JD2000
3rd November 2009, 13:03
Axel,

Thank you for this piece of advice.

The directoryTreeView class was only intended as a means of asking how to integrate QT designer generated code with the rest of the application, but you are correct, I should have given the model a parent or at least made use of the delete operator.