Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Does addwidgets means the make a widget become the parent of another widget
Forum Updated to NodeBB v4.3 + New Features

Does addwidgets means the make a widget become the parent of another widget

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 388 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hehedaozuiteng
    wrote on last edited by hehedaozuiteng
    #1

    Hi all. sorry if my quesion silly, I am confuze about the relation between the different widgets.

    does the following coda means the same thing in a QT widget application ?

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       QGridLayout *layout = new QGridLayout;
       QCustomWidgets *qcw = new QCustomWidget();   
        layout->addWidget(qcw);
        ui->awidget->setLayout(layout);
    }
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       QGridLayout *layout = new QGridLayout;
       QCustomWidgets *qcw = new QCustomWidget(this);   
        layout->addWidget(qcw);
        ui->awidget->setLayout(layout);
    }
    
    

    when I called the

    ui->awidget->setLayout(layout)
    

    in first code, the *qcw 's parent widget becomes ui->awidget, so I can operating it as the ui->awidget->*qcw(maybe not, I have to store it in mainwidow's h file?).

    In second code, the *qcw 's parent widget becomes ui->awidget from MainWindow.

    I get it, I am correct.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      When you add a widget to a layout the ownership is transferred to the layout. When you call setLayout, the owner of the layout becomes the widget on which you call setLayout.

      If you want to access a widget that you add in the constructor of your designer based widget, then yes, make it a member variable of your class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved