Results 1 to 4 of 4

Thread: how to include one widget in another?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to include one widget in another?

    I created two widget one by command another by Qt form designer. The code as shown below. When i execute two separates forms displays....What changes do i need to do, so that one form displays inside another form as single widget?

    fileopen.cpp
    Qt Code:
    1. fileopen::fileopen(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::fileopen),
    4. m_layout( new QVBoxLayout() )
    5. {
    6.  
    7. QPushButton* add = new QPushButton("Add");
    8.  
    9. connect(add, SIGNAL(clicked()), this, SLOT(addRow()));
    10.  
    11. m_layout->addWidget(add);
    12. m_layout->setAlignment(Qt::AlignTop);
    13.  
    14. QWidget* w = new QWidget();
    15. w->setLayout(m_layout);
    16. w->show();
    17.  
    18. ui->setupUi(this);
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by aurora; 2nd November 2011 at 09:09.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to include one widget in another?

    You can have 'this->setCentralWidget(w);' OR 'ui->setupUi(this);', not both.

    To stick new widget inside the 'ui' you could do something like this:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->centralWidget->setLayout(l);
    8.  
    9. QWidget* w = new QWidget();
    10. l->addWidget(w);
    11. }
    To copy to clipboard, switch view to plain text mode 
    That will insert new widget inside the ui created in the designer.
    That's assuming this ui is empty, if you've something inside already, just add new widget to existing layout or parent it to existing widget, ie:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QWidget* w = new QWidget(ui->someExistingWidget);
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to include one widget in another?

    Quote Originally Posted by Spitfire View Post
    You can have 'this->setCentralWidget(w);' OR 'ui->setupUi(this);', not both.

    To stick new widget inside the 'ui' you could do something like this:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. ui->centralWidget->setLayout(l);
    8.  
    9. QWidget* w = new QWidget();
    10. l->addWidget(w);
    11. }
    To copy to clipboard, switch view to plain text mode 
    That will insert new widget inside the ui created in the designer.
    That's assuming this ui is empty, if you've something inside already, just add new widget to existing layout or parent it to existing widget, ie:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QWidget* w = new QWidget(ui->someExistingWidget);
    8. }
    To copy to clipboard, switch view to plain text mode 
    got error!
    :-1: error: cannot open output file debug/FileOpen_V2.exe: Permission denied
    :-1: error: collect2: ld returned 1 exit status

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to include one widget in another?

    Close the app before building it again (or kill the process if it hanged).

Similar Threads

  1. How to Include Qwt in My Project
    By gbmtoday in forum Qwt
    Replies: 8
    Last Post: 3rd February 2011, 01:13
  2. How to include a dll ???
    By anupamgee in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2010, 08:28
  3. mfc in QT include
    By trusch in forum Qt Programming
    Replies: 3
    Last Post: 16th July 2009, 10:01
  4. problem with an include
    By mickey in forum General Programming
    Replies: 1
    Last Post: 20th July 2006, 13:45
  5. include
    By mickey in forum Newbie
    Replies: 6
    Last Post: 4th April 2006, 23:14

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.