PDA

View Full Version : very basic layout problem



tuli
18th August 2014, 15:20
this code in the constructor of my QWidget inherited class:



QHBoxLayout* hv = new QHBoxLayout(this);
QTableWidget* tv = new QTableWidget(this);
tv->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

tv->setSelectionBehavior(QAbstractItemView::SelectionB ehavior::SelectItems);
tv->setColumnCount(20);
tv->setRowCount(20);

hv->addWidget(tv);
setLayout(hv);

creates the tablewidget crammed in the upperleft corner like this:

https://i.imgur.com/ZWjY2mE.png


The tableview that stretches itself over the entire widget as it is supposed to has been created by QtCreator form designer. However, it uses the exact same code! I literally copied over the generated code into my class, but some how only the auto-generated widget stretches properly. Removing the generated one doesnt help either.



Now, why the hell doesnt it stretch correctly!? Seriously, this shit is driving my crazy.

anda_skoa
18th August 2014, 17:52
Is "this" in the layout of its parent?

Cheers,
_

tuli
18th August 2014, 18:03
no, "this" is a QWidget class



class MyWidget : public QWidget
{
MyWidget(..) : ...
{
//code is here
}
}

anda_skoa
18th August 2014, 19:48
no, "this" is a QWidget class

Yes, that was obvious already. I was asking if the widget has been properly added to its parent's layout.

Cheers,
_

tuli
18th August 2014, 19:51
i directly show the widget with show().

anda_skoa
19th August 2014, 07:01
So the widget has no parent?

Cheers,
_

wysota
19th August 2014, 12:24
i directly show the widget with show().

Please show the full code of the widget's constructor. My wild guess would be that you call setupUi() after the code you posted which overrides the widget's layout you set earlier.

tuli
19th August 2014, 14:34
class test: public QWidget
{
Q_OBJECT

public:
test(QWidget *parent = 0);
~test(){}

private:
Ui::test ui;
};




test::teset(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);

QHBoxLayout* hv = new QHBoxLayout(this);
QTableWidget* rt = new QTableWidget(this);

rt->setColumnCount(4);
rt->setRowCount(10);
rt->setRowHeight(0, 22);
rt->verticalHeader()->hide();

hv->addWidget(rt);
setLayout(hv);
}

wysota
19th August 2014, 15:35
What happens if you comment out ui.setupUi(this) ?

anda_skoa
19th August 2014, 16:45
If your designer UI is empty, which it must be since none of its contents would be added to the layout, why have the instance at all?

Cheers,
_

wysota
20th August 2014, 07:25
If your designer UI is empty, which it must be since none of its contents would be added to the layout, why have the instance at all?

I think the point is that it is not empty and in the image we see two separate sets of widgets (with one widget in each set, actually) stacked on top of each other.

tuli
20th August 2014, 08:09
you are right. I am not sure what exactly the problem was, but removing all the designer stuff makes it work.


Essentially we end up with this:



class abc : public QWidget
{
public:
abc(QWidget* p = 0) : QWidget(p)
{
QHBoxLayout* lay = new QHBoxLayout(this);
QTableWidget* tw = new QTableWidget();
tw->setColumnCount(8);
tw->setRowCount(20);
lay->addWidget(tw);
setLayout(lay);
}
};


and that works fine. thanks for your help!

wysota
20th August 2014, 08:16
you are right. I am not sure what exactly the problem was,

The problem was two calls to setLayout().