How to write this QT program as class
Unsolved
General and Desktop
-
I'm new in QT, this is code I copy from another source (which I forget).
I dont see class declaration anywhere in this code
How to re-write it using class?/* QGridLayout */ #include <QApplication> #include <QGroupBox> #include <QLabel> #include <QLineEdit> #include <QMainWindow> #include <QPushButton> #include <QTextEdit> #include <QVBoxLayout> #include <QWidget> #include "mainwindow.h" int main( int argc, char *argv[] ) { QApplication a( argc, argv ); QMainWindow window; QWidget *widget = new QWidget( &window ); QGridLayout *layout = new QGridLayout( widget ); window.setCentralWidget( widget ); widget->setLayout( layout ); QGroupBox *box = new QGroupBox( "Information:", widget ); layout->addWidget( box, 0, 0 ); QVBoxLayout *boxLayout = new QVBoxLayout( box ); QWidget *nameWidget = new QWidget( box ); QWidget *ageWidget = new QWidget( box ); QWidget *addressWidget = new QWidget( box ); boxLayout->addWidget( nameWidget ); boxLayout->addWidget( ageWidget ); boxLayout->addWidget( addressWidget ); QHBoxLayout *nameLayout = new QHBoxLayout( nameWidget ); nameLayout->addWidget( new QLabel( "Name:" ) ); nameLayout->addWidget( new QLineEdit( nameWidget ) ); QHBoxLayout *ageLayout = new QHBoxLayout( ageWidget ); ageLayout->addWidget( new QLabel( "Age:" ) ); ageLayout->addWidget( new QLineEdit( ageWidget ) ); QHBoxLayout *addressLayout = new QHBoxLayout( addressWidget ); addressLayout->addWidget( new QLabel( "Address:" ) ); addressLayout->addWidget( new QLineEdit( addressWidget ) ); layout->addWidget( new QPushButton( "Validate", widget ), 1, 0 ); layout->addWidget( new QPushButton( "Reset", widget ), 1, 1 ); layout->addWidget( new QPushButton( "Cancel", widget ), 1, 2 ); window.show(); return a.exec(); }
-
I'm new in QT, this is code I copy from another source (which I forget).
I dont see class declaration anywhere in this code
How to re-write it using class?/* QGridLayout */ #include <QApplication> #include <QGroupBox> #include <QLabel> #include <QLineEdit> #include <QMainWindow> #include <QPushButton> #include <QTextEdit> #include <QVBoxLayout> #include <QWidget> #include "mainwindow.h" int main( int argc, char *argv[] ) { QApplication a( argc, argv ); QMainWindow window; QWidget *widget = new QWidget( &window ); QGridLayout *layout = new QGridLayout( widget ); window.setCentralWidget( widget ); widget->setLayout( layout ); QGroupBox *box = new QGroupBox( "Information:", widget ); layout->addWidget( box, 0, 0 ); QVBoxLayout *boxLayout = new QVBoxLayout( box ); QWidget *nameWidget = new QWidget( box ); QWidget *ageWidget = new QWidget( box ); QWidget *addressWidget = new QWidget( box ); boxLayout->addWidget( nameWidget ); boxLayout->addWidget( ageWidget ); boxLayout->addWidget( addressWidget ); QHBoxLayout *nameLayout = new QHBoxLayout( nameWidget ); nameLayout->addWidget( new QLabel( "Name:" ) ); nameLayout->addWidget( new QLineEdit( nameWidget ) ); QHBoxLayout *ageLayout = new QHBoxLayout( ageWidget ); ageLayout->addWidget( new QLabel( "Age:" ) ); ageLayout->addWidget( new QLineEdit( ageWidget ) ); QHBoxLayout *addressLayout = new QHBoxLayout( addressWidget ); addressLayout->addWidget( new QLabel( "Address:" ) ); addressLayout->addWidget( new QLineEdit( addressWidget ) ); layout->addWidget( new QPushButton( "Validate", widget ), 1, 0 ); layout->addWidget( new QPushButton( "Reset", widget ), 1, 1 ); layout->addWidget( new QPushButton( "Cancel", widget ), 1, 2 ); window.show(); return a.exec(); }
@ElfMoon Quite generic question.
Why do you want to invent a class for this small piece of code?If you want to add a class here then one way to do that in this particular case is to subclass QMainWindow class and move all the UI related stuff into that new class. If you don't know how to do that then I suggest to create a QtWidgets default project in QtCreator and take a look at it.