Seite 1 von 1

Dynamisches Füllen von GroupBoxen

Verfasst: 10. November 2006 16:06
von mostrich
Hallo allerseits.

Habe folgendes Problem:

Ich entwickle gerade einen Dialog der aus zwei übereinander liegenden
Groupboxen besteht, wovon beim Erstaufruf nur der oberste sichtbar ist. In jeder dieser Groupboxen befindet sich eine variable Anzahl an PushButtons. Beim Drücken eines der Buttons in der oberen Groupbox
soll die untere erscheinen mit einer betimmten Anzahl an Buttons.
Wenn ich nun aber dann einen anderen Button der oberen Groupbox drücke ist die untere Groupbox leer und wird auch nicht mehr mit Buttons gefüllt. Kann mir dabei jemand helfen. Ich benutze Qt-3.3.4 unter Suse 9.3.

Vielen Dank
Wolfgang

Anbei der Quellcode zum Testen:

layout.h:
======

Code: Alles auswählen

#ifndef LAYOUT_H
#define LAYOUT_H

#include <qapplication.h>
#include <qdialog.h>
#include <qgroupbox.h>
#include <qlayout.h>
#include <qpushbutton.h>


class MyWidget:public QDialog
{
  Q_OBJECT

  public:
     MyWidget(QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags f = 0);
     ~MyWidget() {};

  signals:
    void lastWindowClosed();

  public slots:
    void slotButton1Pressed();
    void slotButton2Pressed();
    void quit();

  private:
    QVBoxLayout *mainLayout;
    QGridLayout *box1Layout;
    QGridLayout *box2Layout;
    QGroupBox   *groupBox1;
    QGroupBox   *groupBox2;
    QPushButton *topButton1;
    QPushButton *topButton2;
    QPushButton *bottomButton;
    QPushButton *bottomButton1;
    QPushButton *bottomButton2;
};

#endif
layout.cpp:
========

Code: Alles auswählen

#include "layout.h"

MyWidget::MyWidget( QWidget *parent, const char *name, bool modal, WFlags f )
        : QDialog( parent, name, modal, f )
{
  mainLayout = new QVBoxLayout( this, 10, 5, "MainLayout" );

  groupBox1 = new QGroupBox( this, "GroupBox1" );
  groupBox2 = new QGroupBox( this, "GroupBox2" );

  mainLayout->addWidget( groupBox1 );
  mainLayout->addWidget( groupBox2 );

  groupBox1->setColumnLayout(0, Qt::Vertical);
  groupBox1->layout()->setSpacing(5);
  groupBox1->layout()->setMargin(10);
  groupBox1->layout()->setAlignment(Qt::AlignTop);
  box1Layout = new QGridLayout(groupBox1->layout());

  groupBox2->setColumnLayout(0, Qt::Vertical);
  groupBox2->layout()->setSpacing(5);
  groupBox2->layout()->setMargin(10);
  groupBox2->layout()->setAlignment(Qt::AlignTop);
  box2Layout = new QGridLayout(groupBox2->layout());

  groupBox2->setHidden(TRUE);

  topButton1 = new QPushButton("1", groupBox1);
  topButton2 = new QPushButton("2", groupBox1);

  bottomButton = bottomButton1 = bottomButton2 = 0;

  box1Layout->addWidget(topButton1, 0, 0, Qt::AlignTop);
  box1Layout->addWidget(topButton2, 0, 1, Qt::AlignTop);

  connect(topButton1, SIGNAL(pressed()), this, SLOT(slotButton1Pressed()));
  connect(topButton2, SIGNAL(pressed()), this, SLOT(slotButton2Pressed()));
}

void MyWidget::slotButton1Pressed()
{
  if ( ! box2Layout->isEmpty() )
  {
    if ( bottomButton1 != 0 && bottomButton2 != 0 )
    {
       box2Layout->remove(bottomButton1);
       box2Layout->remove(bottomButton2);
       delete bottomButton1;
       delete bottomButton2;
    }
  }
  bottomButton = new QPushButton(QString("%1.1") .arg(topButton1->text()), groupBox2);
  box2Layout->addWidget(bottomButton, 0, 0, Qt::AlignTop);
  connect(bottomButton, SIGNAL(pressed()), this, SLOT(quit()));
  if ( groupBox2->isHidden() )
    groupBox2->setHidden(FALSE);
}


void MyWidget::slotButton2Pressed()
{
  if ( ! box2Layout->isEmpty() )
  {
    if ( bottomButton != 0 )
    {
       box2Layout->remove( bottomButton );
       delete bottomButton;
    }
  }

  bottomButton1 = new QPushButton(QString("%1.1") .arg(topButton2->text()), groupBox2);
  bottomButton2 = new QPushButton(QString("%1.2") .arg(topButton2->text()), groupBox2);
  box2Layout->addWidget(bottomButton1, 0, 0, Qt::AlignTop);
  box2Layout->addWidget(bottomButton2, 0, 1, Qt::AlignTop);
  connect(bottomButton1, SIGNAL(pressed()), this, SLOT(quit()));
  connect(bottomButton2, SIGNAL(pressed()), this, SLOT(quit()));
  if ( groupBox2->isHidden() )
    groupBox2->setHidden(FALSE);
}

void MyWidget::quit()
{
  emit lastWindowClosed();
}

int main( int argc, char *argv[] )
{
  QApplication a( argc, argv );
  MyWidget *mw = new MyWidget();
  mw->show();
  a.connect( mw, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
  return a.exec();
}
src.pro:
=====

Code: Alles auswählen

SOURCES += layout.cpp
HEADERS += layout.h

TEMPLATE = app
CONFIG   = release \
           warn_on \
           thread \
           qt

TARGET   = ./layout
/edit by Christian81: Siehe http://qtforum.de/forum/faq.php?mode=bbcode#5