[Solved] Q Application Programatically
-
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.
-
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 ?
-
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.
-
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);
}
@ -
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
-
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!
-
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.