Wie man ein erweiterbares / kollabierbares Bereichs-Widget in QT erstellt

Ich möchte erstellen Sie eine benutzerdefinierte widget von QT mit den folgenden Eigenschaften:

  • Es ist ein container
  • Es kann aufgefüllt werden mit jeder QT-layout
  • Es kann innerhalb eines QT-layout
  • Eine Schaltfläche ermöglicht das ausblenden/Fach vertikal um den Inhalt, so dass nur der button sichtbar ist, all die enthaltenen layout ist unsichtbar.
  • Die Schaltfläche ermöglicht es zu erweitern/zu entfalten es wieder auf die Größe des layout-Inhalt.
  • Die Erweiterung und die einstürzung basiert auf Größen, die (nicht auf - /ausblenden) ermöglicht die animation.
  • Verwendbar in QDesigner

Um eine Idee, hier ist ein Bild von einem ähnlichen widget (nicht QT):
Wie man ein erweiterbares /kollabierbares Bereichs-Widget in QT erstellt

Ich habe bereits einen Rahmen, der richtig funktioniert und ausgesetzt in QDesigner. Ich brauche nun, um es zu erweitern/zu reduzieren, was offenbar nicht so einfach.

Habe ich versucht zu spielen mit resize(), sizePolicy(), sizeHint (), aber das funktioniert nicht:
Wenn der Rahmen brach ich bekam folgende Werte:

sizeHint: (500,20)
size    : (500,20)
closestAcceptableSize: (518,150)
Painted size: (518, 150)

QLayout::closestAcceptableSize ist nicht Teil des widget, so kann ich es nicht ändern.

Jeden Hinweis oder/und code-Schnipsel zu erreichen?

BEARBEITET:
Hier ein einfaches Beispiel. Ich entfernte alle außer notwendig.

main.cpp Beispiel

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>

#include "section.hpp"


using namespace myWidgets;
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    //Create the main Window
    QWidget window;
    window.resize(500,500);
    window.setStyleSheet("QPushButton:{background-color:rgba(128,128,128,192);}");

    //Create the main window layout
    QVBoxLayout topLayout(&window);
    QWidget *w1 = new QWidget();
    w1->setStyleSheet("background-color:rgba(128,128,128,192);");
    topLayout.addWidget(w1);

    Section section(&window);
    topLayout.addWidget(&section);

    QVBoxLayout inLayout(&section);
    QPushButton *button = new QPushButton();
    button->setMinimumHeight(100);
    inLayout.addWidget(button);

    QWidget *w2 = new QWidget();
    w2->setStyleSheet("background-color:rgba(128,128,128,192);");
    topLayout.addWidget(w2);



    window.show();

    return a.exec();
}

Abschnitt.hpp

#ifndef SECTION_HPP
#define SECTION_HPP

#include <QPushButton> //for the expand/collapse button
#include <QtDesigner/QDesignerExportWidget>
#include <QLayout>
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>


//Compatibility for noexcept, not supported in vsc++
#ifdef _MSC_VER
#define noexcept throw()
#endif

#if defined SECTION_BUILD
    #define SECTION_BUILD_DLL_SPEC Q_DECL_EXPORT
#elif defined SECTION_EXEC
    #define SECTION_BUILD_DLL_SPEC
#else
    #define SECTION_BUILD_DLL_SPEC Q_DECL_IMPORT
#endif

namespace myWidgets
{

class SECTION_BUILD_DLL_SPEC Section : public QWidget
{
    Q_OBJECT

    Q_PROPERTY( bool is_expanded MEMBER isExpanded)

public:
    //Constructor, standard
    explicit Section( QWidget *parent=0 ): QWidget(parent),
        expandButton(this)
    {
        expandButton.resize(20,20);
        expandButton.move(0,0);
        expandButton.connect(&expandButton, &QPushButton::clicked,
                             this, &Section::expandCollapseEvent);

        QMargins m= contentsMargins();
        m.setTop(m.top()+25);
        setContentsMargins(m);
        //setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Minimum);

    }

    virtual void expand( bool expanding ) noexcept
    {
        resize(sizeHint());
        isExpanded = expanding;
        updateGeometry();

qDebug() << (isExpanded? "expanded":"collapsed") << sizeHint() << QWidget::size() <<
            parentWidget()->layout()->closestAcceptableSize(this, size());
    }

    virtual QSize sizeHint() const noexcept override
    {
        if (isExpanded) return QSize(layout()->contentsRect().width(),
                                     layout()->contentsRect().height());
        else return QSize(layout()->contentsRect().width(), 20);
    }

    //Implement custom appearance
    virtual void paintEvent(QPaintEvent *e) noexcept override
    {
        (void) e; //TODO: remove
        QPainter p(this);
        p.setClipRect(e->rect());
        p.setRenderHint(QPainter::Antialiasing );
        p.fillRect(e->rect(), QColor(0,0,255,128));
    }

protected:

    //on click of the expandButton, collapse/expand this widget
    virtual void expandCollapseEvent() noexcept
    {
        expand(!isExpanded);
    }


    bool isExpanded = true; //whenever the section is collapsed(false) or expanded(true)
    QPushButton expandButton; //the expanding/collapsing button
};

}


#endif //SECTION_HPP

InformationsquelleAutor der Frage Adrian Maire | 2015-09-09

Schreibe einen Kommentar