ウィジェットのレイアウト

2019年5月13日
Qt
$ mkdir age
$ cd age
$ vi age.cpp
#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  QWidget *window = new QWidget;
  window->setWindowTitle("Enter Your Age");

  QSpinBox *spinBox = new QSpinBox;
  QSlider *slider = new QSlider(Qt::Horizontal);
  spinBox->setRange(0, 130);
  slider->setRange(0, 130);

  QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                   slider, SLOT(setValue(int)));
  QObject::connect(slider, SIGNAL(valueChanged(int)),
                   spinBox, SLOT(setValue(int)));
  spinBox->setValue(35);

  QHBoxLayout *layout = new QHBoxLayout;
  layout->addWidget(spinBox);
  layout->addWidget(slider);
  window->setLayout(layout);

  window->show();

  return app.exec();
}
$ qmake -project
$ ls
./  ../  age.cpp  age.pro
$ vi age.pro

age.proの最後に「QT += widgets」を追加する。

######################################################################
# Automatically generated by qmake (3.1) Mon May 13 17:47:14 2019
######################################################################

TEMPLATE = app
TARGET = age
INCLUDEPATH += .

# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Input
SOURCES += age.cpp
QT += widgets
$ qmake age.pro
Info: creating stash file /home/nakada/Qt4/age/.qmake.stash
$ ls
./  ../  .qmake.stash  Makefile  age.cpp  age.pro
$ make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -I. -isystem /usr/include/libdrm -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o age.o age.cpp
g++ -Wl,-O1 -o age age.o   -lQt5Widgets -lQt5Gui -lQt5Core /usr/lib/x86_64-linux-gnu/libGL.so -lpthread
$ ls
./  ../  .qmake.stash  Makefile  age*  age.cpp  age.o  age.pro
$ ./age