Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Incorrect of a widget addition
Qt 6.11 is out! See what's new in the release blog

Incorrect of a widget addition

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 423 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.
  • Evgeny SiberiaE Offline
    Evgeny SiberiaE Offline
    Evgeny Siberia
    wrote on last edited by Evgeny Siberia
    #1

    Hi. Why this code is not correct?
    I get the warning:
    Warning: QLayout: Attempting to add QLayout "to QDialog "QDialog", which already has a layout

    QWidget *w = new QWidget;
    QDialog *dlg = new QDialog;
    QVBoxLayout *lay = new QVBoxLayout;
    dlg->setLayout(lay);
    lay->addWidget(w);
    

    Surely, I know QDialog has a layout. But when I tried to add into this layout widget, I get Segmentation Fault.

    QWidget *w = new QWidget;
    QDialog *dlg = new QDialog;
    dlg->layout()->addWidget(w);
    

    if you see mistakes in English tell me about it

    J.HilkJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Evgeny-Siberia said in Incorrect of a widget addition:

      _settingsForm

      Shouldn't this be w ?

      QDialog has no layout by default. You must be setting another one somewhere else.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • Evgeny SiberiaE Evgeny Siberia

        Hi. Why this code is not correct?
        I get the warning:
        Warning: QLayout: Attempting to add QLayout "to QDialog "QDialog", which already has a layout

        QWidget *w = new QWidget;
        QDialog *dlg = new QDialog;
        QVBoxLayout *lay = new QVBoxLayout;
        dlg->setLayout(lay);
        lay->addWidget(w);
        

        Surely, I know QDialog has a layout. But when I tried to add into this layout widget, I get Segmentation Fault.

        QWidget *w = new QWidget;
        QDialog *dlg = new QDialog;
        dlg->layout()->addWidget(w);
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Evgeny-Siberia

        layout() pointer should be nullptr initialized, so check against it and do it appropriately.

        QWidget *w = new QWidget;
        QDialog *dlg = new QDialog;
        if(dlg->layout()){
            dlg->layout()->addWidget(w);
        }else{
           QVBoxLayout *lay = new QVBoxLayout;
           lay->addWidget(w);
           dlg->setLayout(lay);    
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2

        • Login

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