Results 1 to 5 of 5

Thread: StackedWidget, single-window UI - how to separate code for readability?

  1. #1
    Join Date
    Nov 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default StackedWidget, single-window UI - how to separate code for readability?

    I'm just starting out with QT, and my programming experience is heavily algorithm-based (machine vision research) rather than application/interface work.

    I'm looking at designing an interface for an image processing robotic application. The main window is fine, but I want other windows as well (for example, for tweaking image processing parameters and robot communication testing. Based on searching around, I figured a QStackedWidget would handle this fine, and from a UI-design perspective it certainly does.

    However, I find that my app.cpp (and to a lesser extent app.h) file is getting pretty large with many functions (affecting readability). I'd like to split it up, and would like advise on the advantages/disadvantages of the methods I'm thinking about.

    1. I could maintain the current large app.cpp but preface functions with page1_ or page2_ (and group all functions for a page together) so I'd know where to look for stuff.

    2. I could design multiple forms (.ui files) and put one in each page of the stacked widget. Actually, can I? I'm 75% sure I can, but please correct me if I'm wrong on this.

    3. I could leave the form as-is but create one class per page (not a QWidget class) which is responsible only for the widgets in that page. This seems identical to 1 except for the physical location of the code, not improving any separability/reusability at all. [1] seems to talk a bit about this.

    If there's any options I could use instead, please do suggest them. Main caveat is I'm pretty sure I prefer to design the UI in Designer rather than in code just so I can see what is placed relative to other things.

    Thanks for your time.

    [1] - http://www.qtcentre.org/threads/5015...n-window-class

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: StackedWidget, single-window UI - how to separate code for readability?

    Quote Originally Posted by ngoonee View Post
    2. I could design multiple forms (.ui files) and put one in each page of the stacked widget. Actually, can I? I'm 75% sure I can, but please correct me if I'm wrong on this.
    This is the usual approach.

    The widget that holds the stack widget only has to deal with the stack widget (or any sibling widgets).
    The contents of each page are handled by their respective implementation classes.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: StackedWidget, single-window UI - how to separate code for readability?

    Quote Originally Posted by anda_skoa View Post
    This is the usual approach.

    The widget that holds the stack widget only has to deal with the stack widget (or any sibling widgets).
    The contents of each page are handled by their respective implementation classes.

    Cheers,
    _
    Thank you, I'll pursue this. So I'd have (for example) a base.ui/cpp/h combination of source files which would hold the qstackedwidget and my main menu, then I'd have to have each page's widgets added (using addWidget) in the constructor? Doesn't seem to me like there's any way to (in Designer) look at how the whole UI looks like (the base.ui stackedwidget and menu and whatever other widgets are there plus the contents of the qstackedwidget), is that correct?

    Edit: I can't seem to addWidget, but I'm able to (for my base.ui qStackedWidget) add a new page and promote the QWidget in that page to be my own widget. Is there an error with my widget code? It's just the basic code created by the Qt 'add item' wizard.

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

    Qt Code:
    1. #include "mainpage.h"
    2. #include "ui_mainpage.h"
    3.  
    4. mainPage::mainPage(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::mainPage)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. mainPage::~mainPage()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ngoonee; 29th November 2014 at 06:22.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: StackedWidget, single-window UI - how to separate code for readability?

    addWdget() in the constructor should work.

    How does "I can't seem to addWidget" manifest itself?

    Cheers,
    _

  5. #5
    Join Date
    Nov 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: StackedWidget, single-window UI - how to separate code for readability?

    Quote Originally Posted by anda_skoa View Post
    addWdget() in the constructor should work.

    How does "I can't seem to addWidget" manifest itself?

    Cheers,
    _
    Sorry for the late reply. I fixed it, and have been moving ahead with my (halfway completed) app.

    I was unable to have a private myWidget object in my base class, but fixed that by using a myWidget pointer instead. Then in the constructor I just did the following:-

    Qt Code:
    1. page1 = new myWidget;
    2. ui->stackedWidget->addWidget(page1);
    To copy to clipboard, switch view to plain text mode 

    This worked. Previous I was trying to do stuff like:-

    Qt Code:
    1. page1 = myWidget();
    To copy to clipboard, switch view to plain text mode 
    and that would not be added properly.

Similar Threads

  1. Help - 1 Separate Window to Rule them all, Ideas please
    By 2lights in forum Qt Programming
    Replies: 1
    Last Post: 11th July 2013, 10:30
  2. Replies: 5
    Last Post: 27th July 2012, 07:21
  3. How to decide whether to make a separate class for widgets of a window?
    By TheIndependentAquarius in forum Qt Programming
    Replies: 4
    Last Post: 15th February 2012, 07:51
  4. How to enlarge QtCreator code completion window
    By vaginoid2 in forum Qt Tools
    Replies: 2
    Last Post: 29th January 2010, 23:39
  5. Displaying 2 QApplication's in single window
    By masandeep in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 08:05

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.