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. [Solved] Q Application Programatically

[Solved] Q Application Programatically

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 2.3k Views
  • 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.
  • S Offline
    S Offline
    sting
    wrote on last edited by
    #1

    I am attempting to write a Qt Application and I am having a conceptual issue. I started by writing the project as a dialog and did then entire project in code. I created all of the classes to make the project run, not complete, but enough to see that the structure works.

    Then the next step is to make it into a QApplication. I used the designer to rough out the structure so it is the same as in the dialog I had previously done. I promoted all of the classes to the ones I used in the dialog version, but now i get a warning.

    @
    <widget class="QWidget" name="centralWidget">
    <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
    <widget class="QListWidget" name="listWidget"></widget>
    </item>
    <item>
    <widget class="QScrollArea" name="scrollArea">
    <widget class="QWidget" name="deviceScroll">
    <layout class="QHBoxLayout" name="horizontalLayout_2">
    <item>
    <widget class="ChannelGroup" name="GBoxOne"> </widget>
    </item>
    <item>
    <widget class="ChannelGroup" name="GBoxTwo"> </widget>
    </item>
    <item>
    <widget class="ChannelGroup" name="GBoxThree"> </widget>
    </item>
    <item>
    <widget class="QGroupBox" name="GBoxFour"> </widget>
    </item>
    </layout>
    </widget>
    </widget>
    </item>
    </layout>
    </widget>
    @
    The ChannelGroup Header
    @
    class ChannelGroup : public QGroupBox
    { Q_OBJECT
    public:
    ChannelGroup(QWidget* parent = 0);
    private:
    QVBoxLayout theLayout;
    };
    @
    The ChannelGroup c++
    @
    ChannelGroup::ChannelGroup(QWidget
    parent) : QGroupBox(parent)
    {
    theLayout = new QVBoxLayout(parent);
    setLayout(theLayout);
    }
    @

    Each of the ChannelGroups are GroupBox and I want to put a vertical box layout in it so I can programatically add elements to the GroupBox. This is the point where I have enough of the UI specified in the Designer and want to turn to programatically building the application.

    The Designer shows a layout for the group box, but it is disabled until I add a widget to the Groupbox, which doesn't occur until runtime. I get the following error:

    QLayout: Attempting to add QLayout "" to QWidget "deviceScroll", which already has a layout

    I don't understand because deviceScroll is up several levels from the ChannelGroup.

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

      Hi,

      DeviceScroll has horizontalLayout_2 as layout manager, are you somewhere trying to set a new layout on it ? Can you show the code where you add your ChannelGroup widget to DeviceScroll ?

      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
      0
      • S Offline
        S Offline
        sting
        wrote on last edited by
        #3

        The ScrollDevice has a Layout. I add four GroupBox instances to the ScrollDevice. GroupBox inherits from QWidget. Then each GroupBox attempts to set its own layout within the GroupBox. This is done in the instantiation method for the group box.

        "Can you show the code where you add your ChannelGroup widget to DeviceScroll ?", it is done in the Designer using the class promotion mechanism.

        I can go to whatever level makes sense to build the application in code, I am under the assumption it is better to use the designer as much as possible.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          At casual glance it looks like it should work. Maybe the problem is there is nothing added to the layout?

          Here is code straight from the qt docs, that shows you can absolutely set a layout on the group box. And the parent widget's layout shouldn't matter in that case:

          @
          QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));

          QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
          QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
          QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
          
          radio1->setChecked(true);
          
          QVBoxLayout *vbox = new QVBoxLayout;
          vbox->addWidget(radio1);
          vbox->addWidget(radio2);
          vbox->addWidget(radio3);
          vbox->addStretch(1);
          groupBox->setLayout(vbox);
          

          @

          Maybe try just adding a placeholder widget just to test with:

          @
          ChannelGroup::ChannelGroup(QWidget* parent) : QGroupBox(parent)
          {
          theLayout = new QVBoxLayout(parent);
          theLayout->addWidget(new QLabel("test"));
          theLayout->addStretch(1);
          setLayout(theLayout);
          }
          @

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #5

            In ChannelGroup::ChannelGroup try to change from
            @
            theLayout = new QVBoxLayout(parent);
            setLayout(theLayout);
            @

            to

            @
            theLayout = new QVBoxLayout ;
            setLayout(theLayout);
            @

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sting
              wrote on last edited by
              #6

              That makes the message go away, but what is the effect of no parent to the layout? It is also shown in the "Layout Management page:"http://qt-project.org/doc/qt-5/layout.html so it must be good.

              Thanks

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

                Oh wow, can't believe I missed that (parent) thing. That is definitely the problem, lol.

                When you parented that layout you basically tried to assign it to your "parent" which was the object that already had the layout, hence the error.

                When you call setLayout(theLayout) it will reparent the layout to your current object. But even if you wanted to set a parent manually, you would use:

                @
                theLayout = new QVBoxLayout(this);
                // and NOT
                theLayout = new QVBoxLayout(parent);
                @

                Because the parent to the layout is your group box not the parent to the groupbox.

                Embarrassed I missed that (parent) in the first place. Good catch andreyc!

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andreyc
                  wrote on last edited by
                  #8

                  QVBoxLayout::QVBoxLayout(QWidget * parent) is alternative to

                  @
                  QVBoxLayout *layout = new QVBoxLayout ;
                  setLayout(layout);
                  @

                  "QWidget::setLayout":http://qt-project.org/doc/qt-5/qwidget.html#setLayout

                  Please add [SOLVED] to the title of your message If you consider it is solved.

                  1 Reply Last reply
                  0

                  • Login

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