Results 1 to 5 of 5

Thread: Child widget problem

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

    Question Child widget problem

    I am trying to lear QT creator and have searched and read the forum and several areas but I must be missing something. But first I have a question. I am writing a program that will have a main window and then widgets (dialog forms) that I want to display in the main window. I read about stacked widgets but am concerned that if all the widgets are loaded then this could create a resource issue. Is that thought process correct? I am able to create the forms and have the button open the second form. The problem is that the form does not open as a child of the main window. 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();
    26.  
    27. };
    28.  
    29. #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. #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 () ;
    33. page2->exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

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

    page2.cpp
    Qt Code:
    1. #include "page2.h"
    2. #include "ui_page2.h"
    3.  
    4. Page2::Page2(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Page2)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Page2::~Page2()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Page2::changeEvent(QEvent *e)
    17. {
    18. QDialog::changeEvent(e);
    19. switch (e->type()) {
    20. case QEvent::LanguageChange:
    21. ui->retranslateUi(this);
    22. break;
    23. default:
    24. break;
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your assistance

    Rick

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Child widget problem

    Ok, do you want to use QStackedWidget? then your code is absolutely wrong add your widget in the constructor to the stackwidget and in the slot change the current index of your stackwidget. If you don't want use a stack widget, then you have to add your widget to the layout of your main class. Furthermore it is better to create the widget with a parent, so better always use
    Qt Code:
    1. Page2* page2 = new Page2 (this) ;
    To copy to clipboard, switch view to plain text mode 

    And normally QStackedWidget wont be a resource problem.

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

    Default Re: Child widget problem

    I didn't use a stacked widget because of my concerns with resources as several widgets will be connected to databases, so I was going to load and unload each widget as needed. Which way is better?

    I think I am missing something because I tried
    Qt Code:
    1. 1.Page2* page2 = new Page2 (this) ;
    To copy to clipboard, switch view to plain text mode 
    and Page2 still opened as its own window, not a window within mainwindow. What did I miss?
    Thanks for taking your time!
    Rick

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Child widget problem

    As told, you have to put that widget into the layout of your mainwindow. (See layout() to get it, or use setCentralWidget(), but that depends on your needs)

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

    Default Re: Child widget problem

    I am not sure how to add the widget into the layout of my window dynamically. I looked at the QLayout* QWidget::layout()const class and couldn't figure it out. I think I might need to place some sort of placeholder widget in the mainwindow widget and then somehow load and unload the widgets as needed. could you give me a little more information? Thanks
    Rick

Similar Threads

  1. [problem] No mouse input to child widget
    By Demandred in forum Newbie
    Replies: 3
    Last Post: 13th April 2010, 13:44
  2. Replies: 7
    Last Post: 14th January 2010, 08:47
  3. Replies: 4
    Last Post: 3rd October 2009, 08:19
  4. let parent widget grow with child widget?
    By BeS in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 11:17
  5. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00

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.