neff
30th January 2013, 14:21
Hi all,
I read a few tutorials and started to experiment with qt but I ran into a problem I just don't understand.
So, I'm trying to add and remove QPushButtons to/from a flowlayout (the one from the examples here (http://doc.qt.digia.com/4.7/layouts-flowlayout.html)) which works quite well except for the last item.
Here is my code:
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
deletebutton = new QPushButton("delete",this);
addbutton = new QPushButton("add",this);
hbox = new QHBoxLayout();
hbox->addWidget(deletebutton);
hbox->addWidget(addbutton);
QWidget* widget = new QWidget(this);
flowlayout = new FlowLayout(widget);
widget->setLayout(flowlayout);
QScrollArea *scrollArea = new QScrollArea();
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(widget);
vbox = new QVBoxLayout(this);
vbox->addLayout(hbox);
vbox->addWidget(scrollArea);
setLayout(vbox);
connect(deletebutton, SIGNAL(clicked()), this, SLOT(deleteButton()));
connect(addbutton, SIGNAL(clicked()), this, SLOT(addButton()));
}
void MyWidget::deleteButton() {
flowlayout->takeAt(0);
flowlayout->update();
qDebug() <<"deleted: "<<flowlayout->count();
}
void MyWidget::addButton() {
QPushButton* newButton = new QPushButton("button");
flowlayout->addWidget(newButton);
flowlayout->update();
connect(newButton, SIGNAL(clicked()), this, SLOT(buttonpressed()));
qDebug() <<"added: "<<flowlayout->count();
}
void MyWidget::buttonpressed() {
qDebug() <<"button pressed";
}
When I want to delete the last button, I get the correct qDebug() output (0), but the QPushButton does not disappear and when I click on it, it still produces the "button pressed" output. Everything else works just fine. I can add buttons and every button disappears when I click on delete.
What am I doing wrong?
Thanks!
I read a few tutorials and started to experiment with qt but I ran into a problem I just don't understand.
So, I'm trying to add and remove QPushButtons to/from a flowlayout (the one from the examples here (http://doc.qt.digia.com/4.7/layouts-flowlayout.html)) which works quite well except for the last item.
Here is my code:
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
deletebutton = new QPushButton("delete",this);
addbutton = new QPushButton("add",this);
hbox = new QHBoxLayout();
hbox->addWidget(deletebutton);
hbox->addWidget(addbutton);
QWidget* widget = new QWidget(this);
flowlayout = new FlowLayout(widget);
widget->setLayout(flowlayout);
QScrollArea *scrollArea = new QScrollArea();
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(widget);
vbox = new QVBoxLayout(this);
vbox->addLayout(hbox);
vbox->addWidget(scrollArea);
setLayout(vbox);
connect(deletebutton, SIGNAL(clicked()), this, SLOT(deleteButton()));
connect(addbutton, SIGNAL(clicked()), this, SLOT(addButton()));
}
void MyWidget::deleteButton() {
flowlayout->takeAt(0);
flowlayout->update();
qDebug() <<"deleted: "<<flowlayout->count();
}
void MyWidget::addButton() {
QPushButton* newButton = new QPushButton("button");
flowlayout->addWidget(newButton);
flowlayout->update();
connect(newButton, SIGNAL(clicked()), this, SLOT(buttonpressed()));
qDebug() <<"added: "<<flowlayout->count();
}
void MyWidget::buttonpressed() {
qDebug() <<"button pressed";
}
When I want to delete the last button, I get the correct qDebug() output (0), but the QPushButton does not disappear and when I click on it, it still produces the "button pressed" output. Everything else works just fine. I can add buttons and every button disappears when I click on delete.
What am I doing wrong?
Thanks!