Tuesday, May 12, 2015

Laying out Widgets, Creating Spinbox, Slider, Textbox

In the previous tutorial, you have learned how to create a label and a button in Qt. Qt is designed to simplify C++ programmer to create a nice GUI for their application. Therefore, it provides a wide variety of widget classes to create a cool GUI which is divided into groups such as Layouts, Buttons, Item views, Input Widgets, so on and so forth.


In this post, we will provide another small user interface using QSlider (Slider), QSpinBox, QLineEdit (textbox), and QWidget. We will use QWidget as the main window of the application. In order to lay out the QSlider, QSpinbox, and QLineEdit on the application’s main window, layout manager will be used.

#include <QApplication>
#include <QLineEdit>
#include <QSpinBox>
#include <QSlider>
#include <QVBoxLayout>
 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    
    // Create QWidget serving as the main window
    QWidget* mainWindow = new QWidget;
 

    // Set the title of the main window
    mainWindow->setWindowTitle("Basic GUI");
    mainWindow->setFixedSize(320, 200);
 

    // Create two textbox
    QLineEdit* weightLineEdit = new QLineEdit;
    QLineEdit* temperatureLineEdit = new QLineEdit;
 

    // Create a spinbox
    QSpinBox* weightSpinBox = new QSpinBox();
 
    // Set the range(min, max) of the spinbox
    weightSpinBox->setRange(10, 200);
 

    // Create a Slider
    QSlider* temperatureSlider = new QSlider(Qt::Horizontal);
 
     // Set the range(min, max) of the slider
    temperatureSlider->setRange(0, 100);
 

    // Create the vertical layout
    QVBoxLayout* form = new QVBoxLayout;
 

    // Add spibox, textbox, and slider to the layout
    form->addWidget(weightSpinBox);
    form->addWidget(weightLineEdit);
    form->addWidget(temperatureSlider);
    form->addWidget(temperatureLineEdit);
 

    // Lay out widgets in vertically from top to bottom
    mainWindow->setLayout(form);

    mainWindow->show();
 
    return a.exec();
}



No comments:

Post a Comment