Results 1 to 7 of 7

Thread: Child widgets

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Child widgets

    I am trying to learn QT creator and have searched and read the forum and several other areas but I must be missing something. I am writing a program that will have a main window and then widgets (dialog forms) that I want to open and close inside the main window. I am able to create the forms and have a button open the second form. The problem is that the form does not open as a child of the main window. I think I might need to create some sort of container in the main form and pass a pointer to that container? I am lost at how to do this. I am using QT Creator 1.2.1 based on QT 4.5.2. What am I missing? Here is the code!

    MainWindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. protected:
    19. void changeEvent(QEvent *e);
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23.  
    24. private slots:
    25. void on_btnNextPage_clicked(); //Identify a pointer here? Buy how?
    26.  
    27. };
    To copy to clipboard, switch view to plain text mode 
    MainWindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "page2.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::changeEvent(QEvent *e)
    18. {
    19. QMainWindow::changeEvent(e);
    20. switch (e->type()) {
    21. case QEvent::LanguageChange:
    22. ui->retranslateUi(this);
    23. break;
    24. default:
    25. break;
    26. }
    27. }
    28.  
    29. void MainWindow::on_btnNextPage_clicked()
    30. {
    31.  
    32. Page2* page2 = new Page2 () ; // Page2* page2 = new Page2 (this) ; ??
    33. page2->exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Child widgets

    You can't call QDialog::exec() and do what you want. From the docs:
    A dialog window is a top-level window ...
    ...
    ...if it has a parent, its default location is centered on top of the parent's top-level widget ...
    Change your slot to this:
    Qt Code:
    1. void MainWindow::on_btnNextPage_clicked()
    2. {
    3.  
    4. Page2* page2 = new Page2 () ; // Page2* page2 = new Page2 (this) ; ??
    5. setCentralWidget(page2);
    6. }
    To copy to clipboard, switch view to plain text mode 
    The mainWindow takes ownership of the widget so you don't need a this pointer.

    The widget that was previously the central widget will be deleted so you will save on memory but if you should want to reload any previous widget you will have to poll your database again and re-instantiate the widget.

    I agree with Lykurg's comment in your other post about QStackedWidget not normally being a resource problem.

    HTH
    Last edited by norobro; 9th August 2010 at 03:41. Reason: Corrected error.

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Child widgets

    Thanks, but I am still missing something! I tried your code and a new window opened with the original window still there, but the original window was now empty. How do I close the original window? So you think that the stacked widgets, even if they are connected to databases won't be much of a resource issue?

    Thanks
    Rick

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Child widgets

    You should only have one "MainWindow". Your page1 should be the MainWindow::centralWidget() when the app starts and when the slot is called page2 is set as centralWidget.

    Don't know what kind of resource restrictions you have or what size data sets you are working with but maybe the following will be of help: I created a stacked widget with four pages, each displaying a QTableView, with a corresponding QSqlQueryModel. I queried (select *) a db containing three tables each 14 columns wide: one with 21,143 records (used in two views), one with 17,540 records, and one with 35,036 records. I ran the app on an older machine (AMD Athlon XP 2700+ with 512MB of memory) and had no performance problems. The top command reports the app uses 27MB (5.4%) of system memory.
    Last edited by norobro; 10th August 2010 at 04:09. Reason: typo

  5. #5
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Child widgets

    I don't know why it isn't working like it should??? I think the stacked widgets is the way to go then. I am trying to figure out the stacked widgets now I am using Creator 2.0 and can get the form and the holder for the stacked widgets. I see the properties for page and page_2. I can't find the properties to put in the name of the widget I want for each page. From the examples it shows the form created from code. How do I put the widgets I want on the stacked widgets?

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Child widgets

    Here's a small example app. I just threw it together so the layout is pretty sloppy but maybe it will help.
    Attached Files Attached Files

  7. #7
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Child widgets

    I feel stupid! I was trying to create separate widgets for the pages and then couldn't find how to load them! You just build each widget on each page! Thanks....Sometimes it helps to see an example!
    Thanks a bunch!
    I am sure I will be back several times while I am trying to learn this QT stuff!

Similar Threads

  1. Replies: 5
    Last Post: 19th April 2010, 00:31
  2. How to get the child widgets from a Widget?
    By prykHetQuo in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2009, 14:26
  3. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 09:39
  4. hello! i have a simple child widgets question
    By ht1 in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2007, 00:49
  5. setClipPath on child widgets.
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 27th May 2007, 20:12

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.