9.6.  Layout of Widgets

[ fromfile: layout.xml id: layout ]

Figure 9.11.  Rows and Columns

Rows and Columns

Example 9.6. src/layouts/boxes/cardtable.h

#ifndef CARDTABLE_H
#define CARDTABLE_H
#include <cardpics.h>
#include <QWidget>

class CardTable : public QWidget {
  public:
    explicit CardTable(QWidget* parent=0);
  private:
    CardPics m_deck;
};

#endif        //  #ifndef CARDTABLE_H

Example 9.7. src/layouts/boxes/cardtable.cpp

[ . . . . ]

CardTable::CardTable(QWidget* parent)
: QWidget(parent) {

    QHBoxLayout* row = new QHBoxLayout();       1
    row->addWidget(new Card("ah"));             2
    row->addWidget(new Card("qd"));
    row->addWidget(new Card("ks"));
    row->addWidget(new Card("8c"));

    QVBoxLayout* rows = new QVBoxLayout();      3
    rows->addLayout(row);                       4

    row = new QHBoxLayout();                    5
    row->addWidget(new Card("qs"));
    row->addWidget(new Card("js"));
    row->addWidget(new Card("td"));
    rows->addLayout(row);                       6

    QVBoxLayout* buttons = new QVBoxLayout();   7
    buttons->addWidget(new QPushButton("Deal"));
    buttons->addWidget(new QPushButton("Shuffle"));

    QHBoxLayout* cols = new QHBoxLayout();      8
    setLayout(cols);                            9
    cols->addLayout(rows);                      10
    cols->addLayout(buttons);                   11
}
[ . . . . ]

1

First row

2

Parents are set by layout, so we don't have to.

3

Lay out rows vertically.

4

Nest a row in the vertical layout.

5

Second row

6

Nesting again

7

A column for the buttons

8

Bring them all together.

9

The "root layout" for this widget

10

Add both card rows as a column.

11

Add column of buttons as another column.


Example 9.8. src/layouts/boxes/boxes.cpp

#include <QApplication>
#include "cardtable.h"

int main(int argc, char* argv[]) {
	QApplication app (argc, argv);
	CardTable ct;
	ct.show();
	return app.exec();
}