Results 1 to 13 of 13

Thread: QT designer 4.5

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QT designer 4.5

    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
    Qt Code:
    1. #ifndef DIRECTOTYTREEVIEW_H
    2. #define DIRECTOTYTREEVIEW_H
    3.  
    4. #include <QWidget>
    5. #include <QTreeView>
    6. #include <QModelIndex>
    7. #include <QDirModel>
    8.  
    9. class directoryTreeView : public QWidget
    10. {
    11. public:
    12. directoryTreeView();
    13. };
    14.  
    15. #endif // DIRECTOTYTREEVIEW_H
    To copy to clipboard, switch view to plain text mode 
    -------------------------------------------

    directoryTreeView.cpp

    Qt Code:
    1. #include "directorytreeview.h"
    2.  
    3.  
    4. directoryTreeView::directoryTreeView()
    5. {
    6. QDirModel *model = new QDirModel;
    7. QTreeView *tree = new QTreeView();
    8. tree->setModel(model);
    9. tree->show();
    10. }
    To copy to clipboard, switch view to plain text mode 
    --------------------------------------------


    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.
    Last edited by wysota; 26th October 2009 at 22:32. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    JD2000 (27th October 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6. namespace Ui
    7. {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. };
    22.  
    23. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    --------------------------------

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::MainWindow)
    6. {
    7. // new directoryTreeView;
    8.  
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    To copy to clipboard, switch view to plain text mode 
    ---------------------------------


    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.
    Last edited by wysota; 26th October 2009 at 22:28. Reason: missing [code] tags

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT designer 4.5

    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.

  6. The following user says thank you to squidge for this useful post:

    JD2000 (27th October 2009)

  7. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. The following user says thank you to high_flyer for this useful post:

    JD2000 (27th October 2009)

  9. #6
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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!

    Qt Code:
    1. mainwindow.cpp
    2. -----------------------------------
    3.  
    4. #include "mainwindow.h"
    5. #include "ui_mainwindow.h"
    6. #include <QDirModel>
    7. #include <QTreeView>
    8.  
    9. MainWindow::MainWindow(QWidget *parent)
    10. : QMainWindow(parent), ui(new Ui::MainWindow)
    11. {
    12. // new clientTreeView;
    13.  
    14. ui->setupUi(this);
    15. QDirModel *model = new QDirModel;
    16. QTreeView *tree = new QTreeView();
    17. tree->setModel(model);
    18. tree->show();
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 

  10. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT designer 4.5

    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.

  11. The following user says thank you to squidge for this useful post:

    JD2000 (27th October 2009)

  12. #8
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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:


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QDirModel>
    4. #include <QTreeView>
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QMainWindow(parent), ui(new Ui::MainWindow)
    8. {
    9. // new directoryTreeView;
    10.  
    11. ui->setupUi(this);
    12. QDirModel *model = new QDirModel;
    13. ui->directoryTreeView->setModel(model);
    14. ui->directoryTreeView->show();
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Incidentally I chose the QDirModel to illustrate my question I was never really interested in creating a directory browser!
    Last edited by JD2000; 27th October 2009 at 18:21.

  13. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT designer 4.5

    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.

  14. #10
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_mainwindow.h"
    6.  
    7. class MainWindow : public QMainWindow, private Ui::MainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. private:
    16.  
    17. };
    18.  
    19. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    -------------------------------------------
    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QDirModel>
    3. #include <QTreeView>
    4.  
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. // new directoryTreeView;
    9.  
    10. setupUi(this);
    11. QDirModel *model = new QDirModel;
    12. directoryTreeView->setModel(model);
    13. directoryTreeView->show();
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. }
    To copy to clipboard, switch view to plain text mode 

  15. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. #12
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: QT designer 4.5

    Quote Originally Posted by JD2000 View Post
    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?
    It's nice to be important but it's more important to be nice.

  17. #13
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT designer 4.5

    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.
    Last edited by JD2000; 3rd November 2009 at 13:19.

Similar Threads

  1. Replies: 3
    Last Post: 10th December 2009, 22:53
  2. Threads in Designer plugins
    By hvengel in forum Qt Tools
    Replies: 2
    Last Post: 3rd January 2009, 19:19
  3. Replies: 13
    Last Post: 15th December 2006, 11:52
  4. Designer crashes when selecting some widgets
    By gwendal in forum Qt Tools
    Replies: 4
    Last Post: 21st July 2006, 13:18
  5. Adding slots in Designer
    By jamos in forum Qt Tools
    Replies: 5
    Last Post: 18th May 2006, 23:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.