Vertical Layout: something wrong
-
Hello!
I have one simple Q:
Default Widget project#include "mainwindow.h" #include <QApplication> #include <QVBoxLayout> #include <QTableWidget> void init_form(QWidget *parent); QVBoxLayout *vbox; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; init_form(&w); w.show(); return a.exec(); } void init_form(QWidget *parent){ vbox = new QVBoxLayout(parent); for(int i = 0; i < 2; i++){ QTableWidget *t = new QTableWidget(parent); t->setRowCount(10); t->setColumnCount(10); vbox->addWidget(t); } parent->setLayout(vbox); }
What is wrong?
Thank you! -
Hi,
What does MainWindow contain ?
As for the what is wrong question: why do you store a static pointer to your layout ?
One thing that is wrong is passaient a parent to your layout constructor and call setLayout afterward. If you give a parent to a layout, then it will be automatically applied. -
Hi,
What does MainWindow contain ?
As for the what is wrong question: why do you store a static pointer to your layout ?
One thing that is wrong is passaient a parent to your layout constructor and call setLayout afterward. If you give a parent to a layout, then it will be automatically applied.@SGaist said in Vertical Layout: something wrong:
What does MainWindow contain ?
Default empty form.
@SGaist said in Vertical Layout: something wrong:
As for the what is wrong question: why do you store a static pointer to your layout ?
Ok. What pointer I should use as a parent?
-
hi
MainWindow is special in that it normally has a centralWidget so
what i think you want is#include "mainwindow.h" #include <QApplication> #include <QVBoxLayout> #include <QTableWidget> void init_form(QWidget *parent); QVBoxLayout *vbox; // this is really not needed. could just be in function int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; init_form(w.centralWidget()); // give the centralWidget w.show(); return a.exec(); } void init_form(QWidget *parent){ vbox = new QVBoxLayout(); // no parent yet for(int i = 0; i < 2; i++){ QTableWidget *t = new QTableWidget(parent); t->setRowCount(10); t->setColumnCount(10); vbox->addWidget(t); } parent->setLayout(vbox); }