hi all
I am trying to produce a dynamic layout. That means i want a layout with some buttons in a row. The number of the buttons should change regarding to the number of elements in a hash. I am trying this for days, but it never looks like i want it to look. I am not able to change the number of buttons without seeing the old buttons, which should be deleted. Here is my code and some screenshots:
This is my first approach. Its only a test without real values. First I remove all buttons from the layout (if there are some). I then add 3 buttons regarding to knownClients is empty or not. It looks like in picture "before.jpg" when 3 buttons are inserted. If knownClients is empty there should be nothing, but in fact there still are some buttons displayed. Please look at "after1.jpg".
//.h-File:
//.cpp-File:
ShowClientList
::ShowClientList() : QWidget() { setLayout(justAlayout);
}
void ShowClientList::showClients() {
if ((child = justAlayout->takeAt(1)) != 0) { delete child; }
justAlayout->update();
if (!knownClients.isEmpty()) {
justAlayout->addWidget(pb1);
justAlayout->addWidget(pb2);
justAlayout->addWidget(pb3);
}
}
//.h-File:
QHBoxLayout *justAlayout;
//.cpp-File:
ShowClientList::ShowClientList() : QWidget() {
justAlayout = new QHBoxLayout;
setLayout(justAlayout);
}
void ShowClientList::showClients() {
QLayoutItem *child;
if ((child = justAlayout->takeAt(1)) != 0) { delete child; }
justAlayout->update();
QPushButton *pb1 = new QPushButton ("asdf");
QPushButton *pb2 = new QPushButton ("qwert");
QPushButton *pb3 = new QPushButton ("dada");
if (!knownClients.isEmpty()) {
justAlayout->addWidget(pb1);
justAlayout->addWidget(pb2);
justAlayout->addWidget(pb3);
}
}
To copy to clipboard, switch view to plain text mode
My second approach is similar to the first one. I only added 3 lines of code to delete the layout after deleting all members of the layout. Then I assing a new layout before inserting the buttons. With 3 buttons inserted its looks the same, but after deleting them it looks like in picture "after2.jpg". The buttons are shifted down and are still clickable.
delete this->layout();
setLayout(justAlayout);
delete this->layout();
justAlayout = new QHBoxLayout;
setLayout(justAlayout);
To copy to clipboard, switch view to plain text mode
Is there a possibility to manage this problem and display the right number of buttons correctly?
Hulk
Bookmarks