Skip to content
  • 0 Votes
    3 Posts
    579 Views
    BDC_PatrickB

    @JonB
    Thanks.. found out, that i needed to create a new Pointer in the Declaration of the .h File..

    Means..

    TGS_Widget_AssetList * _assetList;

    Wasn´t enough...
    I needed to do:

    TGS_Widget_AssetList *_assetList = new TGS_Widget_AssetList(this); or auto *_assetList = new TGS_Widget_AssetList(this);

    Now it works.. i will update my initial post here

  • 0 Votes
    3 Posts
    2k Views
    E

    @jsulm Fortunately, I just solved it. I should not have specified the VerticalWidgetList class as the parent of the widgets added. Apparently, when you add it to the layout, the widgets added get some sort of other parent class. I still nullptr out the widget in the takeWidget() function, however, as control of it should pass to the function receiving the return value. When I got rid of setting the parent in the addWidget() and insertWidget() functions, it now works properly.

    I do wonder if I should still delete the child widget in removeWidget() and removeWidgetAt() after removing it from the layout. Or do I need to set the parent to nullptr before deleting it? When my program used clearWidgets(), which calls removeWidget(), there was no problem.

    It works now!

    This is the source code now:

    verticalwidgetlist.h

    #ifndef VERTICALWIDGETLIST_H #define VERTICALWIDGETLIST_H #include <QScrollArea> #include <QVBoxLayout> #include <QWidgetList> class VerticalWidgetList : public QScrollArea { Q_OBJECT public: explicit VerticalWidgetList(QWidget *parent = nullptr); bool addWidget(QWidget *child); void clearWidgets(); bool insertWidget(int index, QWidget *child); bool removeWidget(QWidget *child; bool removeWidgetAt(int index); QWidget *takeWidget(int index); int widgetAt(QWidget *child) const; private: QWidget *m_central; QVBoxLayout *m_layout; QWidgetList m_list; }; #endif // VERTICALWIDGETLIST_H

    verticalwidgetlist.cpp

    #include "verticalwidgetlist.h" VerticalWidgetList::VerticalWidgetList(QWidget *parent) : QScrollArea(parent) , m_central(new QWidget) , m_layout(new QVBoxLayout(m_central)) , m_list() { setWidget(m_central); setWidgetResizable(true); } bool VerticalWidgetList::addWidget(QWidget *child) { if(child == nullptr) return false; m_layout->addWidget(child); m_list.append(child); return true; } void VerticalWidgetList::clearWidgets() { while(m_list.count()) { QWidget *widget = m_list[0]; removeWidget(widget); } } bool VerticalWidgetList::insertWidget(int index, QWidget *child) { if(index < 0 || index > m_list.count()) return false; if(child == nullptr) return false; m_layout->insertWidget(index, child); m_list.insert(index, child); return true; } bool VerticalWidgetList::removeWidget(QWidget *child) { if(child == nullptr) return false; m_layout->removeWidget(child); int index = widgetAt(child); m_list.removeAt(index); delete child; return true; } bool VerticalWidgetList::removeWidgetAt(int index) { if(index < 0 || index >= m_list.count()) return false; QWidget *widget = m_list.takeAt(index); m_layout->removeWidget(widget); delete widget; return true; } QWidget *VerticalWidgetList::takeWidget(int index) { if(index < 0 || index >= m_list.count()) return nullptr; QWidget *widget = m_list.takeAt(index); m_layout->removeWidget(widget); widget->setParent(nullptr); return widget; } int VerticalWidgetList::widgetAt(QWidget *child) const { if(child == nullptr) return -1; return m_list.indexOf(child); }

    EDIT: If anyone tried the source code up until this point, in this post, try again. I was rearranging removeWidget() and removeWidgetAt(), which I realized were mismatched in terms of their parameters. It's good now!

  • 0 Votes
    7 Posts
    5k Views
    pauleddP

    @SGaist thank you, I took QFormLayout into account.
    @VRonin also thank you!
    I can not believe that it would take so much code to get this done...
    After starting over and over again I came up with this:

    GeneralTab::GeneralTab(QWidget *parent) :QWidget(parent) { QLineEdit *lineEdit = new QLineEdit("/dev/video0"); QGroupBox *groupBox = new QGroupBox("Settings",this); groupBox->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); QFormLayout *formLayout = new QFormLayout(groupBox); formLayout->addRow("Video Device: ",lineEdit); }

    and this looks exactly what I wanted, two widgets with a nice group box border sized to minimal space usage:
    https://pauledd.files.wordpress.com/2016/08/pat4.png
    I dont really know If this code is correct or might produce trouble but at least I dont find any errors at the moment.

  • 0 Votes
    2 Posts
    942 Views
    SGaistS

    Hi and welcome to devnet,

    Looks like you rather want to use QSplitter for that

  • 0 Votes
    3 Posts
    1k Views
    joeQJ

    @mcosta thank you very much! I get it.

  • 0 Votes
    4 Posts
    1k Views
    SGaistS

    AFAIK, stylesheets where introduced in Qt 4.2 so how can they use stylesheets if they use 4.1 ?